Add shared heap sample (#3806)

This commit is contained in:
Wenyong Huang
2024-09-20 16:13:20 +08:00
committed by GitHub
parent 1fd422ae6e
commit c4aa1deda5
11 changed files with 548 additions and 2 deletions

View File

@ -0,0 +1,43 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.14)
project(wasm-apps)
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
if (APPLE)
set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
set (CMAKE_C_LINK_FLAGS "")
set (CMAKE_CXX_LINK_FLAGS "")
endif ()
set (CMAKE_SYSTEM_PROCESSOR wasm32)
set (CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
if (NOT DEFINED WASI_SDK_DIR)
set (WASI_SDK_DIR "/opt/wasi-sdk")
endif ()
set (CMAKE_C_FLAGS "-nostdlib -Qunused-arguments -z stack-size=32768")
set (CMAKE_C_COMPILER_TARGET "wasm32")
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, \
-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=print_buf \
-Wl,--allow-undefined"
)
add_executable(test1.wasm test1.c)
target_link_libraries(test1.wasm)
add_executable(test2.wasm test2.c)
target_link_libraries(test2.wasm)

View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdint.h>
extern void *
shared_malloc(uint32_t size);
extern void
shared_free(void *ptr);
void *
my_shared_malloc(uint32_t size, uint32_t index)
{
char *buf = shared_malloc(size);
if (buf)
snprintf(buf, 1024, "Hello, this is buf %u allocated from shared heap",
index);
return buf;
}
void
my_shared_free(void *ptr)
{
shared_free(ptr);
}

View File

@ -0,0 +1,18 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdio.h>
extern void
shared_free(void *ptr);
void
print_buf(char *buf)
{
printf("wasm app2's wasm func received buf: %s\n\n", buf);
shared_free(buf);
}