bh_hashmap.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef WASM_HASHMAP_H
  6. #define WASM_HASHMAP_H
  7. #include "bh_platform.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* Maximum initial size of hash map */
  12. #define HASH_MAP_MAX_SIZE 65536
  13. struct HashMap;
  14. typedef struct HashMap HashMap;
  15. /* Hash function: to get the hash value of key. */
  16. typedef uint32 (*HashFunc)(const void *key);
  17. /* Key equal function: to check whether two keys are equal. */
  18. typedef bool (*KeyEqualFunc)(void *key1, void *key2);
  19. /* Key destroy function: to destroy the key, auto called
  20. when an hash element is removed. */
  21. typedef void (*KeyDestroyFunc)(void *key);
  22. /* Value destroy function: to destroy the value, auto called
  23. when an hash element is removed. */
  24. typedef void (*ValueDestroyFunc)(void *key);
  25. /* traverse callback function:
  26. auto called when traverse every hash element */
  27. typedef void (*TraverseCallbackFunc)(void *key, void *value, void *user_data);
  28. /**
  29. * Create a hash map.
  30. *
  31. * @param size: the initial size of the hash map
  32. * @param use_lock whether to lock the hash map when operating on it
  33. * @param hash_func hash function of the key, must be specified
  34. * @param key_equal_func key equal function, check whether two keys
  35. * are equal, must be specified
  36. * @param key_destroy_func key destroy function, called when an hash element
  37. * is removed if it is not NULL
  38. * @param value_destroy_func value destroy function, called when an hash
  39. * element is removed if it is not NULL
  40. *
  41. * @return the hash map created, NULL if failed
  42. */
  43. HashMap *
  44. bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
  45. KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
  46. ValueDestroyFunc value_destroy_func);
  47. /**
  48. * Insert an element to the hash map
  49. *
  50. * @param map the hash map to insert element
  51. * @key the key of the element
  52. * @value the value of the element
  53. *
  54. * @return true if success, false otherwise
  55. * Note: fail if key is NULL or duplicated key exists in the hash map,
  56. */
  57. bool
  58. bh_hash_map_insert(HashMap *map, void *key, void *value);
  59. /**
  60. * Insert an element to the hash map
  61. *
  62. * @param map the hash map to insert element
  63. * @key the key of the element
  64. * @value the value of the element
  65. *
  66. * @return true if success, false otherwise
  67. * Note: fail if key is NULL
  68. * if duplicated, considered as success.
  69. */
  70. bool
  71. bh_hash_map_insert_with_dup(HashMap *map, void *key, void *value);
  72. /**
  73. * Find an element in the hash map
  74. *
  75. * @param map the hash map to find element
  76. * @key the key of the element
  77. *
  78. * @return the value of the found element if success, NULL otherwise
  79. */
  80. void *
  81. bh_hash_map_find(HashMap *map, void *key);
  82. /**
  83. * Update an element in the hash map with new value
  84. *
  85. * @param map the hash map to update element
  86. * @key the key of the element
  87. * @value the new value of the element
  88. * @p_old_value if not NULL, copies the old value to it
  89. *
  90. * @return true if success, false otherwise
  91. * Note: the old value won't be destroyed by value destroy function,
  92. * it will be copied to p_old_value for user to process.
  93. */
  94. bool
  95. bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value);
  96. /**
  97. * Remove an element from the hash map
  98. *
  99. * @param map the hash map to remove element
  100. * @key the key of the element
  101. * @p_old_key if not NULL, copies the old key to it
  102. * @p_old_value if not NULL, copies the old value to it
  103. *
  104. * @return true if success, false otherwise
  105. * Note: the old key and old value won't be destroyed by key destroy
  106. * function and value destroy function, they will be copied to
  107. * p_old_key and p_old_value for user to process.
  108. */
  109. bool
  110. bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
  111. void **p_old_value);
  112. /**
  113. * Destroy the hashmap
  114. *
  115. * @param map the hash map to destroy
  116. *
  117. * @return true if success, false otherwise
  118. * Note: the key destroy function and value destroy function will be
  119. * called to destroy each element's key and value if they are
  120. * not NULL.
  121. */
  122. bool
  123. bh_hash_map_destroy(HashMap *map);
  124. /**
  125. * Get the structure size of HashMap
  126. *
  127. * @param map the hash map to calculate
  128. *
  129. * @return the memory space occupied by HashMap structure
  130. */
  131. uint32
  132. bh_hash_map_get_struct_size(HashMap *hashmap);
  133. /**
  134. * Get the structure size of HashMap Element
  135. *
  136. * @return the memory space occupied by HashMapElem structure
  137. */
  138. uint32
  139. bh_hash_map_get_elem_struct_size();
  140. /**
  141. * Traverse the hash map and call the callback function
  142. *
  143. * @param map the hash map to traverse
  144. * @param callback the function to be called for every element
  145. * @param user_data the argument to be passed to the callback function
  146. *
  147. * @return true if success, false otherwise
  148. * Note: if the hash map has lock, the map will be locked during traverse,
  149. * keep the callback function as simple as possible.
  150. */
  151. bool
  152. bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
  153. void *user_data);
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif /* endof WASM_HASHMAP_H */