#include "lib/types.h" struct DynArray { u8* base; size_t max_size_pa; // reserved size_t cur_size; // committed size_t pos; int prot; // applied to newly committed pages }; extern int da_alloc(DynArray* da, size_t max_size); extern int da_free(DynArray* da); extern int da_set_size(DynArray* da, size_t new_size); extern int da_set_prot(DynArray* da, int prot); extern int da_wrap_fixed(DynArray* da, u8* p, size_t size); extern int da_read(DynArray* da, void* data_dst, size_t size); extern int da_append(DynArray* da, const void* data_src, size_t size); // // pool allocator // // design goals: O(1) alloc and free; doesn't preallocate the entire pool; // returns sequential addresses. // // (note: this allocator returns fixed-size blocks, the size of which is // specified at pool_create time. this makes O(1) time possible.) // opaque! do not read/write any fields! struct Pool { DynArray da; size_t el_size; // all bytes in da up to this mark are in circulation or freelist. size_t pos; // pointer to freelist (opaque); see freelist_*. void* freelist; }; // ready
for use. pool_alloc will return chunks of memory that
// are exactly . all elements are made unusable
// (it doesn't matter if they were "allocated" or in freelist or unused);
// future alloc and free calls on this pool will fail.
extern int pool_destroy(Pool* p);
// indicate whether