Allow configuring which memory for pool/usage allocators is placed where
This commit is contained in:
@ -36,19 +36,35 @@ void host_print(wasm_exec_env_t exec_env, const char *msg) {
|
||||
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE
|
||||
|
||||
// Bump allocator
|
||||
// Another bump allocator
|
||||
#define WASM_LINEAR_MEMORY_SIZE (64 * 1024) // Need to match --initial-memory?
|
||||
#define ALLOCATOR_POOL_SIZE (WASM_LINEAR_MEMORY_SIZE + 2 * 1024 * 1024)
|
||||
#define RUNTIME_POOL_SIZE (2 * 1024 * 1024)
|
||||
#define LINEAR_POOL_SIZE (WASM_LINEAR_MEMORY_SIZE + 512 * 1024)
|
||||
#define ALIGNMENT 8
|
||||
|
||||
static char allocator_pool[ALLOCATOR_POOL_SIZE];
|
||||
static size_t pool_offset = 0;
|
||||
typedef struct {
|
||||
char *buf;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
} BumpPool;
|
||||
|
||||
#if WAMR_RUNTIME_POOL_IN_TEXT
|
||||
__attribute__((section(".text.wamr_runtime_pool"), aligned(4096)))
|
||||
#endif
|
||||
static char runtime_pool_buf[RUNTIME_POOL_SIZE];
|
||||
|
||||
#if WAMR_LINEAR_POOL_IN_TEXT
|
||||
__attribute__((section(".text.wamr_linear_pool"), aligned(4096)))
|
||||
#endif
|
||||
static char linear_pool_buf[LINEAR_POOL_SIZE];
|
||||
|
||||
// mem_alloc_usage_t: 0 = Alloc_For_Runtime, 1 = Alloc_For_LinearMemory
|
||||
static BumpPool pools[] = {
|
||||
{ runtime_pool_buf, RUNTIME_POOL_SIZE, 0 },
|
||||
{ linear_pool_buf, LINEAR_POOL_SIZE, 0 },
|
||||
};
|
||||
|
||||
static size_t align_up(size_t x, size_t a) {
|
||||
// a is a power of two, e.g., 8:
|
||||
// x+a-1: Shift value upwards to the next alignment "bucket"
|
||||
// a-1: 00000111, ~(a-1): 11111000 - Clears the lower bits/alignment
|
||||
// remainder
|
||||
return (x + a - 1) & ~(a - 1);
|
||||
}
|
||||
|
||||
@ -57,26 +73,25 @@ static size_t alloc_size(void *ptr) {
|
||||
return *(size_t *)((char *)ptr - header_size);
|
||||
}
|
||||
|
||||
static void *bump_alloc(unsigned int size) {
|
||||
// Reserve space for a size_t header before the user pointer
|
||||
static void *bump_alloc(BumpPool *pool, unsigned int size) {
|
||||
size_t header_size = align_up(sizeof(size_t), ALIGNMENT);
|
||||
size_t start = align_up(pool_offset, ALIGNMENT);
|
||||
size_t start = align_up(pool->offset, ALIGNMENT);
|
||||
size_t end = start + header_size + align_up(size, ALIGNMENT);
|
||||
|
||||
if (end > ALLOCATOR_POOL_SIZE) {
|
||||
if (end > pool->size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*(size_t *)&allocator_pool[start] = size;
|
||||
void *ptr = &allocator_pool[start + header_size];
|
||||
pool_offset = end;
|
||||
*(size_t *)&pool->buf[start] = size;
|
||||
void *ptr = &pool->buf[start + header_size];
|
||||
pool->offset = end;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *wamr_malloc(mem_alloc_usage_t usage, unsigned int size) {
|
||||
PRINT("wamr_malloc: usage=%d size=%u\n", usage, size);
|
||||
return bump_alloc(size);
|
||||
return bump_alloc(&pools[usage], size);
|
||||
}
|
||||
|
||||
void *wamr_realloc(mem_alloc_usage_t usage, bool full_size_mmaped, void *ptr,
|
||||
@ -84,7 +99,7 @@ void *wamr_realloc(mem_alloc_usage_t usage, bool full_size_mmaped, void *ptr,
|
||||
PRINT("wamr_realloc: usage=%d full_size_mmaped=%d ptr=%p new_size=%u\n",
|
||||
usage, full_size_mmaped, ptr, new_size);
|
||||
|
||||
void *new_addr = bump_alloc(new_size);
|
||||
void *new_addr = bump_alloc(&pools[usage], new_size);
|
||||
if (!new_addr) {
|
||||
return NULL;
|
||||
}
|
||||
@ -98,13 +113,16 @@ void *wamr_realloc(mem_alloc_usage_t usage, bool full_size_mmaped, void *ptr,
|
||||
}
|
||||
|
||||
void wamr_free(mem_alloc_usage_t usage, void *ptr) {
|
||||
PRINT("wamr_free: usage=%d ptr=%p\n", usage, ptr);
|
||||
// PRINT("wamr_free: usage=%d ptr=%p\n", usage, ptr);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define RUNTIME_POOL_SIZE (4 * STACK_SIZE)
|
||||
|
||||
#if WAMR_GLOBAL_HEAP_IN_TEXT
|
||||
__attribute__((section(".text.wamr_global_heap"), aligned(4096)))
|
||||
#endif
|
||||
static char global_heap_buf[RUNTIME_POOL_SIZE];
|
||||
|
||||
#endif // WASM_MEM_ALLOC_WITH_USAGE
|
||||
|
||||
Reference in New Issue
Block a user