deislabs/wasi-experimental-http

`alloc()` function

Closed this issue · 4 comments

Hi!

Memory allocations currently rely on exporting an alloc() function.

While this works for Rust, it doesn't play well with Zig, that doesn't hide memory allocations (any function requiring dynamic allocations must accept an allocator as a parameter). This is not going to play well with GC'd languages either.

Maybe the application could call an initialization function, passing a function pointer to the allocation function instead?

Is there an example of a Zig project that does that?
I'd very much like to learn about how that works.

Hi!

All of them :) https://ziglang.org/documentation/0.7.1/#Memory

There is no such thing as a global allocator.

Application can choose whatever they want every time a function requiring dynamic allocation is called. Such an allocator can be a preallocated fixed buffer, an arena, a debugging allocator, etc. This gives a lot of control and allows for very efficient memory usage.

We did something similar at Fastly; I wrote a blurb about this: https://www.fastly.com/blog/webassembly-memory-management-guide-for-c-rust-programmers

But maybe an alternative would be for the guest to provide a fixed-size buffer, as well as the size.

The req() function is then called repeatedly, filling the buffer in a streaming fashion, similar to a traditional read() system call, and returning 0 when the end of the stream is reached.

This allows the guest to do whatever they want in order to store the incoming data.

#43 was merged, which no longer required an alloc function to be exported.
Closing.