bh_hashmap.c 7.4 KB

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