multi_module_utils.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_read_file.h"
  6. #include "wasm_export.h"
  7. static char *
  8. build_module_path(const char *module_name)
  9. {
  10. const char *module_search_path = ".";
  11. const char *format = "%s/%s.wasm";
  12. int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
  13. + strlen(".wasm") + 1;
  14. char *wasm_file_name = BH_MALLOC(sz);
  15. if (!wasm_file_name) {
  16. return NULL;
  17. }
  18. snprintf(wasm_file_name, sz, format, module_search_path, module_name);
  19. return wasm_file_name;
  20. }
  21. bool
  22. reader(const char *module_name, uint8 **p_buffer, uint32 *p_size)
  23. {
  24. char *wasm_file_path = build_module_path(module_name);
  25. if (!wasm_file_path) {
  26. return false;
  27. }
  28. *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_path, p_size);
  29. BH_FREE(wasm_file_path);
  30. return *p_buffer != NULL;
  31. }
  32. void
  33. destroyer(uint8 *buffer, uint32 size)
  34. {
  35. if (!buffer) {
  36. return;
  37. }
  38. BH_FREE(buffer);
  39. buffer = NULL;
  40. }