Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

alloc() function #32

Closed
jedisct1 opened this issue Mar 15, 2021 · 4 comments
Closed

alloc() function #32

jedisct1 opened this issue Mar 15, 2021 · 4 comments

Comments

@jedisct1
Copy link
Contributor

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?

@radu-matei
Copy link
Member

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

@jedisct1
Copy link
Contributor Author

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

@jedisct1
Copy link
Contributor Author

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.

@radu-matei
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants