wasm_exec_env.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_exec_env.h"
  6. #include "wasm_runtime_common.h"
  7. WASMExecEnv *
  8. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  9. uint32 stack_size)
  10. {
  11. uint64 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  12. + (uint64)stack_size;
  13. WASMExecEnv *exec_env;
  14. if (total_size >= UINT32_MAX
  15. || !(exec_env = wasm_runtime_malloc((uint32)total_size)))
  16. return NULL;
  17. memset(exec_env, 0, (uint32)total_size);
  18. #if WASM_ENABLE_AOT != 0
  19. if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {
  20. wasm_runtime_free(exec_env);
  21. return NULL;
  22. }
  23. #endif
  24. exec_env->module_inst = module_inst;
  25. exec_env->wasm_stack_size = stack_size;
  26. exec_env->wasm_stack.s.top_boundary =
  27. exec_env->wasm_stack.s.bottom + stack_size;
  28. exec_env->wasm_stack.s.top = exec_env->wasm_stack.s.bottom;
  29. return exec_env;
  30. }
  31. void
  32. wasm_exec_env_destroy(WASMExecEnv *exec_env)
  33. {
  34. #if WASM_ENABLE_AOT != 0
  35. wasm_runtime_free(exec_env->argv_buf);
  36. #endif
  37. wasm_runtime_free(exec_env);
  38. }
  39. WASMModuleInstanceCommon *
  40. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env)
  41. {
  42. return exec_env->module_inst;
  43. }
  44. void
  45. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
  46. {
  47. exec_env->handle = os_self_thread();
  48. exec_env->native_stack_boundary = os_thread_get_stack_boundary()
  49. + RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY;
  50. }