bh_hashmap.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. if (total_size >= UINT32_MAX || !(map = BH_MALLOC((uint32)total_size))) {
  46. LOG_ERROR("HashMap create failed: alloc memory failed.\n");
  47. return NULL;
  48. }
  49. memset(map, 0, (uint32)total_size);
  50. if (use_lock) {
  51. map->lock = (korp_mutex *)((uint8 *)map + offsetof(HashMap, elements)
  52. + sizeof(HashMapElem *) * size);
  53. if (os_mutex_init(map->lock)) {
  54. LOG_ERROR("HashMap create failed: init map lock failed.\n");
  55. BH_FREE(map);
  56. return NULL;
  57. }
  58. }
  59. map->size = size;
  60. map->hash_func = hash_func;
  61. map->key_equal_func = key_equal_func;
  62. map->key_destroy_func = key_destroy_func;
  63. map->value_destroy_func = value_destroy_func;
  64. return map;
  65. }
  66. bool
  67. bh_hash_map_insert(HashMap *map, void *key, void *value)
  68. {
  69. uint32 index;
  70. HashMapElem *elem;
  71. if (!map || !key) {
  72. LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
  73. return false;
  74. }
  75. if (map->lock) {
  76. os_mutex_lock(map->lock);
  77. }
  78. index = map->hash_func(key) % map->size;
  79. elem = map->elements[index];
  80. while (elem) {
  81. if (map->key_equal_func(elem->key, key)) {
  82. LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
  83. goto fail;
  84. }
  85. elem = elem->next;
  86. }
  87. if (!(elem = BH_MALLOC(sizeof(HashMapElem)))) {
  88. LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
  89. goto fail;
  90. }
  91. elem->key = key;
  92. elem->value = value;
  93. elem->next = map->elements[index];
  94. map->elements[index] = elem;
  95. if (map->lock) {
  96. os_mutex_unlock(map->lock);
  97. }
  98. return true;
  99. fail:
  100. if (map->lock) {
  101. os_mutex_unlock(map->lock);
  102. }
  103. return false;
  104. }
  105. void *
  106. bh_hash_map_find(HashMap *map, void *key)
  107. {
  108. uint32 index;
  109. HashMapElem *elem;
  110. void *value;
  111. if (!map || !key) {
  112. LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
  113. return NULL;
  114. }
  115. if (map->lock) {
  116. os_mutex_lock(map->lock);
  117. }
  118. index = map->hash_func(key) % map->size;
  119. elem = map->elements[index];
  120. while (elem) {
  121. if (map->key_equal_func(elem->key, key)) {
  122. value = elem->value;
  123. if (map->lock) {
  124. os_mutex_unlock(map->lock);
  125. }
  126. return value;
  127. }
  128. elem = elem->next;
  129. }
  130. if (map->lock) {
  131. os_mutex_unlock(map->lock);
  132. }
  133. return NULL;
  134. }
  135. bool
  136. bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value)
  137. {
  138. uint32 index;
  139. HashMapElem *elem;
  140. if (!map || !key) {
  141. LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
  142. return false;
  143. }
  144. if (map->lock) {
  145. os_mutex_lock(map->lock);
  146. }
  147. index = map->hash_func(key) % map->size;
  148. elem = map->elements[index];
  149. while (elem) {
  150. if (map->key_equal_func(elem->key, key)) {
  151. if (p_old_value)
  152. *p_old_value = elem->value;
  153. elem->value = value;
  154. if (map->lock) {
  155. os_mutex_unlock(map->lock);
  156. }
  157. return true;
  158. }
  159. elem = elem->next;
  160. }
  161. if (map->lock) {
  162. os_mutex_unlock(map->lock);
  163. }
  164. return false;
  165. }
  166. bool
  167. bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
  168. void **p_old_value)
  169. {
  170. uint32 index;
  171. HashMapElem *elem, *prev;
  172. if (!map || !key) {
  173. LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
  174. return false;
  175. }
  176. if (map->lock) {
  177. os_mutex_lock(map->lock);
  178. }
  179. index = map->hash_func(key) % map->size;
  180. prev = elem = map->elements[index];
  181. while (elem) {
  182. if (map->key_equal_func(elem->key, key)) {
  183. if (p_old_key)
  184. *p_old_key = elem->key;
  185. if (p_old_value)
  186. *p_old_value = elem->value;
  187. if (elem == map->elements[index])
  188. map->elements[index] = elem->next;
  189. else
  190. prev->next = elem->next;
  191. BH_FREE(elem);
  192. if (map->lock) {
  193. os_mutex_unlock(map->lock);
  194. }
  195. return true;
  196. }
  197. prev = elem;
  198. elem = elem->next;
  199. }
  200. if (map->lock) {
  201. os_mutex_unlock(map->lock);
  202. }
  203. return false;
  204. }
  205. bool
  206. bh_hash_map_destroy(HashMap *map)
  207. {
  208. uint32 index;
  209. HashMapElem *elem, *next;
  210. if (!map) {
  211. LOG_ERROR("HashMap destroy failed: map is NULL.\n");
  212. return false;
  213. }
  214. if (map->lock) {
  215. os_mutex_lock(map->lock);
  216. }
  217. for (index = 0; index < map->size; index++) {
  218. elem = map->elements[index];
  219. while (elem) {
  220. next = elem->next;
  221. if (map->key_destroy_func) {
  222. map->key_destroy_func(elem->key);
  223. }
  224. if (map->value_destroy_func) {
  225. map->value_destroy_func(elem->value);
  226. }
  227. BH_FREE(elem);
  228. elem = next;
  229. }
  230. }
  231. if (map->lock) {
  232. os_mutex_unlock(map->lock);
  233. os_mutex_destroy(map->lock);
  234. }
  235. BH_FREE(map);
  236. return true;
  237. }
  238. uint32
  239. bh_hash_map_get_struct_size(HashMap *hashmap)
  240. {
  241. uint32 size = (uint32)(uintptr_t)offsetof(HashMap, elements)
  242. + (uint32)sizeof(HashMapElem *) * hashmap->size;
  243. if (hashmap->lock) {
  244. size += (uint32)sizeof(korp_mutex);
  245. }
  246. return size;
  247. }
  248. uint32
  249. bh_hash_map_get_elem_struct_size()
  250. {
  251. return (uint32)sizeof(HashMapElem);
  252. }
  253. bool
  254. bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
  255. void *user_data)
  256. {
  257. uint32 index;
  258. HashMapElem *elem, *next;
  259. if (!map || !callback) {
  260. LOG_ERROR("HashMap traverse failed: map or callback is NULL.\n");
  261. return false;
  262. }
  263. if (map->lock) {
  264. os_mutex_lock(map->lock);
  265. }
  266. for (index = 0; index < map->size; index++) {
  267. elem = map->elements[index];
  268. while (elem) {
  269. next = elem->next;
  270. callback(elem->key, elem->value, user_data);
  271. elem = next;
  272. }
  273. }
  274. if (map->lock) {
  275. os_mutex_unlock(map->lock);
  276. }
  277. return true;
  278. }