Modules

Flat_Pool

moduleSource
macOSLinuxWindows

#import "Flat_Pool";

A bump allocator over one contiguous range of reserved address space.

Procedures#

(pool: *Flat_Pool, reserve := DEFAULT_VIRTUAL_MEMORY_RESERVE)

Reserves reserve bytes of address space for the pool, rounded up to a whole number of pages, and starts it empty.

(pool: *Flat_Pool, nbytes: s64) -> *void

Returns nbytes bytes aligned to the pool's alignment, initializing the pool first if needed, or null if the OS refuses to commit the pages.

(pool: *Flat_Pool, overwrite_memory := false)

Rewinds the pool so the next get starts at the beginning; the committed pages stay committed.

(pool: *Flat_Pool)

Returns the whole reservation to the OS and leaves the pool uninitialized, so a later get starts over. Safe on a pool that never allocated.

(mode: Allocator_Mode, requested_size: s64, old_size: s64, old_memory: *void, allocator_data: *void) -> *void

The Allocator_Proc for a flat pool: use it as Allocator.{ flat_pool_allocator_proc, *pool }.

()

A small demonstration: initialize, allocate, reset with overwrite, finalize. It exists as a smoke test and is not part of the API.

Types#

Flat_Poolstruct
:: struct

A bump allocator over one contiguous range of reserved address space, committed page by page as it is used.

pool: Flat_Pool;
init(*pool, 1024 * 1024);
defer fini(*pool);

first := cast(*int) get(*pool, size_of(int));
first.* = 7;
for 0..9999 get(*pool, 64); // growing never moves `first`
assert(first.* == 7);

reset(*pool);
assert(get(*pool, size_of(int)) == first); // the same memory comes back

Doesn't run in the browser (skipped on wasm).

Constants#