3 ms·
// Really trivial malloc() implementation. We just allocate bytes sequentially from the start of // the heap, and reset the whole heap to empty at the start o
by 1_player 8y ago
// Really trivial malloc() implementation. We just allocate bytes sequentially from the start of
// the heap, and reset the whole heap to empty at the start of each request.
extern byte __heap_base; // Start of heap -- symbol provided by compiler.
byte* heap = NULL; // Current heap position.
void* last_malloc = NULL; // Last value returned by malloc(), for trivial optimizations.
void* malloc(size_t n) {
last_malloc = heap;
heap += n;
return last_malloc;
}
Am I missing something? How can this work? __heap_base is never used, so first malloc returns a pointer to (void * )NULL;
Shouldn't it be:
byte *heap = &__heap_base;
or something to that effect?
I haven't read the rest of the code, nor know how WebAssembly works, but I can't make sense of that snippet.
- Arnavion 8y agohttps://github.com/cloudflare/cloudflare-workers-wasm-demo/blob/a153279d3ba17b19aa25b0afd329515f5412077b/main.c#L31 https://github.com/cloudflare/cloudflare-workers-wasm-demo/b... // init() is called from JS to allocate space for the image file. byte* init(size_t image_size) { // Reset the heap to empty. (See malloc() implementation in bootstrap.h.) heap = &__heap_base;