bh_hashmap.c 7.5 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,
  26. HashFunc hash_func,
  27. KeyEqualFunc key_equal_func,
  28. KeyDestroyFunc key_destroy_func,
  29. ValueDestroyFunc value_destroy_func)
  30. {
  31. HashMap *map;
  32. uint64 total_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
  46. || !(map = BH_MALLOC((uint32)total_size))) {
  47. LOG_ERROR("HashMap create failed: alloc memory failed.\n");
  48. return NULL;
  49. }
  50. memset(map, 0, (uint32)total_size);
  51. if (use_lock) {
  52. map->lock = (korp_mutex*)
  53. ((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,
  139. void **p_old_value)
  140. {
  141. uint32 index;
  142. HashMapElem *elem;
  143. if (!map || !key) {
  144. LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
  145. return false;
  146. }
  147. if (map->lock) {
  148. os_mutex_lock(map->lock);
  149. }
  150. index = map->hash_func(key) % map->size;
  151. elem = map->elements[index];
  152. while (elem) {
  153. if (map->key_equal_func(elem->key, key)) {
  154. if (p_old_value)
  155. *p_old_value = elem->value;
  156. elem->value = value;
  157. if (map->lock) {
  158. os_mutex_unlock(map->lock);
  159. }
  160. return true;
  161. }
  162. elem = elem->next;
  163. }
  164. if (map->lock) {
  165. os_mutex_unlock(map->lock);
  166. }
  167. return false;
  168. }
  169. bool
  170. bh_hash_map_remove(HashMap *map, void *key,
  171. void **p_old_key, void **p_old_value)
  172. {
  173. uint32 index;
  174. HashMapElem *elem, *prev;
  175. if (!map || !key) {
  176. LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
  177. return false;
  178. }
  179. if (map->lock) {
  180. os_mutex_lock(map->lock);
  181. }
  182. index = map->hash_func(key) % map->size;
  183. prev = elem = map->elements[index];
  184. while (elem) {
  185. if (map->key_equal_func(elem->key, key)) {
  186. if (p_old_key)
  187. *p_old_key = elem->key;
  188. if (p_old_value)
  189. *p_old_value = elem->value;
  190. if (elem == map->elements[index])
  191. map->elements[index] = elem->next;
  192. else
  193. prev->next = elem->next;
  194. BH_FREE(elem);
  195. if (map->lock) {
  196. os_mutex_unlock(map->lock);
  197. }
  198. return true;
  199. }
  200. prev = elem;
  201. elem = elem->next;
  202. }
  203. if (map->lock) {
  204. os_mutex_unlock(map->lock);
  205. }
  206. return false;
  207. }
  208. bool
  209. bh_hash_map_destroy(HashMap *map)
  210. {
  211. uint32 index;
  212. HashMapElem *elem, *next;
  213. if (!map) {
  214. LOG_ERROR("HashMap destroy failed: map is NULL.\n");
  215. return false;
  216. }
  217. if (map->lock) {
  218. os_mutex_lock(map->lock);
  219. }
  220. for (index = 0; index < map->size; index++) {
  221. elem = map->elements[index];
  222. while (elem) {
  223. next = elem->next;
  224. if (map->key_destroy_func) {
  225. map->key_destroy_func(elem->key);
  226. }
  227. if (map->value_destroy_func) {
  228. map->value_destroy_func(elem->value);
  229. }
  230. BH_FREE(elem);
  231. elem = next;
  232. }
  233. }
  234. if (map->lock) {
  235. os_mutex_unlock(map->lock);
  236. os_mutex_destroy(map->lock);
  237. }
  238. BH_FREE(map);
  239. return true;
  240. }
  241. uint32
  242. bh_hash_map_get_struct_size(HashMap *hashmap)
  243. {
  244. uint32 size = (uint32)(uintptr_t)offsetof(HashMap, elements)
  245. + (uint32)sizeof(HashMapElem *) * hashmap->size;
  246. if (hashmap->lock) {
  247. size += (uint32)sizeof(korp_mutex);
  248. }
  249. return size;
  250. }
  251. uint32
  252. bh_hash_map_get_elem_struct_size()
  253. {
  254. return (uint32)sizeof(HashMapElem);
  255. }
  256. bool
  257. bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
  258. void *user_data)
  259. {
  260. uint32 index;
  261. HashMapElem *elem, *next;
  262. if (!map || !callback) {
  263. LOG_ERROR("HashMap traverse failed: map or callback is NULL.\n");
  264. return false;
  265. }
  266. if (map->lock) {
  267. os_mutex_lock(map->lock);
  268. }
  269. for (index = 0; index < map->size; index++) {
  270. elem = map->elements[index];
  271. while (elem) {
  272. next = elem->next;
  273. callback(elem->key, elem->value, user_data);
  274. elem = next;
  275. }
  276. }
  277. if (map->lock) {
  278. os_mutex_unlock(map->lock);
  279. }
  280. return true;
  281. }