Repair Doxygen build infrastructure
[vpp.git] / src / plugins / lb / refcount.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /*
17  * vlib provides lock-free counters but those
18  * - Have 16bits per-CPU counter, which may overflow.
19  * - Would only increment.
20  *
21  * This is very similar to vlib counters, but may be used to count reference.
22  * Such a counter includes an arbitrary number of counters. Each counter
23  * is identified by its index. This is used to aggregate per-cpu memory.
24  *
25  * Warning:
26  *   This reference counter is lock-free but is not race-condition free.
27  *   The counting result is approximate and another mechanism needs to be used
28  *   in order to ensure that an object may be freed.
29  *
30  */
31
32 #include <vnet/vnet.h>
33
34 typedef struct {
35   u32 *counters;
36   u32 length;
37   u32 *reader_lengths;
38   CLIB_CACHE_LINE_ALIGN_MARK(o);
39 } vlib_refcount_per_cpu_t;
40
41 typedef struct {
42   vlib_refcount_per_cpu_t *per_cpu;
43 } vlib_refcount_t;
44
45 void __vlib_refcount_resize(vlib_refcount_per_cpu_t *per_cpu, u32 size);
46
47 static_always_inline
48 void vlib_refcount_add(vlib_refcount_t *r, u32 cpu_index, u32 counter_index, i32 v)
49 {
50   vlib_refcount_per_cpu_t *per_cpu = &r->per_cpu[cpu_index];
51   if (PREDICT_FALSE(counter_index >= per_cpu->length))
52     __vlib_refcount_resize(per_cpu, clib_max(counter_index + 16, per_cpu->length * 2));
53
54   per_cpu->counters[counter_index] += v;
55 }
56
57 u64 vlib_refcount_get(vlib_refcount_t *r, u32 index);
58
59 static_always_inline
60 void vlib_refcount_init(vlib_refcount_t *r)
61 {
62   vlib_thread_main_t *tm = vlib_get_thread_main ();
63   r->per_cpu = 0;
64   vec_validate (r->per_cpu, tm->n_vlib_mains - 1);
65 }
66
67