Implement shared heap for AOT (#3815)

This commit is contained in:
Wenyong Huang
2024-09-29 12:50:59 +08:00
committed by GitHub
parent c4aa1deda5
commit 9ba36e284c
29 changed files with 684 additions and 81 deletions

View File

@ -26,12 +26,12 @@ set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (DEFINED_SYMBOLS "${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
set (CMAKE_EXE_LINKER_FLAGS
"-Wl,--initial-memory=65536, \
"-O0 -Wl,--initial-memory=65536, \
-Wl,--no-entry,--strip-all, \
-Wl,--export=__heap_base,--export=__data_end \
-Wl,--export=__wasm_call_ctors \
-Wl,--export=my_shared_malloc \
-Wl,--export=my_shared_free \
-Wl,--export=my_shared_heap_malloc \
-Wl,--export=my_shared_heap_free \
-Wl,--export=print_buf \
-Wl,--allow-undefined"
)

View File

@ -5,26 +5,56 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
extern void *
shared_malloc(uint32_t size);
shared_heap_malloc(uint32_t size);
extern void
shared_free(void *ptr);
shared_heap_free(void *ptr);
void *
my_shared_malloc(uint32_t size, uint32_t index)
my_shared_heap_malloc(uint32_t size, uint32_t index)
{
char *buf = shared_malloc(size);
char *buf1 = NULL, *buf2 = NULL, *buf;
if (buf)
snprintf(buf, 1024, "Hello, this is buf %u allocated from shared heap",
index);
buf1 = shared_heap_malloc(128);
if (!buf1)
return NULL;
buf1[0] = 'H';
buf1[1] = 'e';
buf1[2] = 'l';
buf1[3] = 'l';
buf1[4] = 'o';
buf1[5] = ',';
buf1[6] = ' ';
buf2 = shared_heap_malloc(128);
if (!buf2) {
shared_heap_free(buf1);
return NULL;
}
snprintf(buf2, 128, "this is buf %u allocated from shared heap", index);
buf = shared_heap_malloc(size);
if (!buf) {
shared_heap_free(buf1);
shared_heap_free(buf2);
return NULL;
}
memset(buf, 0, size);
memcpy(buf, buf1, strlen(buf1));
memcpy(buf + strlen(buf1), buf2, strlen(buf2));
shared_heap_free(buf1);
shared_heap_free(buf2);
return buf;
}
void
my_shared_free(void *ptr)
my_shared_heap_free(void *ptr)
{
shared_free(ptr);
shared_heap_free(ptr);
}

View File

@ -8,11 +8,11 @@
#include <stdio.h>
extern void
shared_free(void *ptr);
shared_heap_free(void *ptr);
void
print_buf(char *buf)
{
printf("wasm app2's wasm func received buf: %s\n\n", buf);
shared_free(buf);
shared_heap_free(buf);
}