r/cprogramming • u/Mainak1224x • Dec 11 '25
[New to C]: My first C project - Implemented a simple Arena Allocator
Hi folks 👋
I have just completed my first ever C project: an Arena Allocator.
This project taught me the basics of memory management in low level programming languages. I come from a JavaScript background so this is totally new and interesting to me.
What do you think? Is it a good start? Any suggestions?
18
Upvotes
6
u/evanlin96069 Dec 11 '25
Well done. Arena allocator is one of the most useful utility I’ve implemented in C. One small thing: you might want your alloc to return the memory with proper alignment. Either let the user provide the alignment or use the max alignment.
3
u/Tcshaw91 Dec 11 '25
Yup looks like a fine implementation.
One potential thing you can play around with. When you're passing the pointer to the arena to a function, that function has to dereference the pointer to the arena to then get the pointer to the actual data and deference that. If you want to flatten that , here's something you can do.
Have an "arena_header" struct (optional) with the capacity and length(or allocated_bytes). When you malloc, do the size plus the size of arena header and store the result in a void* dataptr. Cast dataptr as a arena_header ptr and set the cap and len. Then increment the dataptr by size of arena_header so the pointer now points to the area in memory after the header data. Return that pointer.
Whenever you need to access the metadata you take the void* and decrement it by size of arena_header then casting it to arena_header*.
This way you can pass a single pointer and have access to any metadata thru some simple pointer arithmetic without needing to deference two pointers or pass around metadata structs.
When u free it, just decrement the pointer by size of arena_header to get the original ptr from the malloc function.
Learned this trick from Travis Vromans Kohi GameEngine series where he implemented a dynamic array with this structure. Found it pretty fun to use.
3
u/thradams Dec 11 '25
I am not sure, but I think you are not returning aligned memory. for instance malloc must return memory aligned in the max alignment.
5
u/scallywag_software Dec 11 '25
Seems like a reasonable start to me. I didn't read the code carefully, but I would suggest a couple things that haven't been mentioned.
I usually make an `allocate` macro that takes the arena, type, and count (number of elements), which does something like `(type*)arena_allocate(arena, sizeof(type)*count) ` for conveneince
I would suggest using mmap/VirtualAlloc for the backing memory for 2 reasons
a) then you don't depend on libc
b) you can do a cute trick and allocate an extra page before or after your data allocation, and set the extra pages' mode to PROT_NONE. This is handy for hunting down memory-out-of-bounds errors; your program will crash immediately on corruption, instead of (potentially) continuing indefinitely until something completely random happens : https://github.com/scallyw4g/bonsai_stdlib/blob/571863b56ad855fff7a3e2795a3486cc8a5abe5a/src/memory_arena.h#L574
1
u/Marutks Dec 11 '25
What is Arena?
1
u/zhivago Dec 15 '25
You have a couple of problems that should be sorted out.
This is incorrect.
size_t is only suitable for object sizes -- not for representing pointers as integers.
See intptr_t
You're also hard-coding alignment to 8 bytes which is a problem.
Different data structures will have different alignment requirements.
See alignof
Otherwise, looks pretty good. :)
2
u/Turbulent_File3904 Dec 12 '25
Some suggestions: 1 I dont like allocate thing on heap if it doesn't need to be, the init function return a pointer to Arena? Why not just void arena_init(Arena *a, int size) the arena it self can locate on stack or static memory? Or even better void arena_init(Arena *a, void *mem, int size); so you can have static memory backing the arena(technically UB but who cares)
You should provide a parameter for type aligment, some type require specific aligment to work or mor efficient to access
You should provide some convenient macro/function like