User defined memory allocator for different purposes (#3316)

Some issues are related with memory fragmentation, which may cause
the linear memory cannot be allocated. In WAMR, the memory managed
by the system is often trivial, but linear memory usually directly allocates
a large block and often remains unchanged for a long time. Their sensitivity
and contribution to fragmentation are different, which is suitable for
different allocation strategies. If we can control the linear memory's allocation,
do not make it from system heap, the overhead of heap management might
be avoided.

Add `mem_alloc_usage_t usage` as the first argument for user defined
malloc/realloc/free functions when `WAMR_BUILD_ALLOC_WITH_USAGE` cmake
variable is set as 1, and make passing `Alloc_For_LinearMemory` to the
argument when allocating the linear memory.
This commit is contained in:
dongsheng28849455
2024-04-18 19:40:57 +08:00
committed by GitHub
parent 68bd30c6f9
commit ba59e56e19
8 changed files with 131 additions and 20 deletions

View File

@ -113,6 +113,11 @@ typedef enum {
Alloc_With_System_Allocator,
} mem_alloc_type_t;
typedef enum {
Alloc_For_Runtime,
Alloc_For_LinearMemory
} mem_alloc_usage_t;
/* Memory allocator option */
typedef union MemAllocOption {
struct {
@ -120,6 +125,9 @@ typedef union MemAllocOption {
uint32_t heap_size;
} pool;
struct {
/* the function signature is varied when
WASM_MEM_ALLOC_WITH_USER_DATA and
WASM_MEM_ALLOC_WITH_USAGE are defined */
void *malloc_func;
void *realloc_func;
void *free_func;