bh_hashmap.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  26. * Create a hash map.
  27. *
  28. * @param size: the initial size of the hash map
  29. * @param use_lock whether to lock the hash map when operating on it
  30. * @param hash_func hash function of the key, must be specified
  31. * @param key_equal_func key equal function, check whether two keys
  32. * are equal, must be specified
  33. * @param key_destroy_func key destroy function, called when an hash element
  34. * is removed if it is not NULL
  35. * @param value_destroy_func value destroy function, called when an hash
  36. * element is removed if it is not NULL
  37. *
  38. * @return the hash map created, NULL if failed
  39. */
  40. HashMap*
  41. bh_hash_map_create(uint32 size, bool use_lock,
  42. HashFunc hash_func,
  43. KeyEqualFunc key_equal_func,
  44. KeyDestroyFunc key_destroy_func,
  45. ValueDestroyFunc value_destroy_func);
  46. /**
  47. * Insert an element to the hash map
  48. *
  49. * @param map the hash map to insert element
  50. * @key the key of the element
  51. * @value the value of the element
  52. *
  53. * @return true if success, false otherwise
  54. * Note: fail if key is NULL or duplicated key exists in the hash map,
  55. */
  56. bool
  57. bh_hash_map_insert(HashMap *map, void *key, void *value);
  58. /**
  59. * Find an element in the hash map
  60. *
  61. * @param map the hash map to find element
  62. * @key the key of the element
  63. *
  64. * @return the value of the found element if success, NULL otherwise
  65. */
  66. void*
  67. bh_hash_map_find(HashMap *map, void *key);
  68. /**
  69. * Update an element in the hash map with new value
  70. *
  71. * @param map the hash map to update element
  72. * @key the key of the element
  73. * @value the new value of the element
  74. * @p_old_value if not NULL, copies the old value to it
  75. *
  76. * @return true if success, false otherwise
  77. * Note: the old value won't be destroyed by value destroy function,
  78. * it will be copied to p_old_value for user to process.
  79. */
  80. bool
  81. bh_hash_map_update(HashMap *map, void *key, void *value,
  82. 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,
  98. void **p_old_key, 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. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif /* endof WASM_HASHMAP_H */