mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-10 05:36:15 +00:00

* Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
293 lines
6.5 KiB
C
293 lines
6.5 KiB
C
/*
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
|
|
#include "bh_hashmap.h"
|
|
#include "bh_log.h"
|
|
#include "bh_thread.h"
|
|
#include "bh_memory.h"
|
|
|
|
|
|
typedef struct HashMapElem {
|
|
void *key;
|
|
void *value;
|
|
struct HashMapElem *next;
|
|
} HashMapElem;
|
|
|
|
struct HashMap {
|
|
/* size of element array */
|
|
uint32 size;
|
|
/* lock for elements */
|
|
korp_mutex *lock;
|
|
/* hash function of key */
|
|
HashFunc hash_func;
|
|
/* key equal function */
|
|
KeyEqualFunc key_equal_func;
|
|
KeyDestroyFunc key_destroy_func;
|
|
ValueDestroyFunc value_destroy_func;
|
|
HashMapElem *elements[1];
|
|
};
|
|
|
|
HashMap*
|
|
bh_hash_map_create(uint32 size, bool use_lock,
|
|
HashFunc hash_func,
|
|
KeyEqualFunc key_equal_func,
|
|
KeyDestroyFunc key_destroy_func,
|
|
ValueDestroyFunc value_destroy_func)
|
|
{
|
|
HashMap *map;
|
|
uint64 total_size;
|
|
|
|
if (size > HASH_MAP_MAX_SIZE) {
|
|
LOG_ERROR("HashMap create failed: size is too large.\n");
|
|
return NULL;
|
|
}
|
|
|
|
if (!hash_func || !key_equal_func) {
|
|
LOG_ERROR("HashMap create failed: hash function or key equal function "
|
|
" is NULL.\n");
|
|
return NULL;
|
|
}
|
|
|
|
total_size = offsetof(HashMap, elements) +
|
|
sizeof(HashMapElem) * (uint64)size +
|
|
(use_lock ? sizeof(korp_mutex) : 0);
|
|
|
|
if (total_size >= UINT32_MAX
|
|
|| !(map = bh_malloc((uint32)total_size))) {
|
|
LOG_ERROR("HashMap create failed: alloc memory failed.\n");
|
|
return NULL;
|
|
}
|
|
|
|
memset(map, 0, (uint32)total_size);
|
|
|
|
if (use_lock) {
|
|
map->lock = (korp_mutex*)
|
|
((uint8*)map + offsetof(HashMap, elements)
|
|
+ sizeof(HashMapElem) * size);
|
|
if (vm_mutex_init(map->lock)) {
|
|
LOG_ERROR("HashMap create failed: init map lock failed.\n");
|
|
bh_free(map);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
map->size = size;
|
|
map->hash_func = hash_func;
|
|
map->key_equal_func = key_equal_func;
|
|
map->key_destroy_func = key_destroy_func;
|
|
map->value_destroy_func = value_destroy_func;
|
|
return map;
|
|
}
|
|
|
|
bool
|
|
bh_hash_map_insert(HashMap *map, void *key, void *value)
|
|
{
|
|
uint32 index;
|
|
HashMapElem *elem;
|
|
|
|
if (!map || !key) {
|
|
LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
|
|
return false;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_lock(map->lock);
|
|
}
|
|
|
|
index = map->hash_func(key) % map->size;
|
|
elem = map->elements[index];
|
|
while (elem) {
|
|
if (map->key_equal_func(elem->key, key)) {
|
|
LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
|
|
goto fail;
|
|
}
|
|
elem = elem->next;
|
|
}
|
|
|
|
if (!(elem = bh_malloc(sizeof(HashMapElem)))) {
|
|
LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
|
|
goto fail;
|
|
}
|
|
|
|
elem->key = key;
|
|
elem->value = value;
|
|
elem->next = map->elements[index];
|
|
map->elements[index] = elem;
|
|
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return true;
|
|
|
|
fail:
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void*
|
|
bh_hash_map_find(HashMap *map, void *key)
|
|
{
|
|
uint32 index;
|
|
HashMapElem *elem;
|
|
void *value;
|
|
|
|
if (!map || !key) {
|
|
LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
|
|
return NULL;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_lock(map->lock);
|
|
}
|
|
|
|
index = map->hash_func(key) % map->size;
|
|
elem = map->elements[index];
|
|
|
|
while (elem) {
|
|
if (map->key_equal_func(elem->key, key)) {
|
|
value = elem->value;
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return value;
|
|
}
|
|
elem = elem->next;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
bool
|
|
bh_hash_map_update(HashMap *map, void *key, void *value,
|
|
void **p_old_value)
|
|
{
|
|
uint32 index;
|
|
HashMapElem *elem;
|
|
|
|
if (!map || !key) {
|
|
LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
|
|
return false;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_lock(map->lock);
|
|
}
|
|
|
|
index = map->hash_func(key) % map->size;
|
|
elem = map->elements[index];
|
|
|
|
while (elem) {
|
|
if (map->key_equal_func(elem->key, key)) {
|
|
if (p_old_value)
|
|
*p_old_value = elem->value;
|
|
elem->value = value;
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return true;
|
|
}
|
|
elem = elem->next;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
bh_hash_map_remove(HashMap *map, void *key,
|
|
void **p_old_key, void **p_old_value)
|
|
{
|
|
uint32 index;
|
|
HashMapElem *elem, *prev;
|
|
|
|
if (!map || !key) {
|
|
LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
|
|
return false;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_lock(map->lock);
|
|
}
|
|
|
|
index = map->hash_func(key) % map->size;
|
|
prev = elem = map->elements[index];
|
|
|
|
while (elem) {
|
|
if (map->key_equal_func(elem->key, key)) {
|
|
if (p_old_key)
|
|
*p_old_key = elem->key;
|
|
if (p_old_value)
|
|
*p_old_value = elem->value;
|
|
|
|
if (elem == map->elements[index])
|
|
map->elements[index] = elem->next;
|
|
else
|
|
prev->next = elem->next;
|
|
|
|
bh_free(elem);
|
|
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
prev = elem;
|
|
elem = elem->next;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
bh_hash_map_destroy(HashMap *map)
|
|
{
|
|
uint32 index;
|
|
HashMapElem *elem, *next;
|
|
|
|
if (!map) {
|
|
LOG_ERROR("HashMap destroy failed: map is NULL.\n");
|
|
return false;
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_lock(map->lock);
|
|
}
|
|
|
|
for (index = 0; index < map->size; index++) {
|
|
elem = map->elements[index];
|
|
while (elem) {
|
|
next = elem->next;
|
|
|
|
if (map->key_destroy_func) {
|
|
map->key_destroy_func(elem->key);
|
|
}
|
|
if (map->value_destroy_func) {
|
|
map->value_destroy_func(elem->value);
|
|
}
|
|
bh_free(elem);
|
|
|
|
elem = next;
|
|
}
|
|
}
|
|
|
|
if (map->lock) {
|
|
vm_mutex_unlock(map->lock);
|
|
vm_mutex_destroy(map->lock);
|
|
}
|
|
bh_free(map);
|
|
return true;
|
|
}
|