wee_alloc(malloc 替代方案)如何知道在 WebAssembly 中从哪里开始堆?

How does wee_alloc (a malloc alternative) know where to start the heap in WebAssembly?

我正在尝试在自定义语言中使用 wee_alloc,在 WebAssembly 中使用 运行。但是,我需要完全理解它是如何知道从哪里开始堆的,这样我的堆栈和静态分配就不会破坏它,反之亦然。

据我了解,malloc 等如何。知道从哪里开始堆是平台相关的,通常只是一个约定,或者在某些情况下不适用。但是在 WebAssembly 中我们只能有一个连续的线性内存,所以我们必须共享它并且需要使用约定。

Reading through the code it appears wee_alloc 所做的是假设我们开始的任何内存都是完全禁止的,而是使用 grow_memory 指令来创建第一块堆所需的内存。这实际上意味着堆开始的 index/address 是初始大小加一的最高索引。 (编辑:其实不是+1,我忘了索引是从零开始的;差一错误☠️)

例如如果我们从 1 页的初始内存大小开始:

 current_memory = 1 page = 64KiB = 65,536 bytes

然后堆从索引 65537 开始。

我的理解正确吗?

Your understanding is correct! With a small exception though: since the indexes are zero based, the last index of the first page is 65535, and the first index of the second page is 65536. - @pepyakin

https://github.com/rustwasm/wee_alloc/issues/61#issuecomment-416868326