compiler-construction – wee_alloc(malloc替代)如何知道在WebAssembly中启动堆的位置?

我正在尝试在自定义语言中使用
wee_alloc,在WebAssembly中运行.但是,我需要完全了解它如何知道在哪里启动堆,以便我的堆栈和静态分配不会破坏它,反之亦然.

这是我的理解,如malloc,等.知道从哪里开始堆是依赖于平台的,通常只是一个约定,或者在某些情况下不适用.但是在WebAssembly中,我们只能有一个连续的线性内存,因此我们必须共享它,并且需要使用约定.

Reading through the code it appears wee_alloc所做的是假设我们开始的任何内存完全是禁止的,而是使用grow_memory指令来创建堆所需的第一块内存.这实际上意味着堆的起始索引/地址是初始大小的最高索引加一. (编辑:它实际上不是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

点赞