I'm writing small programs in WebAssembly text format (.wat
). I've looked at Wasm code compiled from C and it looks like the common practice is to maintain a global stack pointer, and when we enter a function, to create a stack frame by subtracting the stack pointer by some multiple of 4 (e.g. 16):
(global.set $g0
(local.tee $l1
(i32.sub
(global.get $g0)
(i32.const 16))))
;; --snip--
(global $g0 (mut i32) (i32.const 67088))
I need to implement a very basic malloc
to do dynamic memory management. In this implementation, I think I need to grow the memory with memory.grow
if I run out of memory.
My question:
What should I do with the stack space when I grow the memory? AFAIK if I don't do anything the memory looks like this:
+-----------------------+ high memory
| Newly acquired memory |
| |
+-----------------------+
| Stack space |
+-----------------------+
| Heap (managed with |
| malloc) |
+-----------------------+ low memory