was reading some custom C memory pool allocator code and stumbled on std::aligned_alloc today.
The cool part wasn't even the allocation, it was how the pool reuses freed object memory itself as the freelist
std::aligned_alloc(alignof(T), capacity * sizeof(T))
Then free slots are reused as freelist nodes:
struct FreeNode {
FreeNode* next;
};
using:
reinterpret_cast<FreeNode*>(slot_ptr(i - 1))
So the same chunk of memory is:
- a real object while allocated
- a linked-list node while free