Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / slist.h
1 /*
2   Copyright (c) 2012 Cisco and/or its affiliates.
3
4   * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #ifndef included_slist_h
18 #define included_slist_h
19
20 #include <stdarg.h>
21 #include <vppinfra/clib.h>
22 #include <vppinfra/vec.h>
23 #include <vppinfra/pool.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/format.h>
26 #include <vppinfra/cache.h>
27
28 typedef word (clib_slist_key_compare_function_t)
29 (void *key, u32 elt_pool_index);
30
31 typedef enum {
32   CLIB_SLIST_MATCH = 0,
33   CLIB_SLIST_NO_MATCH
34 } clib_slist_search_result_t;
35
36 typedef struct {
37   /* Vector of next elements. Every valid instance has at least one */
38   union {
39     u32 next0[2];
40     u32 *nexts;
41   } n;
42
43   /* Index of item in user's pool */
44   u32 user_pool_index;
45   /* $$$ pad to even divisor of cache line */
46 } clib_slist_elt_t;
47
48 static inline u32 clib_slist_get_next_at_level (clib_slist_elt_t * elt, 
49                                                 int level)
50 {
51   if (elt->n.next0[0] & 1)
52     {
53       ASSERT (level < 2);
54       if (level == 1)
55         return elt->n.next0[1];
56       /* preserve ~0 (end of list) */
57       return (elt->n.next0[0] == (u32)~0) ? elt->n.next0[0] :
58         (elt->n.next0[0]>>1);
59     }
60   else
61     {
62       ASSERT(level < vec_len (elt->n.nexts));
63       return elt->n.nexts[level];
64     }
65 }
66
67 static inline void clib_slist_set_next_at_level (clib_slist_elt_t * elt,
68                                                  u32 index, int level)
69 {
70   u32 old_level0_value[2];
71   /* level0 and not a vector */
72   if (level < 2 && (elt->n.next0[0] == 0 || elt->n.next0[0] & 1))
73     {
74       if (level == 0)
75         {
76           elt->n.next0[0] = (index<<1) | 1;
77           return;
78         }
79       elt->n.next0[1] = index;
80       return;
81     }
82   /* have to save old level0 values? */
83   if (elt->n.next0[0] & 1)
84     {
85       old_level0_value[0] = (elt->n.next0[0] == (u32)~0) ? 
86         elt->n.next0[0] : elt->n.next0[0]>>1;
87       old_level0_value[1] = elt->n.next0[1];
88       elt->n.nexts = 0;
89       vec_add1 (elt->n.nexts, old_level0_value[0]);
90       vec_add1 (elt->n.nexts, old_level0_value[1]);
91     }
92   vec_validate (elt->n.nexts, level);
93   elt->n.nexts[level] = index;
94 }
95
96
97 typedef struct {
98   /* pool of skip-list elements */
99   clib_slist_elt_t *elts;
100
101   /* last search path */
102   u32 *path;
103
104   /* last search number of compares */
105   u32 ncompares;
106
107   /* occupancy stats */
108   u32 *occupancy;
109
110   /* Comparison function */
111   clib_slist_key_compare_function_t *compare;
112
113   /* Format function */
114   format_function_t *format_user_element;
115
116   /* items appear in successive plies with Pr (1 / branching_factor) */
117   f64 branching_factor;
118
119   /* random seed */
120   u32 seed;
121 } clib_slist_t;
122
123 clib_error_t *
124 clib_slist_init (clib_slist_t *sp, f64 branching_factor, 
125                  clib_slist_key_compare_function_t compare,
126                  format_function_t format_user_element);
127
128 format_function_t format_slist;
129
130 void clib_slist_add (clib_slist_t *sp, void *key, u32 user_pool_index);
131 clib_slist_search_result_t 
132 clib_slist_del (clib_slist_t *sp, void *key);
133 u32 clib_slist_search (clib_slist_t *sp, void *key, u32 *ncompares);
134
135 #endif /* included_slist_h */