mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-07-11 15:03:33 +00:00

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.
47 lines
740 B
C
47 lines
740 B
C
/*
|
|
* Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
extern void *
|
|
shared_heap_malloc(int size);
|
|
extern void
|
|
shared_heap_free(void *offset);
|
|
extern void *
|
|
test_addr_conv(void *ptr);
|
|
|
|
int
|
|
test()
|
|
{
|
|
int *ptr = NULL;
|
|
int *ptr2 = NULL;
|
|
|
|
ptr = (int *)shared_heap_malloc(4);
|
|
|
|
if (ptr == NULL) {
|
|
return 0;
|
|
}
|
|
ptr2 = test_addr_conv(ptr);
|
|
if (ptr2 != ptr) {
|
|
return 0;
|
|
}
|
|
shared_heap_free(ptr);
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
test_preallocated(void *app_addr)
|
|
{
|
|
int *ptr = (int *)app_addr;
|
|
int *ptr2 = NULL;
|
|
|
|
ptr2 = test_addr_conv(ptr);
|
|
if (ptr2 != ptr) {
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|