bh_hashmap.h 4.8 KB

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