diff --git a/Kconfig b/Kconfig index bf2d4a01..779dded9 100644 --- a/Kconfig +++ b/Kconfig @@ -33,19 +33,6 @@ config OCRE # Enable the WASM Micro Runtime select WAMR -config OCRE_WAMR_HEAP_BUFFER_SIZE - int "WAMR heap buffer size in bytes" - default 32768 - help - A static memory allocation for WAMR to use as a heap. - -config OCRE_STORAGE_HEAP_BUFFER_SIZE - int "Storage heap buffer size in bytes" - default 500000 - depends on MEMC - help - A static memory allocation for container storage to use as a heap. - config OCRE_CONTAINER_DEFAULT_HEAP_SIZE int "Default value for the container heap size" default 4096 diff --git a/boards/b_u585i_iot02a.conf b/boards/b_u585i_iot02a.conf index 90d47674..6efe69b4 100644 --- a/boards/b_u585i_iot02a.conf +++ b/boards/b_u585i_iot02a.conf @@ -8,8 +8,6 @@ CONFIG_MEMC=y # Container defaults CONFIG_MAX_CONTAINERS=5 -CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=6388608 -CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE=2000000 # Bus interfaces CONFIG_GPIO=y diff --git a/prj.conf b/prj.conf index 519f3f84..51e4fe66 100644 --- a/prj.conf +++ b/prj.conf @@ -64,7 +64,6 @@ CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1 # Ocre configuration -CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=512000 CONFIG_OCRE_CONTAINER_DEFAULT_HEAP_SIZE=4096 CONFIG_OCRE_CONTAINER_DEFAULT_STACK_SIZE=4096 CONFIG_LOG_TRACE_SHORT_TIMESTAMP=y diff --git a/src/ocre/components/container_supervisor/cs_sm_impl.c b/src/ocre/components/container_supervisor/cs_sm_impl.c index 04ba07ae..c4addb56 100644 --- a/src/ocre/components/container_supervisor/cs_sm_impl.c +++ b/src/ocre/components/container_supervisor/cs_sm_impl.c @@ -34,15 +34,6 @@ LOG_MODULE_DECLARE(ocre_cs_component, OCRE_LOG_LEVEL); #include "cs_sm.h" #include "cs_sm_impl.h" -#include "ocre_psram.h" - -// WAMR heap buffer - uses PSRAM when available -#if defined(CONFIG_MEMC) - PSRAM_SECTION_ATTR -#endif -static char wamr_heap_buf[CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE] = {0}; - - // Thread pool for container execution #define CONTAINER_THREAD_POOL_SIZE 4 static core_thread_t container_threads[CONTAINER_THREAD_POOL_SIZE]; @@ -189,7 +180,7 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument } container_arguments->size = file_size; - container_arguments->buffer = storage_heap_alloc(file_size); + container_arguments->buffer = user_malloc(file_size); if (!container_arguments->buffer) { LOG_ERR("Failed to allocate memory for container binary from PSRAM."); return -ENOMEM; @@ -200,7 +191,7 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument ret = core_fileopen(filepath, &file_handle); if (ret < 0) { LOG_ERR("Failed to open file %s: %d", filepath, ret); - storage_heap_free(container_arguments->buffer); + user_free(container_arguments->buffer); return ret; } @@ -208,14 +199,14 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument if (ret < 0) { LOG_ERR("Failed to read file %s: %d", filepath, ret); core_fileclose(file_handle); - storage_heap_free(container_arguments->buffer); + user_free(container_arguments->buffer); return ret; } ret = core_fileclose(file_handle); if (ret < 0) { LOG_ERR("Failed to close file %s: %d", filepath, ret); - storage_heap_free(container_arguments->buffer); + user_free(container_arguments->buffer); return ret; } return 0; @@ -239,12 +230,17 @@ int CS_ctx_init(ocre_cs_ctx *ctx) { return 0; } +#if WASM_MEM_ALLOC_WITH_USER_DATA || WASM_MEM_ALLOC_WITH_USAGE +#error user allocator not compatible with WASM_MEM_ALLOC_WITH_USER_DATA or WASM_MEM_ALLOC_WITH_USAGE +#endif + ocre_container_runtime_status_t CS_runtime_init(ocre_cs_ctx *ctx, ocre_container_init_arguments_t *args) { RuntimeInitArgs init_args; memset(&init_args, 0, sizeof(RuntimeInitArgs)); - init_args.mem_alloc_type = Alloc_With_Pool; - init_args.mem_alloc_option.pool.heap_buf = wamr_heap_buf; - init_args.mem_alloc_option.pool.heap_size = sizeof(wamr_heap_buf); + init_args.mem_alloc_type = Alloc_With_Allocator; + init_args.mem_alloc_option.allocator.malloc_func = user_malloc; + init_args.mem_alloc_option.allocator.free_func = user_free; + init_args.mem_alloc_option.allocator.realloc_func = user_realloc; init_args.native_module_name = "env"; init_args.n_native_symbols = ocre_api_table_size; init_args.native_symbols = ocre_api_table; @@ -266,7 +262,6 @@ ocre_container_runtime_status_t CS_runtime_init(ocre_cs_ctx *ctx, ocre_container #ifdef CONFIG_OCRE_CONTAINER_MESSAGING ocre_messaging_init(); #endif - storage_heap_init(); return RUNTIME_STATUS_INITIALIZED; } @@ -321,7 +316,7 @@ ocre_container_status_t CS_create_container(ocre_container_t *container) { curr_container_arguments->error_buf, sizeof(curr_container_arguments->error_buf)); if (!curr_container_arguments->module) { LOG_ERR("Failed to load WASM module: %s", curr_container_arguments->error_buf); - storage_heap_free(curr_container_arguments->buffer); + user_free(curr_container_arguments->buffer); return CONTAINER_STATUS_ERROR; } @@ -380,7 +375,7 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) { LOG_ERR("Failed to instantiate WASM module: %s, for containerID= %d", curr_container_arguments->error_buf, curr_container_ID); wasm_runtime_unload(curr_container_arguments->module); - storage_heap_free(curr_container_arguments->buffer); + user_free(curr_container_arguments->buffer); return CONTAINER_STATUS_ERROR; } #if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS) || \ @@ -513,7 +508,7 @@ ocre_container_status_t CS_destroy_container(ocre_container_t *container, ocre_c } if (container->ocre_runtime_arguments.buffer) { - storage_heap_free(container->ocre_runtime_arguments.buffer); + user_free(container->ocre_runtime_arguments.buffer); container->ocre_runtime_arguments.buffer = NULL; } diff --git a/src/shared/platform/ocre_core_external.h b/src/shared/platform/ocre_core_external.h index 69375219..79efb20f 100644 --- a/src/shared/platform/ocre_core_external.h +++ b/src/shared/platform/ocre_core_external.h @@ -142,6 +142,30 @@ void *core_malloc(size_t size); */ void core_free(void *ptr); +/** + * @brief Allocate memory in application heap. + * + * @param size Number of bytes to allocate. + * @return Pointer to allocated memory, or NULL on failure. + */ +void *user_malloc(size_t size); + +/** + * @brief Free previously allocated memory in application heap. + * + * @param ptr Pointer to memory to free. + */ +void user_free(void *ptr); + +/** + * @brief Reallocate memory in application heap. + * + * @param ptr pointer to already allocated memory + * @param size Number of bytes to allocate. + * @return Pointer to reallocated memory, or NULL on failure. + */ +void *user_realloc(void *ptr, size_t size); + /** * @brief Yield the current thread's execution. */ diff --git a/src/shared/platform/ocre_psram.h b/src/shared/platform/ocre_psram.h deleted file mode 100644 index 1072b49b..00000000 --- a/src/shared/platform/ocre_psram.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef OCRE_PSRAM -#define OCRE_PSRAM - -// PSRAM configuration - centralized for different platforms -#if defined(CONFIG_MEMC) - // Board-specific PSRAM section attributes - #if defined(CONFIG_BOARD_ARDUINO_PORTENTA_H7) - #define PSRAM_SECTION_ATTR __attribute__((section("SDRAM1"), aligned(32))) - #elif defined(CONFIG_BOARD_B_U585I_IOT02A) - #define PSRAM_SECTION_ATTR __attribute__((section(".stm32_psram"), aligned(32))) - #elif defined(CONFIG_BOARD_MIMXRT1064_EVK) - #define PSRAM_SECTION_ATTR __attribute__((section("SDRAM"), aligned(32))) - #else - #define PSRAM_SECTION_ATTR __attribute__((aligned(32))) - #endif - - PSRAM_SECTION_ATTR - static char storage_heap_buf[CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE] = {0}; - - static struct k_heap storage_heap; - #define storage_heap_init() k_heap_init(&storage_heap, storage_heap_buf, CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE) - #define storage_heap_alloc(size) k_heap_alloc(&storage_heap, (size), K_SECONDS(1)) - #define storage_heap_free(buffer) k_heap_free(&storage_heap, (void*)buffer) -#else - // No PSRAM - use system malloc - #define storage_heap_init() /* No initialization needed */ - #define storage_heap_alloc(size) malloc(size) - #define storage_heap_free(buffer) free(buffer) -#endif - -#endif /* OCRE_PSRAM*/ diff --git a/src/shared/platform/posix/core_fs.c b/src/shared/platform/posix/core_fs.c index 76c2fddd..fc559b14 100644 --- a/src/shared/platform/posix/core_fs.c +++ b/src/shared/platform/posix/core_fs.c @@ -73,11 +73,11 @@ int core_filestat(const char *path, size_t *size) { } int core_fileopen(const char *path, void **handle) { - int *fd = core_malloc(sizeof(int)); + int *fd = user_malloc(sizeof(int)); if (!fd) return -ENOMEM; *fd = open(path, O_RDONLY); if (*fd < 0) { - core_free(fd); + user_free(fd); return -errno; } *handle = fd; @@ -95,7 +95,7 @@ int core_fileread(void *handle, void *buffer, size_t size) { int core_fileclose(void *handle) { int fd = *(int *)handle; int ret = close(fd); - core_free(handle); + user_free(handle); return ret; } diff --git a/src/shared/platform/posix/core_memory.c b/src/shared/platform/posix/core_memory.c index 0e13a8c0..04d0ae7d 100644 --- a/src/shared/platform/posix/core_memory.c +++ b/src/shared/platform/posix/core_memory.c @@ -16,3 +16,15 @@ void *core_malloc(size_t size) { void core_free(void *ptr) { free(ptr); } + +void *user_malloc(size_t size) { + return malloc(size); +} + +void user_free(void *ptr) { + free(ptr); +} + +void *user_realloc(void *ptr, size_t size) { + return realloc(ptr, size); +} diff --git a/src/shared/platform/zephyr/core_fs.c b/src/shared/platform/zephyr/core_fs.c index ef08a6de..42f1d614 100644 --- a/src/shared/platform/zephyr/core_fs.c +++ b/src/shared/platform/zephyr/core_fs.c @@ -47,12 +47,12 @@ int core_filestat(const char *path, size_t *size) { } int core_fileopen(const char *path, void **handle) { - struct fs_file_t *file = core_malloc(sizeof(struct fs_file_t)); + struct fs_file_t *file = user_malloc(sizeof(struct fs_file_t)); if (!file) return -ENOMEM; fs_file_t_init(file); int ret = fs_open(file, path, FS_O_READ); if (ret < 0) { - core_free(file); + user_free(file); return ret; } *handle = file; @@ -67,7 +67,7 @@ int core_fileread(void *handle, void *buffer, size_t size) { int core_fileclose(void *handle) { struct fs_file_t *file = (struct fs_file_t *)handle; int ret = fs_close(file); - core_free(file); + user_free(file); return ret; } diff --git a/src/shared/platform/zephyr/core_memory.c b/src/shared/platform/zephyr/core_memory.c index 3655ed4b..d295b32c 100644 --- a/src/shared/platform/zephyr/core_memory.c +++ b/src/shared/platform/zephyr/core_memory.c @@ -8,6 +8,7 @@ #include "ocre_core_external.h" #include +#include void *core_malloc(size_t size) { return k_malloc(size); @@ -16,3 +17,33 @@ void *core_malloc(size_t size) { void core_free(void *ptr) { k_free(ptr); } + +#ifdef CONFIG_SHARED_MULTI_HEAP +void *user_malloc(size_t size) { + return shared_multi_heap_aligned_alloc(SMH_REG_ATTR_EXTERNAL, 32, size); +} + +void user_free(void *ptr) { + shared_multi_heap_free(ptr); +} + +void *user_realloc(void *ptr, size_t size) { + // TODO + return NULL; +} + +#else +#warning CONFIG_SHARED_MULTI_HEAP is not defined. Using internal RAM +void *user_malloc(size_t size) { + return k_malloc(size); +} + +void user_free(void *ptr) { + k_free(ptr); +} + +void *user_realloc(void *ptr, size_t size) { + return k_realloc(ptr, size); +} + +#endif