1 /*
2  * Copyright (C) 2011, 2012, 2013 Citrix Systems
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the project nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef __TURN_MAPS__
32 #define __TURN_MAPS__
33 
34 #include "ns_turn_ioaddr.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 //////////////// UR MAP //////////////////
41 
42 struct _ur_map;
43 typedef struct _ur_map ur_map;
44 
45 //////////////// Common Definitions //////
46 
47 typedef uint64_t ur_map_key_type;
48 typedef unsigned long ur_map_value_type;
49 
50 typedef void (*ur_map_del_func)(ur_map_value_type);
51 
52 typedef int (*foreachcb_type)(ur_map_key_type key, ur_map_value_type value);
53 typedef int (*foreachcb_arg_type)(ur_map_key_type key,
54 				  ur_map_value_type value,
55 				  void *arg);
56 
57 ///////////// non-local map /////////////////////
58 
59 ur_map* ur_map_create(void);
60 
61 /**
62  * @ret:
63  * 0 - success
64  * -1 - error
65  */
66 
67 int ur_map_put(ur_map* map, ur_map_key_type key, ur_map_value_type value);
68 
69 /**
70  * @ret:
71  * 1 - success
72  * 0 - not found
73  */
74 
75 int ur_map_get(const ur_map* map, ur_map_key_type key, ur_map_value_type *value);
76 /**
77  * @ret:
78  * 1 - success
79  * 0 - not found
80  */
81 
82 int ur_map_del(ur_map* map, ur_map_key_type key,ur_map_del_func delfunc);
83 /**
84  * @ret:
85  * 1 - success
86  * 0 - not found
87  */
88 
89 int ur_map_exist(const ur_map* map, ur_map_key_type key);
90 
91 void ur_map_free(ur_map** map);
92 
93 size_t ur_map_size(const ur_map* map);
94 
95 int ur_map_foreach(ur_map* map, foreachcb_type func);
96 
97 int ur_map_foreach_arg(ur_map* map, foreachcb_arg_type func, void* arg);
98 
99 int ur_map_lock(const ur_map* map);
100 int ur_map_unlock(const ur_map* map);
101 
102 ///////////// "local" map /////////////////////
103 
104 #define LM_MAP_HASH_SIZE (8)
105 #define LM_MAP_ARRAY_SIZE (3)
106 
107 typedef struct _lm_map_array {
108 	ur_map_key_type main_keys[LM_MAP_ARRAY_SIZE];
109 	ur_map_value_type main_values[LM_MAP_ARRAY_SIZE];
110 	size_t extra_sz;
111 	ur_map_key_type **extra_keys;
112 	ur_map_value_type **extra_values;
113 } lm_map_array;
114 
115 typedef struct _lm_map {
116 	lm_map_array table[LM_MAP_HASH_SIZE];
117 } lm_map;
118 
119 void lm_map_init(lm_map *map);
120 
121 /**
122  * @ret:
123  * 0 - success
124  * -1 - error
125  */
126 
127 int lm_map_put(lm_map* map, ur_map_key_type key, ur_map_value_type value);
128 
129 /**
130  * @ret:
131  * 1 - success
132  * 0 - not found
133  */
134 
135 int lm_map_get(const lm_map* map, ur_map_key_type key, ur_map_value_type *value);
136 /**
137  * @ret:
138  * 1 - success
139  * 0 - not found
140  */
141 
142 int lm_map_del(lm_map* map, ur_map_key_type key,ur_map_del_func delfunc);
143 /**
144  * @ret:
145  * 1 - success
146  * 0 - not found
147  */
148 
149 int lm_map_exist(const lm_map* map, ur_map_key_type key);
150 
151 void lm_map_clean(lm_map* map);
152 
153 size_t lm_map_size(const lm_map* map);
154 
155 int lm_map_foreach(lm_map* map, foreachcb_type func);
156 
157 int lm_map_foreach_arg(lm_map* map, foreachcb_arg_type func, void* arg);
158 
159 //////////////// UR ADDR MAP //////////////////
160 
161 typedef unsigned long ur_addr_map_value_type;
162 
163 #define ADDR_MAP_SIZE (1024)
164 #define ADDR_ARRAY_SIZE (4)
165 
166 typedef struct _addr_elem {
167   ioa_addr key;
168   ur_addr_map_value_type value;
169 } addr_elem;
170 
171 typedef struct _addr_list_header {
172   addr_elem main_list[ADDR_ARRAY_SIZE];
173   addr_elem *extra_list;
174   size_t extra_sz;
175 } addr_list_header;
176 
177 struct _ur_addr_map {
178   addr_list_header lists[ADDR_MAP_SIZE];
179   uint64_t magic;
180 };
181 
182 struct _ur_addr_map;
183 typedef struct _ur_addr_map ur_addr_map;
184 
185 typedef void (*ur_addr_map_func)(ur_addr_map_value_type);
186 
187 void ur_addr_map_init(ur_addr_map* map);
188 void ur_addr_map_clean(ur_addr_map* map);
189 
190 /**
191  * @ret:
192  * 0 - success
193  * -1 - error
194  * if the addr key exists, the value is updated.
195  */
196 int ur_addr_map_put(ur_addr_map* map, ioa_addr* key, ur_addr_map_value_type value);
197 
198 /**
199  * @ret:
200  * 1 - success
201  * 0 - not found
202  */
203 int ur_addr_map_get(const ur_addr_map* map, ioa_addr* key, ur_addr_map_value_type *value);
204 
205 /**
206  * @ret:
207  * 1 - success
208  * 0 - not found
209  */
210 int ur_addr_map_del(ur_addr_map* map, ioa_addr* key,ur_addr_map_func func);
211 
212 /**
213  * @ret:
214  * 1 - success
215  * 0 - not found
216  */
217 void ur_addr_map_foreach(ur_addr_map* map, ur_addr_map_func func);
218 
219 size_t ur_addr_map_num_elements(const ur_addr_map* map);
220 size_t ur_addr_map_size(const ur_addr_map* map);
221 
222 //////////////// UR STRING MAP //////////////////
223 
224 typedef char* ur_string_map_key_type;
225 typedef void* ur_string_map_value_type;
226 struct _ur_string_map;
227 typedef struct _ur_string_map ur_string_map;
228 
229 typedef void (*ur_string_map_func)(ur_string_map_value_type);
230 
231 ur_string_map* ur_string_map_create(ur_string_map_func del_value_func);
232 
233 /**
234  * @ret:
235  * 0 - success
236  * -1 - error
237  * if the string key exists, and the value is different, return error.
238  */
239 int ur_string_map_put(ur_string_map* map, const ur_string_map_key_type key, ur_string_map_value_type value);
240 
241 /**
242  * @ret:
243  * 1 - success
244  * 0 - not found
245  */
246 int ur_string_map_get(ur_string_map* map, const ur_string_map_key_type key, ur_string_map_value_type *value);
247 
248 /**
249  * @ret:
250  * 1 - success
251  * 0 - not found
252  */
253 int ur_string_map_del(ur_string_map* map, const ur_string_map_key_type key);
254 
255 void ur_string_map_clean(ur_string_map* map);
256 void ur_string_map_free(ur_string_map** map);
257 
258 size_t ur_string_map_size(const ur_string_map* map);
259 
260 int ur_string_map_lock(const ur_string_map* map);
261 int ur_string_map_unlock(const ur_string_map* map);
262 
263 ////////////////////////////////////////////
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif //__TURN_MAPS__
270