Skip to content

Commit 3a4bbf1

Browse files
committed
Add memoryLimit
1 parent 0d78706 commit 3a4bbf1

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

examples/Wasm_Blink/Wasm_Blink.ino

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66

77
#include <wasm3.h>
88

9-
// You can redefine the default LED pin here, if needed
10-
//#define LED_BUILTIN D7
9+
/*
10+
* Configuration
11+
*/
1112

12-
#ifndef LED_BUILTIN
13+
// Redefine the default LED pin here, if needed
1314
#define LED_BUILTIN 13
14-
#endif
1515

1616
#define WASM_STACK_SLOTS 1024
1717
#define NATIVE_STACK_SIZE 32768
1818

19+
// For devices that cannot allocate a 64KiB wasm page
20+
#if defined(ESP8266) || defined(PARTICLE) //...
21+
#define WASM_MEMORY_LIMIT 2048
22+
#endif
23+
1924
/*
2025
* WebAssembly app
2126
*
@@ -158,6 +163,10 @@ void wasm_task(void*)
158163
IM3Runtime runtime = m3_NewRuntime (env, WASM_STACK_SLOTS, NULL);
159164
if (!runtime) FATAL("NewRuntime", "failed");
160165

166+
#ifdef WASM_MEMORY_LIMIT
167+
runtime->memoryLimit = WASM_MEMORY_LIMIT;
168+
#endif
169+
161170
IM3Module module;
162171
result = m3_ParseModule (env, &module, app_wasm, app_wasm_len-1);
163172
if (result) FATAL("ParseModule", result);

src/m3_env.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ M3Result ResizeMemory (IM3Runtime io_runtime, u32 i_numPages)
291291
if (numPagesToAlloc <= memory->maxPages)
292292
{
293293
size_t numPageBytes = numPagesToAlloc * d_m3MemPageSize;
294+
295+
// Limit the amount of memory that gets allocated
296+
if (io_runtime->memoryLimit) {
297+
numPageBytes = min(numPageBytes, io_runtime->memoryLimit);
298+
}
299+
294300
size_t numBytes = numPageBytes + sizeof (M3MemoryHeader);
295301

296302
size_t numPreviousBytes = memory->numPages * d_m3MemPageSize;

src/m3_env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ typedef struct M3Runtime
223223
M3Result runtimeError;
224224

225225
M3Memory memory;
226+
u32 memoryLimit;
226227

227228
M3ErrorInfo error;
228229
#if defined(d_m3VerboseLogs)

0 commit comments

Comments
 (0)