wasm_hashmap.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef WASM_HASHMAP_H
  17. #define WASM_HASHMAP_H
  18. #include "bh_platform.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* Maximum initial size of hash map */
  23. #define HASH_MAP_MAX_SIZE 65536
  24. struct HashMap;
  25. typedef struct HashMap HashMap;
  26. /* Hash function: to get the hash value of key. */
  27. typedef uint32 (*HashFunc)(const void *key);
  28. /* Key equal function: to check whether two keys are equal. */
  29. typedef bool (*KeyEqualFunc)(void *key1, void *key2);
  30. /* Key destroy function: to destroy the key, auto called
  31. when an hash element is removed. */
  32. typedef void (*KeyDestroyFunc)(void *key);
  33. /* Value destroy function: to destroy the value, auto called
  34. when an hash element is removed. */
  35. typedef void (*ValueDestroyFunc)(void *key);
  36. /**
  37. * Create a hash map.
  38. *
  39. * @param size: the initial size of the hash map
  40. * @param use_lock whether to lock the hash map when operating on it
  41. * @param hash_func hash function of the key, must be specified
  42. * @param key_equal_func key equal function, check whether two keys
  43. * are equal, must be specified
  44. * @param key_destroy_func key destroy function, called when an hash element
  45. * is removed if it is not NULL
  46. * @param value_destroy_func value destroy function, called when an hash
  47. * element is removed if it is not NULL
  48. *
  49. * @return the hash map created, NULL if failed
  50. */
  51. HashMap*
  52. wasm_hash_map_create(uint32 size, bool use_lock,
  53. HashFunc hash_func,
  54. KeyEqualFunc key_equal_func,
  55. KeyDestroyFunc key_destroy_func,
  56. ValueDestroyFunc value_destroy_func);
  57. /**
  58. * Insert an element to the hash map
  59. *
  60. * @param map the hash map to insert element
  61. * @key the key of the element
  62. * @value the value of the element
  63. *
  64. * @return true if success, false otherwise
  65. * Note: fail if key is NULL or duplicated key exists in the hash map,
  66. */
  67. bool
  68. wasm_hash_map_insert(HashMap *map, void *key, void *value);
  69. /**
  70. * Find an element in the hash map
  71. *
  72. * @param map the hash map to find element
  73. * @key the key of the element
  74. *
  75. * @return the value of the found element if success, NULL otherwise
  76. */
  77. void*
  78. wasm_hash_map_find(HashMap *map, void *key);
  79. /**
  80. * Update an element in the hash map with new value
  81. *
  82. * @param map the hash map to update element
  83. * @key the key of the element
  84. * @value the new value of the element
  85. * @p_old_value if not NULL, copies the old value to it
  86. *
  87. * @return true if success, false otherwise
  88. * Note: the old value won't be destroyed by value destory function,
  89. * it will be copied to p_old_value for user to process.
  90. */
  91. bool
  92. wasm_hash_map_update(HashMap *map, void *key, void *value,
  93. void **p_old_value);
  94. /**
  95. * Remove an element from the hash map
  96. *
  97. * @param map the hash map to remove element
  98. * @key the key of the element
  99. * @p_old_key if not NULL, copies the old key to it
  100. * @p_old_value if not NULL, copies the old value to it
  101. *
  102. * @return true if success, false otherwise
  103. * Note: the old key and old value won't be destroyed by key destroy
  104. * function and value destroy function, they will be copied to
  105. * p_old_key and p_old_value for user to process.
  106. */
  107. bool
  108. wasm_hash_map_remove(HashMap *map, void *key,
  109. void **p_old_key, void **p_old_value);
  110. /**
  111. * Destroy the hashmap
  112. *
  113. * @param map the hash map to destroy
  114. *
  115. * @return true if success, false otherwise
  116. * Note: the key destroy function and value destroy function will be
  117. * called to destroy each element's key and value if they are
  118. * not NULL.
  119. */
  120. bool
  121. wasm_hash_map_destroy(HashMap *map);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif /* endof WASM_HASHMAP_H */