bh_hashmap.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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,
  45. HashFunc hash_func,
  46. KeyEqualFunc key_equal_func,
  47. 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,
  85. void **p_old_value);
  86. /**
  87. * Remove an element from the hash map
  88. *
  89. * @param map the hash map to remove element
  90. * @key the key of the element
  91. * @p_old_key if not NULL, copies the old key to it
  92. * @p_old_value if not NULL, copies the old value to it
  93. *
  94. * @return true if success, false otherwise
  95. * Note: the old key and old value won't be destroyed by key destroy
  96. * function and value destroy function, they will be copied to
  97. * p_old_key and p_old_value for user to process.
  98. */
  99. bool
  100. bh_hash_map_remove(HashMap *map, void *key,
  101. void **p_old_key, void **p_old_value);
  102. /**
  103. * Destroy the hashmap
  104. *
  105. * @param map the hash map to destroy
  106. *
  107. * @return true if success, false otherwise
  108. * Note: the key destroy function and value destroy function will be
  109. * called to destroy each element's key and value if they are
  110. * not NULL.
  111. */
  112. bool
  113. bh_hash_map_destroy(HashMap *map);
  114. /**
  115. * Get the structure size of HashMap
  116. *
  117. * @param map the hash map to calculate
  118. *
  119. * @return the memory space occupied by HashMap structure
  120. */
  121. uint32
  122. bh_hash_map_get_struct_size(HashMap *hashmap);
  123. /**
  124. * Get the structure size of HashMap Element
  125. *
  126. * @return the memory space occupied by HashMapElem structure
  127. */
  128. uint32
  129. bh_hash_map_get_elem_struct_size();
  130. /**
  131. * Traverse the hash map and call the callback function
  132. *
  133. * @param map the hash map to traverse
  134. * @param callback the function to be called for every element
  135. * @param user_data the argument to be passed to the callback function
  136. *
  137. * @return true if success, false otherwise
  138. * Note: if the hash map has lock, the map will be locked during traverse,
  139. * keep the callback function as simple as possible.
  140. */
  141. bool
  142. bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
  143. void *user_data);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif /* endof WASM_HASHMAP_H */