bh_hashmap.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. for each key when the hash map is destroyed. */
  21. typedef void (*KeyDestroyFunc)(void *key);
  22. /* Value destroy function: to destroy the value, auto called
  23. for each value when the hash map is destroyed. */
  24. typedef void (*ValueDestroyFunc)(void *value);
  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 for each key if not NULL
  37. * when the hash map is destroyed
  38. * @param value_destroy_func value destroy function, called for each value if
  39. * not NULL when the hash map is destroyed
  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. * Find an element in the hash map
  61. *
  62. * @param map the hash map to find element
  63. * @key the key of the element
  64. *
  65. * @return the value of the found element if success, NULL otherwise
  66. */
  67. void *
  68. bh_hash_map_find(HashMap *map, void *key);
  69. /**
  70. * Update an element in the hash map with new value
  71. *
  72. * @param map the hash map to update element
  73. * @key the key of the element
  74. * @value the new value of the element
  75. * @p_old_value if not NULL, copies the old value to it
  76. *
  77. * @return true if success, false otherwise
  78. * Note: the old value won't be destroyed by value destroy function,
  79. * it will be copied to p_old_value for user to process.
  80. */
  81. bool
  82. bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value);
  83. /**
  84. * Remove an element from the hash map
  85. *
  86. * @param map the hash map to remove element
  87. * @key the key of the element
  88. * @p_old_key if not NULL, copies the old key to it
  89. * @p_old_value if not NULL, copies the old value to it
  90. *
  91. * @return true if success, false otherwise
  92. * Note: the old key and old value won't be destroyed by key destroy
  93. * function and value destroy function, they will be copied to
  94. * p_old_key and p_old_value for user to process.
  95. */
  96. bool
  97. bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
  98. void **p_old_value);
  99. /**
  100. * Destroy the hashmap
  101. *
  102. * @param map the hash map to destroy
  103. *
  104. * @return true if success, false otherwise
  105. * Note: the key destroy function and value destroy function will be
  106. * called to destroy each element's key and value if they are
  107. * not NULL.
  108. */
  109. bool
  110. bh_hash_map_destroy(HashMap *map);
  111. /**
  112. * Get the structure size of HashMap
  113. *
  114. * @param map the hash map to calculate
  115. *
  116. * @return the memory space occupied by HashMap structure
  117. */
  118. uint32
  119. bh_hash_map_get_struct_size(HashMap *hashmap);
  120. /**
  121. * Get the structure size of HashMap Element
  122. *
  123. * @return the memory space occupied by HashMapElem structure
  124. */
  125. uint32
  126. bh_hash_map_get_elem_struct_size();
  127. /**
  128. * Traverse the hash map and call the callback function
  129. *
  130. * @param map the hash map to traverse
  131. * @param callback the function to be called for every element
  132. * @param user_data the argument to be passed to the callback function
  133. *
  134. * @return true if success, false otherwise
  135. * Note: if the hash map has lock, the map will be locked during traverse,
  136. * keep the callback function as simple as possible.
  137. */
  138. bool
  139. bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
  140. void *user_data);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif /* endof WASM_HASHMAP_H */