Memory Model#
Every RCSW module that owns memory lets the caller decide, per region, whether
the library allocates it or the caller provides it. This makes the same code
usable with malloc() on Linux and with no heap at all on bare metal.
Regions#
A module manages up to three regions. Each has a flag; passing the flag means “I am providing this region”.
Region |
Flag |
Caller provides |
|---|---|---|
Handle |
The |
|
Data |
Storage for the elements, passed in |
|
Metadata |
Per-element bookkeeping such as list or tree nodes, passed in
|
RCSW_NOALLOC_ALL sets all three. The flags are independent: you can
provide a handle and let the library allocate the elements. Each module’s
flags field lists which flags it accepts; a module that has no data or
metadata region ignores those flags. Building with
RCSW_CONFIG_NOALLOC implies all three flags for every
module.
xx_destroy() frees only the regions the library allocated. A region the
caller provided is left alone, and remains the caller’s to release.
Sizing caller-provided memory#
Each region the caller provides must be large enough for everything the library will put in it:
Handle: at least
sizeof(struct xx).Data: usually
xx_element_space(max_elts, elt_size)bytes.Metadata: usually
xx_meta_space(max_elts)bytes, for modules that have metadata (lists, trees, hashmaps, memory pools, multififo, swbus). Contiguous structures such asdarrayandrbufferhave none.
A few sizing functions take different arguments because the structure is sized differently:
Each one’s API documentation lists its arguments.
The sizing functions include any internal bookkeeping (allocation maps,
sentinel nodes), so the result can be larger than max_elts * elt_size.
They are static inline functions, not constant expressions; to use a
static buffer, size it generously and check it at run time, as the
Quickstart Example does.
Warning
Undersized caller memory corrupts whatever follows it. The library cannot
detect it, and with zeroing enabled the damage happens inside xx_init()
itself, far from the eventual symptom.
Zeroing#
RCSW_ZALLOC asks for memory to be zeroed before use: library
allocations use calloc(), and caller-provided regions are cleared with
memset() for their full size. RCSW_CONFIG_ZALLOC turns
this on everywhere.
Because caller-provided regions are cleared for their full size, zeroing turns an undersized buffer from a latent bug into an immediate overwrite of the neighbouring memory.
Layout & Alignment#
How RCSW lays out caller-provided memory, and what it guarantees about alignment, so the same code is correct on x86, ARM, RISC-V and smaller embedded targets.
The short version:
Pass memory aligned to at least
RCSW_CONFIG_PTR_ALIGN(anydptr_tarray is), sized with the structure’s*_element_space()/*_meta_space()functions.Set
elt_sizetosizeofyour element type.Every internal node and element is then naturally aligned, at every optimization level, with every conforming compiler.
Element i of a data structure’s element pool is at i * elt_size from the
start of the pool, exactly like a C array. With elt_size == sizeof(T), every
element therefore has T’s alignment, because sizeof(T) is always a
multiple of _Alignof(T). An elt_size that is not a multiple of
RCSW_CONFIG_PTR_ALIGN is allowed; elements then have their
type’s alignment but not the stronger
RCSW_CONFIG_PTR_ALIGN.
The same applies to mpool chunks, and therefore to swbus
packet buffers: chunk i is at i * elt_size, so a pool with 170-byte
elements hands out buffers that are only 2-byte aligned. Choose element sizes
that are multiples of the alignment your data needs, or copy packets out before
accessing them as structs.
Warning
Setting RCSW_CONFIG_PTR_ALIGN to anything other
than the actual alignment for the target platform breaks all
alignment and correctness guarantees that RCSW otherwise makes.
dptr_t and RCSW_CONFIG_PTR_ALIGN#
RCSW_CONFIG_PTR_ALIGN is the alignment RCSW promises for the
start of every element and node in RCSW-managed memory. dptr_t is an
unsigned integer type with exactly that alignment, so a dptr_t[] buffer is
always suitably aligned for use as caller-provided storage. Choose the
alignment of the most strictly aligned type you store; e.g., 8 on 64-bit
targets that store pointers, or structs containing them.
RCSW’s own list and tree nodes contain pointers, so with caller-provided
metadata (RCSW_NOALLOC_META) the value must also be at least the
target’s pointer alignment. Below that (e.g., 4 on a 64-bit target), every
node in the metadata region has misaligned pointers: undefined behavior that
works on x86 but traps or slows down on stricter targets. Metadata the
library allocates itself is not affected.
The library checks at build time that the target’s ABI really gives dptr_t
that alignment; on 32-bit x86, for example, uint64_t is only 4-byte aligned
in structs, so 8 is rejected there.
Why internal structs are not packed#
Nodes and other internal structs (bstree_node, struct
ostree_node, struct inttree_node, struct llist_node,
hashnode, allocm_entry, etc) are declared with natural
alignment, and with aligned(sizeof(dptr_t)) where an array of them must
keep every element aligned. They are deliberately not packed.
packed removes padding by placing each member immediately after the previous
one. aligned only raises the alignment of the struct as a whole; it does not
restore the padding inside it. For example, if in bstree_node the key is 4
bytes, with packed the data, left, right and parent pointers
would start at offsets 4, 12, 20 and 28: misaligned on every 64-bit target. A
misaligned pointer access is undefined behavior in C; it is slow on x86, and on
many ARM, RISC-V and MIPS cores it traps.
Without packed the compiler inserts 4 bytes of padding after the key, and
the pointers land at 8, 16, 24 and 32. The node is the same size either way (48
bytes on 64-bit targets), because aligned(8) already rounded the packed size
up to a multiple of 8.
A natural-alignment layout is fixed by the platform ABI (System V psABI for x86 and x86-64, AAPCS for ARM, the RISC-V psABI, and so on): each member is placed at the next offset that is a multiple of its alignment, and the struct is padded to a multiple of its largest member alignment. This is what makes separately compiled code and libraries interoperate, so:
It does not change with
-O0/-O2/-Osor LTO;GCC and Clang produce the same layout for the same target;
The only thing that can change it is a different target ABI, which is exactly when you rebuild the library anyway.