wasm_hashmap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_hashmap.h"
  6. #include "wasm_log.h"
  7. #include "wasm_thread.h"
  8. #include "wasm_memory.h"
  9. typedef struct HashMapElem {
  10. void *key;
  11. void *value;
  12. struct HashMapElem *next;
  13. } HashMapElem;
  14. struct HashMap {
  15. /* size of element array */
  16. uint32 size;
  17. /* lock for elements */
  18. korp_mutex *lock;
  19. /* hash function of key */
  20. HashFunc hash_func;
  21. /* key equal function */
  22. KeyEqualFunc key_equal_func;
  23. KeyDestroyFunc key_destroy_func;
  24. ValueDestroyFunc value_destroy_func;
  25. HashMapElem *elements[1];
  26. };
  27. HashMap*
  28. wasm_hash_map_create(uint32 size, bool use_lock,
  29. HashFunc hash_func,
  30. KeyEqualFunc key_equal_func,
  31. KeyDestroyFunc key_destroy_func,
  32. ValueDestroyFunc value_destroy_func)
  33. {
  34. HashMap *map;
  35. uint64 total_size;
  36. if (size > HASH_MAP_MAX_SIZE) {
  37. LOG_ERROR("HashMap create failed: size is too large.\n");
  38. return NULL;
  39. }
  40. if (!hash_func || !key_equal_func) {
  41. LOG_ERROR("HashMap create failed: hash function or key equal function "
  42. " is NULL.\n");
  43. return NULL;
  44. }
  45. total_size = offsetof(HashMap, elements) +
  46. sizeof(HashMapElem) * (uint64)size +
  47. (use_lock ? sizeof(korp_mutex) : 0);
  48. if (total_size >= UINT32_MAX
  49. || !(map = wasm_malloc((uint32)total_size))) {
  50. LOG_ERROR("HashMap create failed: alloc memory failed.\n");
  51. return NULL;
  52. }
  53. memset(map, 0, (uint32)total_size);
  54. if (use_lock) {
  55. map->lock = (korp_mutex*)
  56. ((uint8*)map + offsetof(HashMap, elements)
  57. + sizeof(HashMapElem) * size);
  58. if (ws_mutex_init(map->lock, false)) {
  59. LOG_ERROR("HashMap create failed: init map lock failed.\n");
  60. wasm_free(map);
  61. return NULL;
  62. }
  63. }
  64. map->size = size;
  65. map->hash_func = hash_func;
  66. map->key_equal_func = key_equal_func;
  67. map->key_destroy_func = key_destroy_func;
  68. map->value_destroy_func = value_destroy_func;
  69. return map;
  70. }
  71. bool
  72. wasm_hash_map_insert(HashMap *map, void *key, void *value)
  73. {
  74. uint32 index;
  75. HashMapElem *elem;
  76. if (!map || !key) {
  77. LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
  78. return false;
  79. }
  80. if (map->lock) {
  81. ws_mutex_lock(map->lock);
  82. }
  83. index = map->hash_func(key) % map->size;
  84. elem = map->elements[index];
  85. while (elem) {
  86. if (map->key_equal_func(elem->key, key)) {
  87. LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
  88. goto fail;
  89. }
  90. elem = elem->next;
  91. }
  92. if (!(elem = wasm_malloc(sizeof(HashMapElem)))) {
  93. LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
  94. goto fail;
  95. }
  96. elem->key = key;
  97. elem->value = value;
  98. elem->next = map->elements[index];
  99. map->elements[index] = elem;
  100. if (map->lock) {
  101. ws_mutex_unlock(map->lock);
  102. }
  103. return true;
  104. fail:
  105. if (map->lock) {
  106. ws_mutex_unlock(map->lock);
  107. }
  108. return false;
  109. }
  110. void*
  111. wasm_hash_map_find(HashMap *map, void *key)
  112. {
  113. uint32 index;
  114. HashMapElem *elem;
  115. void *value;
  116. if (!map || !key) {
  117. LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
  118. return NULL;
  119. }
  120. if (map->lock) {
  121. ws_mutex_lock(map->lock);
  122. }
  123. index = map->hash_func(key) % map->size;
  124. elem = map->elements[index];
  125. while (elem) {
  126. if (map->key_equal_func(elem->key, key)) {
  127. value = elem->value;
  128. if (map->lock) {
  129. ws_mutex_unlock(map->lock);
  130. }
  131. return value;
  132. }
  133. elem = elem->next;
  134. }
  135. if (map->lock) {
  136. ws_mutex_unlock(map->lock);
  137. }
  138. return NULL;
  139. }
  140. bool
  141. wasm_hash_map_update(HashMap *map, void *key, void *value,
  142. void **p_old_value)
  143. {
  144. uint32 index;
  145. HashMapElem *elem;
  146. if (!map || !key) {
  147. LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
  148. return false;
  149. }
  150. if (map->lock) {
  151. ws_mutex_lock(map->lock);
  152. }
  153. index = map->hash_func(key) % map->size;
  154. elem = map->elements[index];
  155. while (elem) {
  156. if (map->key_equal_func(elem->key, key)) {
  157. if (p_old_value)
  158. *p_old_value = elem->value;
  159. elem->value = value;
  160. if (map->lock) {
  161. ws_mutex_unlock(map->lock);
  162. }
  163. return true;
  164. }
  165. elem = elem->next;
  166. }
  167. if (map->lock) {
  168. ws_mutex_unlock(map->lock);
  169. }
  170. return false;
  171. }
  172. bool
  173. wasm_hash_map_remove(HashMap *map, void *key,
  174. void **p_old_key, void **p_old_value)
  175. {
  176. uint32 index;
  177. HashMapElem *elem, *prev;
  178. if (!map || !key) {
  179. LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
  180. return false;
  181. }
  182. if (map->lock) {
  183. ws_mutex_lock(map->lock);
  184. }
  185. index = map->hash_func(key) % map->size;
  186. prev = elem = map->elements[index];
  187. while (elem) {
  188. if (map->key_equal_func(elem->key, key)) {
  189. if (p_old_key)
  190. *p_old_key = elem->key;
  191. if (p_old_value)
  192. *p_old_value = elem->value;
  193. if (elem == map->elements[index])
  194. map->elements[index] = elem->next;
  195. else
  196. prev->next = elem->next;
  197. wasm_free(elem);
  198. if (map->lock) {
  199. ws_mutex_unlock(map->lock);
  200. }
  201. return true;
  202. }
  203. prev = elem;
  204. elem = elem->next;
  205. }
  206. if (map->lock) {
  207. ws_mutex_unlock(map->lock);
  208. }
  209. return false;
  210. }
  211. bool
  212. wasm_hash_map_destroy(HashMap *map)
  213. {
  214. uint32 index;
  215. HashMapElem *elem, *next;
  216. if (!map) {
  217. LOG_ERROR("HashMap destroy failed: map is NULL.\n");
  218. return false;
  219. }
  220. if (map->lock) {
  221. ws_mutex_lock(map->lock);
  222. }
  223. for (index = 0; index < map->size; index++) {
  224. elem = map->elements[index];
  225. while (elem) {
  226. next = elem->next;
  227. if (map->key_destroy_func) {
  228. map->key_destroy_func(elem->key);
  229. }
  230. if (map->value_destroy_func) {
  231. map->value_destroy_func(elem->value);
  232. }
  233. wasm_free(elem);
  234. elem = next;
  235. }
  236. }
  237. if (map->lock) {
  238. ws_mutex_unlock(map->lock);
  239. ws_mutex_destroy(map->lock);
  240. }
  241. wasm_free(map);
  242. return true;
  243. }