Skip to content

Instantly share code, notes, and snippets.

@jdf-id-au
jdf-id-au / align.c
Created February 5, 2025 11:28
Alignment calculation for arena
// Trying to understand the padding calculation from https://nullprogram.com/blog/2023/09/27/
byte *alloc(arena *a, size objsize, size align, size count) {
size avail = a->end - a->beg;
/*
Padding is how far the next aligned address is beyond the beginning of the arena.
(The "beginning" of the arena advances as data is added, and is really the beginning of the remaining avilable space.)
Use wrapping unsigned integer negation of the beginning address to measure what's left rather than what's in use.
Calculate how far this address is beyond the previous aligned address using modulo: