This is an implementation of stdlib malloc. Heap is an array of uintptr_t. There is also a garbage collector which frees unreachable chunks of allocated memory.
void* mem_alloc(size_t size_bytes)
Allocates memory on heap in words. (Padding is done automatically.)
void mem_free(void* ptr)
Frees memory if its allocated.
void merge_fragments()
: Merge fragmented memory. This is called at every mem_alloc()
.
void heap_collect()
: Frees unreachable chunks of allocated memory. This should be called by user.
Limitations
- No packed structures
- No tricks that obscure the pointers
I followed Tsoding's "Artifacts of that Memory Management Tsoding Session" tutorial so this repository is very much identical to his one:
- Tsoding's repo
- Tsoding Writing My Own Malloc in C Video
- Tsoding Writing Garbage Collector in C Video
Other resources that might be helpful: