Shared heap enhancements for Interpreter and AOT (#4400)

Propose two enhancements:

- Shared heap created from preallocated memory buffer: The user can create a shared heap from a pre-allocated buffer and see that memory region as one large chunk; there's no need to dynamically manage it(malloc/free). The user needs to make sure the native address and size of that memory region are valid.
- Introduce shared heap chain: The user can create a shared heap chain, from the wasm app point of view, it's still a continuous memory region in wasm app's point of view while in the native it can consist of multiple shared heaps (each of which is a continuous memory region). For example, one 500MB shared heap 1 and one 500 MB shared heap 2 form a chain, in Wasm's point of view, it's one 1GB shared heap.

After these enhancements, the data sharing between wasm apps, and between hosts can be more efficient and flexible. Admittedly shared heap management can be more complex for users, but it's similar to the zero-overhead principle. No overhead will be imposed for the users who don't use the shared heap enhancement or don't use the shared heap at all.
This commit is contained in:
TianlongLiang
2025-07-04 10:44:51 +08:00
committed by GitHub
parent ee056d8076
commit 8a55a1e7a1
35 changed files with 2792 additions and 657 deletions

View File

@ -30,9 +30,7 @@ set (CMAKE_EXE_LINKER_FLAGS
-Wl,--no-entry,--strip-all, \
-Wl,--export=__heap_base,--export=__data_end \
-Wl,--export=__wasm_call_ctors \
-Wl,--export=my_shared_heap_malloc \
-Wl,--export=my_shared_heap_free \
-Wl,--export=print_buf \
-Wl,--export-all \
-Wl,--allow-undefined"
)

View File

@ -58,3 +58,14 @@ my_shared_heap_free(void *ptr)
{
shared_heap_free(ptr);
}
void *
produce_str(char *addr, uint32_t index)
{
char c;
snprintf(addr, 512, "Data: %u stores to pre-allocated shared heap", index);
/* Actually access it in wasm */
c = addr[0];
printf("In WASM: the first char is %c\n", c);
return addr;
}

View File

@ -4,8 +4,7 @@
*/
#include <stdio.h>
#include <stdio.h>
#include <stdint.h>
extern void
shared_heap_free(void *ptr);
@ -16,3 +15,14 @@ print_buf(char *buf)
printf("wasm app2's wasm func received buf: %s\n\n", buf);
shared_heap_free(buf);
}
void
consume_str(char *buf)
{
/* Actually access it in wasm */
char c = buf[0];
printf("In WASM: wasm app2's wasm func received buf in pre-allocated "
"shared buf: "
"%s with its first char is %c\n\n",
buf, c);
}