ns_turn_maps.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #ifndef __TURN_MAPS__
  31. #define __TURN_MAPS__
  32. #include "ns_turn_ioaddr.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. //////////////// UR MAP //////////////////
  37. struct _ur_map;
  38. typedef struct _ur_map ur_map;
  39. //////////////// Common Definitions //////
  40. typedef u64bits ur_map_key_type;
  41. typedef unsigned long ur_map_value_type;
  42. typedef void (*ur_map_del_func)(ur_map_value_type);
  43. typedef int (*foreachcb_type)(ur_map_key_type key, ur_map_value_type value);
  44. typedef int (*foreachcb_arg_type)(ur_map_key_type key,
  45. ur_map_value_type value,
  46. void *arg);
  47. ///////////// non-local map /////////////////////
  48. ur_map* ur_map_create(void);
  49. /**
  50. * @ret:
  51. * 0 - success
  52. * -1 - error
  53. */
  54. int ur_map_put(ur_map* map, ur_map_key_type key, ur_map_value_type value);
  55. /**
  56. * @ret:
  57. * 1 - success
  58. * 0 - not found
  59. */
  60. int ur_map_get(const ur_map* map, ur_map_key_type key, ur_map_value_type *value);
  61. /**
  62. * @ret:
  63. * 1 - success
  64. * 0 - not found
  65. */
  66. int ur_map_del(ur_map* map, ur_map_key_type key,ur_map_del_func delfunc);
  67. /**
  68. * @ret:
  69. * 1 - success
  70. * 0 - not found
  71. */
  72. int ur_map_exist(const ur_map* map, ur_map_key_type key);
  73. void ur_map_free(ur_map** map);
  74. size_t ur_map_size(const ur_map* map);
  75. int ur_map_foreach(ur_map* map, foreachcb_type func);
  76. int ur_map_foreach_arg(ur_map* map, foreachcb_arg_type func, void* arg);
  77. int ur_map_lock(const ur_map* map);
  78. int ur_map_unlock(const ur_map* map);
  79. ///////////// "local" map /////////////////////
  80. #define LM_MAP_HASH_SIZE (8)
  81. #define LM_MAP_ARRAY_SIZE (3)
  82. typedef struct _lm_map_array {
  83. ur_map_key_type main_keys[LM_MAP_ARRAY_SIZE];
  84. ur_map_value_type main_values[LM_MAP_ARRAY_SIZE];
  85. size_t extra_sz;
  86. ur_map_key_type **extra_keys;
  87. ur_map_value_type **extra_values;
  88. } lm_map_array;
  89. typedef struct _lm_map {
  90. lm_map_array table[LM_MAP_HASH_SIZE];
  91. } lm_map;
  92. void lm_map_init(lm_map *map);
  93. /**
  94. * @ret:
  95. * 0 - success
  96. * -1 - error
  97. */
  98. int lm_map_put(lm_map* map, ur_map_key_type key, ur_map_value_type value);
  99. /**
  100. * @ret:
  101. * 1 - success
  102. * 0 - not found
  103. */
  104. int lm_map_get(const lm_map* map, ur_map_key_type key, ur_map_value_type *value);
  105. /**
  106. * @ret:
  107. * 1 - success
  108. * 0 - not found
  109. */
  110. int lm_map_del(lm_map* map, ur_map_key_type key,ur_map_del_func delfunc);
  111. /**
  112. * @ret:
  113. * 1 - success
  114. * 0 - not found
  115. */
  116. int lm_map_exist(const lm_map* map, ur_map_key_type key);
  117. void lm_map_clean(lm_map* map);
  118. size_t lm_map_size(const lm_map* map);
  119. int lm_map_foreach(lm_map* map, foreachcb_type func);
  120. int lm_map_foreach_arg(lm_map* map, foreachcb_arg_type func, void* arg);
  121. //////////////// UR ADDR MAP //////////////////
  122. typedef unsigned long ur_addr_map_value_type;
  123. #define ADDR_MAP_SIZE (1024)
  124. #define ADDR_ARRAY_SIZE (4)
  125. typedef struct _addr_elem {
  126. ioa_addr key;
  127. ur_addr_map_value_type value;
  128. } addr_elem;
  129. typedef struct _addr_list_header {
  130. addr_elem main_list[ADDR_ARRAY_SIZE];
  131. addr_elem *extra_list;
  132. size_t extra_sz;
  133. } addr_list_header;
  134. struct _ur_addr_map {
  135. addr_list_header lists[ADDR_MAP_SIZE];
  136. u64bits magic;
  137. };
  138. struct _ur_addr_map;
  139. typedef struct _ur_addr_map ur_addr_map;
  140. typedef void (*ur_addr_map_func)(ur_addr_map_value_type);
  141. void ur_addr_map_init(ur_addr_map* map);
  142. void ur_addr_map_clean(ur_addr_map* map);
  143. /**
  144. * @ret:
  145. * 0 - success
  146. * -1 - error
  147. * if the addr key exists, the value is updated.
  148. */
  149. int ur_addr_map_put(ur_addr_map* map, ioa_addr* key, ur_addr_map_value_type value);
  150. /**
  151. * @ret:
  152. * 1 - success
  153. * 0 - not found
  154. */
  155. int ur_addr_map_get(const ur_addr_map* map, ioa_addr* key, ur_addr_map_value_type *value);
  156. /**
  157. * @ret:
  158. * 1 - success
  159. * 0 - not found
  160. */
  161. int ur_addr_map_del(ur_addr_map* map, ioa_addr* key,ur_addr_map_func func);
  162. /**
  163. * @ret:
  164. * 1 - success
  165. * 0 - not found
  166. */
  167. void ur_addr_map_foreach(ur_addr_map* map, ur_addr_map_func func);
  168. size_t ur_addr_map_num_elements(const ur_addr_map* map);
  169. size_t ur_addr_map_size(const ur_addr_map* map);
  170. //////////////// UR STRING MAP //////////////////
  171. typedef s08bits* ur_string_map_key_type;
  172. typedef void* ur_string_map_value_type;
  173. struct _ur_string_map;
  174. typedef struct _ur_string_map ur_string_map;
  175. typedef void (*ur_string_map_func)(ur_string_map_value_type);
  176. ur_string_map* ur_string_map_create(ur_string_map_func del_value_func);
  177. /**
  178. * @ret:
  179. * 0 - success
  180. * -1 - error
  181. * if the string key exists, and the value is different, return error.
  182. */
  183. int ur_string_map_put(ur_string_map* map, const ur_string_map_key_type key, ur_string_map_value_type value);
  184. /**
  185. * @ret:
  186. * 1 - success
  187. * 0 - not found
  188. */
  189. int ur_string_map_get(ur_string_map* map, const ur_string_map_key_type key, ur_string_map_value_type *value);
  190. /**
  191. * @ret:
  192. * 1 - success
  193. * 0 - not found
  194. */
  195. int ur_string_map_del(ur_string_map* map, const ur_string_map_key_type key);
  196. void ur_string_map_clean(ur_string_map* map);
  197. void ur_string_map_free(ur_string_map** map);
  198. size_t ur_string_map_size(const ur_string_map* map);
  199. int ur_string_map_lock(const ur_string_map* map);
  200. int ur_string_map_unlock(const ur_string_map* map);
  201. ////////////////////////////////////////////
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif //__TURN_MAPS__