shared_heap_wrapper.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_common.h"
  6. #include "bh_log.h"
  7. #include "wasm_export.h"
  8. #include "../interpreter/wasm.h"
  9. #include "../common/wasm_runtime_common.h"
  10. /* clang-format off */
  11. #define validate_native_addr(addr, size) \
  12. wasm_runtime_validate_native_addr(module_inst, addr, size)
  13. #define module_shared_malloc(size, p_native_addr) \
  14. wasm_runtime_shared_heap_malloc(module_inst, size, p_native_addr)
  15. #define module_shared_free(offset) \
  16. wasm_runtime_shared_heap_free(module_inst, offset)
  17. /* clang-format on */
  18. static uint32
  19. shared_malloc_wrapper(wasm_exec_env_t exec_env, uint32 size)
  20. {
  21. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  22. return (uint32)module_shared_malloc((uint64)size, NULL);
  23. }
  24. static void
  25. shared_free_wrapper(wasm_exec_env_t exec_env, void *ptr)
  26. {
  27. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  28. if (!validate_native_addr(ptr, (uint64)sizeof(uintptr_t))) {
  29. LOG_WARNING("Invalid app address");
  30. return;
  31. }
  32. module_shared_free(addr_native_to_app(ptr));
  33. }
  34. /* clang-format off */
  35. #define REG_NATIVE_FUNC(func_name, signature) \
  36. { #func_name, func_name##_wrapper, signature, NULL }
  37. /* clang-format on */
  38. static NativeSymbol native_symbols_shared_heap[] = {
  39. REG_NATIVE_FUNC(shared_malloc, "(i)i"),
  40. REG_NATIVE_FUNC(shared_free, "(*)"),
  41. };
  42. uint32
  43. get_lib_shared_heap_export_apis(NativeSymbol **p_shared_heap_apis)
  44. {
  45. *p_shared_heap_apis = native_symbols_shared_heap;
  46. return sizeof(native_symbols_shared_heap) / sizeof(NativeSymbol);
  47. }