@@ -3,9 +3,9 @@ **kmem** is a libk module that contains various functions for memory allocation and deallocation. it uses the **short** naming convention with the glyph `m`. # description -kmem allocators can work in several different ways. they can allocate memory directly from the heap (like `kmheapa()` and `kmlina()`), use a header that has already been allocated by another function, or allocate memory only from a pre-allocated pool. linear allocation with pool allocation is particularly useful, as it permits the very rapid allocation and deallocation of lots of objects with only a few adjustments to the heap, and no possibility of fragmentation or need for expensive algorithms like `malloc()` or `kmheapa()` +kmem allocators can work in several different ways. they can allocate memory directly from the heap (like `kmheapa()` and `kmlina()`), use a header that has already been allocated by another function, or allocate memory only from a pre-allocated pool. linear allocation with pool allocation is particularly useful, as it permits the very rapid allocation and deallocation of lots of objects with only a few adjustments to the heap, and no possibility of fragmentation or need for expensive algorithms like `malloc()` or `kmheapa()`. functions that can return a value and an error condition always return a value of type `kmres`, a struct that contains the field `kmcond` with an error code, and one of `raw` (for functions that return raw pointers) or `ptr` (for functions that return `kmptr` objects). # module functions kmem supplies two module-level functions, used to interact with the `kmptr` container type. @@ -112,10 +112,10 @@ kmem function names are based on the **method** of allocation and the **action** being performed. methods are listed in the section below. kmem defines a number of standardized actions, though not every method uses every action. the character listed in brackets is suffixed to the name of the method to produce a function name: for instance, `kmheapa` will allocate memory on the heap, while `kmrefd` will decrement the reference count of its argument. * initialize [i] - initializes a memory store on the heap * initialize fixed [if] - initialize a memory store on the stack or in a fixed-size global - * allocate [a] -a llocate a new region of memory of the given size, ready to write, and write a pointer to it into argument `where`. returns a value of `kmcond`; always check this to ensure allocation succeeded. contents of that region undefined. takes parameters `void** where, size_t sz`. - * allocate pointer object [o] - like *allocate*, but fills in a `kmptr` instead of a raw `void*`. takes parameters `kmptr* where, size_t sz`. + * allocate [a] - allocate a new region of memory of the given size, ready to write, and return a struct `kmres` containing an error code and, if the call succeeded, a pointer in the field `raw`. always check the `cond` field of its return value to ensure allocation succeeded. contents of the region specified by the `raw` field are undefined. takes parameter `size_t sz`. + * allocate pointer object [o] - like *allocate*, but yields a `kmptr` in the `ptr` field of its return value instead of a raw `void*`. takes parameters `size_t sz`. * zero [z] - allocate a new region of memory and zero it before returning it for writing. * zero pointer object [zo] - like *zero*, but returns a `kmptr` instead of a raw `void*`. * free [f] - free a section of memory, either decrementing a reference count or returning it to whatever pool it came from. * shred [s] - destroy whatever was in the segment of memory, then return it to the pool it came from. @@ -127,42 +127,44 @@ kmem currently supports the following methods of memory management, along with which methods are defined for it. (note that `a` implies `z` and `f` implies `s`). a method may be excluded from a libk binary by defining the flag `KFmem_no[name]`, e.g. `KFmem_noheap`. the fastest allocator is the linear allocator, which should be sufficient for most simple programs. it allocates and deallocates memory simply by resizing the stack; there is no fragmentation, but objects must be freed in the order they are allocated. however, entire groups of objects can be freed at once at very little cost. +**caveat scriptor:** the linear allocation method is unfortunately complicated by the fact that the methods it uses make it fundamentally incompatible with use in the program of the "malloc" allocator used by libc. this should not be a problem for programs that only link libk, but if they link in any external libraries that depend on libc and which use malloc internally, **the linear allocator cannot be used.** + * `lin` [iax] - linear heap allocator - * `kmlini(void) → void*` - return a pointer to the current top of the heap - * `kmlina(size_t) → void*` - allocate space on the heap and increase its size appropriately - * `kmlinz(size_t) → void*` - allocate zero-filled space on the heap and increase its size appropriately - * `kmlinx(void*) → void*` - returns the top of the heap to the location specified, freeing all memory allocated since the call to kmlini() or `kmlina()` that produced it + * `kmlini` - return a pointer to the current top of the heap + * `kmlina` - allocate space on the heap and increase its size appropriately + * `kmlinz` - allocate zero-filled space on the heap and increase its size appropriately + * `kmlinx` - returns the top of the heap to the location specified, freeing all memory allocated since the call to kmlini() or `kmlina()` that produced it * `heap` [af] - random heap allocation - * `kmheapa(size_t) → void*` - allocate - * `kmheapz(size_t) → void*` - zero-allocate - * `kmheapao(size_t) → kmptr` - allocate pointer object - * `kmheapzo(size_t) → kmptr` - zero-allocate pointer object - * `kmheapf(void*) → void` - free - * `kmheaps(void*) → void` - shred + * `kmheapa` - allocate + * `kmheapz` - zero-allocate + * `kmheapo` - allocate pointer object + * `kmheapzo` - zero-allocate pointer object + * `kmheapf` - free + * `kmheaps` - shred * `ref` [afu] - reference-counted heap object - * `kmrefa(kmcell*, size_t) → void*` - allocate - * `kmrefz(kmcell*, size_t) → void*` - zero-allocate - * `kmrefao(kmcell*, size_t) → void*` - allocate pointer object - * `kmrefzo(kmcell*, size_t) → void*` - zero-allocate pointer object - * `kmreff(void*) → void` - downref; free if last ref - * `kmrefs(void*) → void` - downref and mark for shred on last ref + * `kmrefa` - allocate + * `kmrefz` - zero-allocate + * `kmrefo` - allocate pointer object + * `kmrefzo` - zero-allocate pointer object + * `kmreff` - downref; free if last ref + * `kmrefs` - downref and mark for shred on last ref * `pool` [ixaf] - memory pool * `kmpooli(kmcell*, size_t sz, size_t n) → kmpool*` - initialize a fixed memory pool (a pool of `n` cells of length `sz`) - * `kmpoolx(kmpool*) → void` - tear down a memory pool - * `kmpoola(kmpool*) → void*` - allocate from pool - * `kmpoolz(kmpool*, size_t) → void*` - zero-allocate from pool - * `kmpoolao(kmpool*, size_t) → void*` - allocate pointer object - * `kmpoolzo(kmpool*, size_t) → void*` - zero-allocate pointer object - * `kmpoolf(void*) → void` - downref; free if last ref - * `kmpools(void*) → void` - downref and mark for shred on last ref + * `kmpoolx` - tear down a memory pool + * `kmpoola` - allocate from pool + * `kmpoolz` - zero-allocate from pool + * `kmpoolo` - allocate pointer object + * `kmpoolzo` - zero-allocate pointer object + * `kmpoolf` - downref; free if last ref + * `kmpools` - downref and mark for shred on last ref * `tree` [af] - uses a node-child strategy. when a node is freed, all of its children are automatically freed as well. - * `kmtreea(kmcell* src, void* parent, size_t) → void*` - create a tree node. if `parent` is NULL, the node will the top of a new tree. if src is null, allocate on-heap. - * `kmtreez(kmcell* src, void* parent, size_t) → void*` - like `kmtreea` but zeroed - * `kmtreeao(kmcell* src, void* parent, size_t) → kmptr` - like `kmtreea` but returns a `kmptr` - * `kmtreezo(kmcell* src, void* parent, size_t) → kmptr` - like `kmtreez` but returns a `kmptr` - * `kmtreef(void*) → kmptr` - frees a node and all its children + * `kmtreea` - create a tree node. if `parent` is NULL, the node will the top of a new tree. if src is null, allocate on-heap. + * `kmtreez` - like `kmtreea` but zeroed + * `kmtreeao` - like `kmtreea` but returns a `kmptr` + * `kmtreezo` - like `kmtreez` but returns a `kmptr` + * `kmtreef` - frees a node and all its children ## macros kmem defines the following macros.