[OT] web assembly memory model
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Sun Jan 4 06:05:12 UTC 2026
On 04/01/2026 6:28 PM, Walter Bright wrote:
> On 1/3/2026 7:30 PM, Richard (Rikki) Andrew Cattermole wrote:
>>> I don't see why calls to `new` cannot be redirected to whatever WASM
>>> does?
>>
>> You can't do pointer arithmetic with WasmGC.
>> No subtraction, no getting pointers to fields, nothing like that.
>> That is the GC offering currently.
>
> ```
> struct S { int a; }
> S* s = new S();
> s.a = 3;
> ```
>
> What's the problem?
Loading and storing fields work.
But this doesn't when using the Wasm GC:
``int* ptr = &s.a;``
Or this:
```d
func(s.a);
void func(ref int);
```
>> For the linear memory, its a memory mapper only, sbrk.
>> Oh and you can have multiple linear memories that you have to keep
>> track what the offset is actually for when dereferencing.
>
> I don't get it.
Ahhh, I see.
Here is the man page for bsd 2.11 which is the last BSD (that is
actively in use by retro community) to run on PDP-11:
https://man.freebsd.org/cgi/man.cgi?query=sbrk&apropos=0&sektion=0&manpath=2.11+BSD&arch=default&format=html
It was removed in Posix 2001.
https://en.wikipedia.org/wiki/Sbrk
This is how memory is mapped into a process and then is cut up and
returned by memory allocators like malloc.
For an example of this, open K&R C Programming Language 2nd edition to
page 185. It has an example malloc implementation that uses sbrk.
This isn't how its done today, these days memory is mapped using mmap
instead, sbrk isn't an option.
Web assembly folks however decided to do it the way we did it in the
70's before MMU's were a thing.
>> They are typed entirely differently, you cannot mix them.
>> It is exactly like near vs far pointers.
>
> ??
Okay, I think I understand the confusion.
Web assembly isn't a byte code like x86 is.
It is fully typed, a reference to a GC object is different to a pointer
to linear memory (sbrk).
In pseudo code:
```
struct S {
int field;
}
linear(S*) l = cast(S*)linear_alloc(4);
l++; // ok
gc(S) g = new S;
int* ptr = &g.field; // error no instruction to do this
l = g; // error
g = l; // error
l - l; // ok
g - g; // error no instruction to do this
```
More information about the Digitalmars-d
mailing list