bh_hashmap.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_hashmap.h"
  6. typedef struct HashMapElem {
  7. void *key;
  8. void *value;
  9. struct HashMapElem *next;
  10. } HashMapElem;
  11. struct HashMap {
  12. /* size of element array */
  13. uint32 size;
  14. /* lock for elements */
  15. korp_mutex *lock;
  16. /* hash function of key */
  17. HashFunc hash_func;
  18. /* key equal function */
  19. KeyEqualFunc key_equal_func;
  20. KeyDestroyFunc key_destroy_func;
  21. ValueDestroyFunc value_destroy_func;
  22. HashMapElem *elements[1];
  23. };
  24. HashMap *
  25. bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
  26. KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
  27. ValueDestroyFunc value_destroy_func)
  28. {
  29. HashMap *map;
  30. uint64 total_size;
  31. if (size < HASH_MAP_MIN_SIZE)
  32. size = HASH_MAP_MIN_SIZE;
  33. if (size > HASH_MAP_MAX_SIZE) {
  34. LOG_ERROR("HashMap create failed: size is too large.\n");
  35. return NULL;
  36. }
  37. if (!hash_func || !key_equal_func) {
  38. LOG_ERROR("HashMap create failed: hash function or key equal function "
  39. " is NULL.\n");
  40. return NULL;
  41. }
  42. total_size = offsetof(HashMap, elements)
  43. + sizeof(HashMapElem *) * (uint64)size
  44. + (use_lock ? sizeof(korp_mutex) : 0);
  45. /* size <= HASH_MAP_MAX_SIZE, so total_size won't be larger than
  46. UINT32_MAX, no need to check integer overflow */
  47. if (!(map = BH_MALLOC((uint32)total_size))) {
  48. LOG_ERROR("HashMap create failed: alloc memory failed.\n");
  49. return NULL;
  50. }
  51. memset(map, 0, (uint32)total_size);
  52. if (use_lock) {
  53. map->lock = (korp_mutex *)((uint8 *)map + offsetof(HashMap, elements)
  54. + sizeof(HashMapElem *) * size);
  55. if (os_mutex_init(map->lock)) {
  56. LOG_ERROR("HashMap create failed: init map lock failed.\n");
  57. BH_FREE(map);
  58. return NULL;
  59. }
  60. }
  61. map->size = size;
  62. map->hash_func = hash_func;
  63. map->key_equal_func = key_equal_func;
  64. map->key_destroy_func = key_destroy_func;
  65. map->value_destroy_func = value_destroy_func;
  66. return map;
  67. }
  68. bool
  69. bh_hash_map_insert(HashMap *map, void *key, void *value)
  70. {
  71. uint32 index;
  72. HashMapElem *elem;
  73. if (!map || !key) {
  74. LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
  75. return false;
  76. }
  77. if (map->lock) {
  78. os_mutex_lock(map->lock);
  79. }
  80. index = map->hash_func(key) % map->size;
  81. elem = map->elements[index];
  82. while (elem) {
  83. if (map->key_equal_func(elem->key, key)) {
  84. LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
  85. goto fail;
  86. }
  87. elem = elem->next;
  88. }
  89. if (!(elem = BH_MALLOC(sizeof(HashMapElem)))) {
  90. LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
  91. goto fail;
  92. }
  93. elem->key = key;
  94. elem->value = value;
  95. elem->next = map->elements[index];
  96. map->elements[index] = elem;
  97. if (map->lock) {
  98. os_mutex_unlock(map->lock);
  99. }
  100. return true;
  101. fail:
  102. if (map->lock) {
  103. os_mutex_unlock(map->lock);
  104. }
  105. return false;
  106. }
  107. void *
  108. bh_hash_map_find(HashMap *map, void *key)
  109. {
  110. uint32 index;
  111. HashMapElem *elem;
  112. void *value;
  113. if (!map || !key) {
  114. LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
  115. return NULL;
  116. }
  117. if (map->lock) {
  118. os_mutex_lock(map->lock);
  119. }
  120. index = map->hash_func(key) % map->size;
  121. elem = map->elements[index];
  122. while (elem) {
  123. if (map->key_equal_func(elem->key, key)) {
  124. value = elem->value;
  125. if (map->lock) {
  126. os_mutex_unlock(map->lock);
  127. }
  128. return value;
  129. }
  130. elem = elem->next;
  131. }
  132. if (map->lock) {
  133. os_mutex_unlock(map->lock);
  134. }
  135. return NULL;
  136. }
  137. bool
  138. bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value)
  139. {
  140. uint32 index;
  141. HashMapElem *elem;
  142. if (!map || !key) {
  143. LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
  144. return false;
  145. }
  146. if (map->lock) {
  147. os_mutex_lock(map->lock);
  148. }
  149. index = map->hash_func(key) % map->size;
  150. elem = map->elements[index];
  151. while (elem) {
  152. if (map->key_equal_func(elem->key, key)) {
  153. if (p_old_value)
  154. *p_old_value = elem->value;
  155. elem->value = value;
  156. if (map->lock) {
  157. os_mutex_unlock(map->lock);
  158. }
  159. return true;
  160. }
  161. elem = elem->next;
  162. }
  163. if (map->lock) {
  164. os_mutex_unlock(map->lock);
  165. }
  166. return false;
  167. }
  168. bool
  169. bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
  170. void **p_old_value)
  171. {
  172. uint32 index;
  173. HashMapElem *elem, *prev;
  174. if (!map || !key) {
  175. LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
  176. return false;
  177. }
  178. if (map->lock) {
  179. os_mutex_lock(map->lock);
  180. }
  181. index = map->hash_func(key) % map->size;
  182. prev = elem = map->elements[index];
  183. while (elem) {
  184. if (map->key_equal_func(elem->key, key)) {
  185. if (p_old_key)
  186. *p_old_key = elem->key;
  187. if (p_old_value)
  188. *p_old_value = elem->value;
  189. if (elem == map->elements[index])
  190. map->elements[index] = elem->next;
  191. else
  192. prev->next = elem->next;
  193. BH_FREE(elem);
  194. if (map->lock) {
  195. os_mutex_unlock(map->lock);
  196. }
  197. return true;
  198. }
  199. prev = elem;
  200. elem = elem->next;
  201. }
  202. if (map->lock) {
  203. os_mutex_unlock(map->lock);
  204. }
  205. return false;
  206. }
  207. bool
  208. bh_hash_map_destroy(HashMap *map)
  209. {
  210. uint32 index;
  211. HashMapElem *elem, *next;
  212. if (!map) {
  213. LOG_ERROR("HashMap destroy failed: map is NULL.\n");
  214. return false;
  215. }
  216. if (map->lock) {
  217. os_mutex_lock(map->lock);
  218. }
  219. for (index = 0; index < map->size; index++) {
  220. elem = map->elements[index];
  221. while (elem) {
  222. next = elem->next;
  223. if (map->key_destroy_func) {
  224. map->key_destroy_func(elem->key);
  225. }
  226. if (map->value_destroy_func) {
  227. map->value_destroy_func(elem->value);
  228. }
  229. BH_FREE(elem);
  230. elem = next;
  231. }
  232. }
  233. if (map->lock) {
  234. os_mutex_unlock(map->lock);
  235. os_mutex_destroy(map->lock);
  236. }
  237. BH_FREE(map);
  238. return true;
  239. }
  240. uint32
  241. bh_hash_map_get_struct_size(HashMap *hashmap)
  242. {
  243. uint32 size = (uint32)(uintptr_t)offsetof(HashMap, elements)
  244. + (uint32)sizeof(HashMapElem *) * hashmap->size;
  245. if (hashmap->lock) {
  246. size += (uint32)sizeof(korp_mutex);
  247. }
  248. return size;
  249. }
  250. uint32
  251. bh_hash_map_get_elem_struct_size()
  252. {
  253. return (uint32)sizeof(HashMapElem);
  254. }
  255. bool
  256. bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
  257. void *user_data)
  258. {
  259. uint32 index;
  260. HashMapElem *elem, *next;
  261. if (!map || !callback) {
  262. LOG_ERROR("HashMap traverse failed: map or callback is NULL.\n");
  263. return false;
  264. }
  265. if (map->lock) {
  266. os_mutex_lock(map->lock);
  267. }
  268. for (index = 0; index < map->size; index++) {
  269. elem = map->elements[index];
  270. while (elem) {
  271. next = elem->next;
  272. callback(elem->key, elem->value, user_data);
  273. elem = next;
  274. }
  275. }
  276. if (map->lock) {
  277. os_mutex_unlock(map->lock);
  278. }
  279. return true;
  280. }