Skip to content

Commit dbe6303

Browse files
committed
Added requested changes
1 parent 2b4d5d7 commit dbe6303

File tree

7 files changed

+108
-38
lines changed

7 files changed

+108
-38
lines changed

core/shared/platform/esp-idf/espidf_malloc.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
15
#include "platform_api_vmcore.h"
26
#include "platform_api_extension.h"
37

@@ -6,16 +10,16 @@ os_malloc(unsigned size)
610
{
711
void *buf_origin;
812
void *buf_fixed;
9-
uint32 *addr_field;
13+
uintptr_t *addr_field;
1014

11-
buf_origin = malloc(size + 8 + sizeof(uint32));
15+
buf_origin = malloc(size + 8 + sizeof(uintptr_t));
1216
buf_fixed = buf_origin + sizeof(void *);
13-
if ((uint32)buf_fixed & 0x7) {
14-
buf_fixed = (void *)((size_t)(buf_fixed + 8) & (~7));
17+
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
18+
buf_fixed = (void *)((uintptr_t)(buf_fixed + 8) & (~(uintptr_t)7));
1519
}
1620

1721
addr_field = buf_fixed - sizeof(uint32);
18-
*addr_field = (uint32)buf_origin;
22+
*addr_field = (uintptr_t)buf_origin;
1923

2024
return buf_fixed;
2125
}
@@ -26,24 +30,25 @@ os_realloc(void *ptr, unsigned size)
2630
void *mem_origin;
2731
void *mem_new;
2832
void *mem_new_fixed;
29-
int *addr_field;
33+
uintptr_t *addr_field;
3034

3135
if (!ptr) {
3236
return NULL;
3337
}
3438

35-
addr_field = ptr - sizeof(uint32);
39+
addr_field = ptr - sizeof(uintptr_t);
3640
mem_origin = (void *)(*addr_field);
37-
mem_new = realloc(mem_origin, size + 8 + sizeof(uint32));
41+
mem_new = realloc(mem_origin, size + 8 + sizeof(uintptr_t));
3842

3943
if (mem_origin != mem_new) {
40-
mem_new_fixed = mem_new + sizeof(uint32);
44+
mem_new_fixed = mem_new + sizeof(uintptr_t);
4145
if ((uint32)mem_new_fixed & 0x7) {
42-
mem_new_fixed = (void *)((uint32)(mem_new_fixed + 8) & (~7));
46+
mem_new_fixed =
47+
(void *)((uintptr_t)(mem_new + 8) & (~(uintptr_t)7));
4348
}
4449

45-
addr_field = mem_new_fixed - sizeof(uint32);
46-
*addr_field = (uint32)mem_new;
50+
addr_field = mem_new_fixed - sizeof(uintptr_t);
51+
*addr_field = (uintptr_t)mem_new;
4752

4853
return mem_new_fixed;
4954
}

core/shared/platform/esp-idf/espidf_memmap.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
15
#include "platform_api_vmcore.h"
26
#include "platform_api_extension.h"
37

48
void *
59
os_mmap(void *hint, size_t size, int prot, int flags)
610
{
7-
return malloc((int)size);
11+
return os_malloc((int)size);
812
}
913

1014
void
1115
os_munmap(void *addr, size_t size)
1216
{
13-
return free(addr);
17+
return os_free(addr);
1418
}
1519

1620
int

core/shared/platform/esp-idf/espidf_platform.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ bh_platform_destroy()
1919
int
2020
os_printf(const char *format, ...)
2121
{
22-
return printf(format);
22+
int ret = 0;
23+
va_list ap;
24+
25+
va_start(ap, format);
26+
ret += vprintf(format, ap);
27+
va_end(ap);
28+
29+
return ret;
2330
}
2431

2532
int

core/shared/platform/esp-idf/espidf_thread.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ os_thread_wrapper(void *arg)
3131
korp_tid
3232
os_self_thread(void)
3333
{
34-
// only allowed if this is a thread, xTaskCreate is not enough
35-
// look at product_mini for how to use this
34+
/* only allowed if this is a thread, xTaskCreate is not enough look at
35+
* product_mini for how to use this*/
3636
return pthread_self();
3737
}
3838

@@ -143,6 +143,43 @@ os_cond_wait(korp_cond *cond, korp_mutex *mutex)
143143
return pthread_cond_wait(cond, mutex);
144144
}
145145

146+
static void
147+
msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
148+
{
149+
struct timeval tv;
150+
time_t tv_sec_new;
151+
long int tv_nsec_new;
152+
153+
gettimeofday(&tv, NULL);
154+
155+
tv_sec_new = (time_t)(tv.tv_sec + usec / 1000000);
156+
if (tv_sec_new >= tv.tv_sec) {
157+
ts->tv_sec = tv_sec_new;
158+
}
159+
else {
160+
/* integer overflow */
161+
ts->tv_sec = BH_TIME_T_MAX;
162+
os_printf("Warning: os_cond_reltimedwait exceeds limit, "
163+
"set to max timeout instead\n");
164+
}
165+
166+
tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000);
167+
if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) {
168+
ts->tv_nsec = tv_nsec_new;
169+
}
170+
else {
171+
/* integer overflow */
172+
ts->tv_nsec = LONG_MAX;
173+
os_printf("Warning: os_cond_reltimedwait exceeds limit, "
174+
"set to max timeout instead\n");
175+
}
176+
177+
if (ts->tv_nsec >= 1000000000L && ts->tv_sec < BH_TIME_T_MAX) {
178+
ts->tv_sec++;
179+
ts->tv_nsec -= 1000000000L;
180+
}
181+
}
182+
146183
int
147184
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
148185
{

doc/build_wamr.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,13 @@ ESP-IDF
464464
-------------------------
465465
WAMR integrates with ESP-IDF both for the XTENSA and RISC-V chips (esp32x and esp32c3 respectively).
466466
467-
In order to use this, you need version at least version 4.3.1 of ESP-IDF.
467+
In order to use this, you need at least version 4.3.1 of ESP-IDF.
468468
If you don't have it installed, follow the instructions [here](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/#get-started-get-prerequisites).
469469
ESP-IDF also installs the toolchains needed for compiling WAMR and ESP-IDF.
470470
A small demonstration of how to use WAMR and ESP-IDF can be found under [product_mini](/product-mini/platforms/esp-idf).
471471
The demo builds WAMR for ESP-IDF and runs a small wasm program.
472-
In order to run it for you're specific Espressif chip, edit the ['build.sh'](/product-mini/platforms/esp-idf/build.sh) file and put the correct toolchain file (see #Cross-compilation) and `IDF_TARGET`.
473-
Before compiling it is also necessary to calls ESP-IDF's `export.sh` script to bring all compile time relevant information in scope.
472+
In order to run it for your specific Espressif chip, edit the ['build.sh'](/product-mini/platforms/esp-idf/build.sh) file and put the correct toolchain file (see #Cross-compilation) and `IDF_TARGET`.
473+
Before compiling it is also necessary to call ESP-IDF's `export.sh` script to bring all compile time relevant information in scope.
474474
475475
Docker
476476
-------------------------

product-mini/platforms/esp-idf/CMakeLists.txt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ project(wamr_on_esp32c3)
77

88
enable_language(ASM)
99

10-
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
10+
if (NOT CMAKE_BUILD_TYPE)
11+
set(CMAKE_BUILD_TYPE Release)
12+
endif ()
1113

1214
if("${IDF_TARGET}" STREQUAL "")
1315
message(FATAL_ERROR "You need to set IDF_TARGET to your target string")
@@ -36,11 +38,27 @@ else()
3638
endif()
3739

3840
set(WAMR_BUILD_PLATFORM "esp-idf")
39-
set(WAMR_BUILD_INTERP 1)
40-
set(WAMR_BUILD_FAST_INTERP 0)
41-
set(WAMR_BUILD_AOT 0)
42-
set(WAMR_BUILD_LIBC_BUILTIN 1)
43-
set(WAMR_BUILD_APP_FRAMEWORK 0)
41+
42+
if (NOT DEFINED WAMR_BUILD_INTERP)
43+
set (WAMR_BUILD_INTERP 1)
44+
endif ()
45+
46+
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
47+
set (WAMR_BUILD_FAST_INTERP 0)
48+
endif ()
49+
50+
if (NOT DEFINED WAMR_BUILD_AOT)
51+
set (WAMR_BUILD_AOT 0)
52+
endif ()
53+
54+
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
55+
set (WAMR_BUILD_LIBC_BUILTIN 1)
56+
endif ()
57+
58+
if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK)
59+
set (WAMR_BUILD_APP_FRAMEWORK 0)
60+
endif ()
61+
4462

4563
# Set the compile time variable so that the right binary is selected
4664
add_compile_options(-DWAMR_BUILD_INTERP=${WAMR_BUILD_INTERP})

product-mini/platforms/esp-idf/main.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "wasm_export.h"
99
#include "aot_export.h"
1010
#include "bh_platform.h"
11-
#include "platform_api_vmcore.h"
1211
#include "test_wasm.h"
1312

1413
static void *
@@ -29,14 +28,14 @@ iwasm_main(void *arg)
2928

3029
printf("about to set up the stuff\n");
3130

32-
// setup variables for instantiating and running the wasm module
31+
/* setup variables for instantiating and running the wasm module */
3332
uint8_t *wasm_file_buf = NULL;
3433
unsigned wasm_file_buf_size = 0;
3534
wasm_module_t wasm_module = NULL;
3635
wasm_module_inst_t wasm_module_inst = NULL;
3736
char error_buf[128];
3837

39-
// configure memory allocation
38+
/* configure memory allocation */
4039
RuntimeInitArgs init_args;
4140
memset(&init_args, 0, sizeof(RuntimeInitArgs));
4241

@@ -46,22 +45,22 @@ iwasm_main(void *arg)
4645
init_args.mem_alloc_option.allocator.free_func = (void *)os_free;
4746

4847
printf("wasm_runtime_full_init\n");
49-
// initialize runtime environment
48+
/* initialize runtime environment */
5049
if (!wasm_runtime_full_init(&init_args)) {
5150
printf("Init runtime failed.\n");
5251
return NULL;
5352
}
5453

55-
// load WASM byte buffer from byte buffer of include file
54+
/* load WASM byte buffer from byte buffer of include file */
5655
printf("use an internal test file, that's going to output Hello World\n");
5756
wasm_file_buf = (uint8_t *)wasm_test_file;
5857
wasm_file_buf_size = sizeof(wasm_test_file);
5958

60-
// load WASM module
59+
/* load WASM module */
6160
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_buf_size,
6261
error_buf, sizeof(error_buf)))) {
6362
printf("Error in wasm_runtime_load: %s\n", error_buf);
64-
goto fail2;
63+
goto fail1;
6564
}
6665

6766
printf("about to call wasm_runtime_instantiate\n");
@@ -70,24 +69,24 @@ iwasm_main(void *arg)
7069
32 * 1024, // heap size
7170
error_buf, sizeof(error_buf)))) {
7271
printf("Error while instantiating: %s\n", error_buf);
73-
goto fail1;
72+
goto fail2;
7473
}
7574

7675
printf("run main() of the application\n");
7776
void *ret = app_instance_main(wasm_module_inst);
7877
assert(!ret);
7978

80-
fail1:
81-
// destroy the module instance
79+
/* destroy the module instance */
8280
printf("wasm_runtime_deinstantiate\n");
8381
wasm_runtime_deinstantiate(wasm_module_inst);
8482

8583
fail2:
86-
// unload the module
84+
/* unload the module */
8785
printf("wasm_runtime_unload\n");
8886
wasm_runtime_unload(wasm_module);
8987

90-
// destroy runtime environment
88+
fail1:
89+
/* destroy runtime environment */
9190
printf("wasm_runtime_destroy\n");
9291
wasm_runtime_destroy();
9392

0 commit comments

Comments
 (0)