alt.hn

5/11/2026 at 9:53:02 AM

Building a Memory Allocator from Scratch in C

https://0xkiire.com/memory-allocators/

by kiirecodes

5/13/2026 at 12:00:44 PM

Text appears generated. Minus 10 points.

by ETAOINSHRDLU2

5/11/2026 at 7:17:28 PM

This was a class assignment in the 15-213 class at Carnegie Mellon. The staff had set up a test suite and an online leaderboard to rank the speed of each student's malloc implementation.

I figured out that the test cases allocated a disproportionate amount of X-byte blocks. I was able to get to the top by hardcoding a specific freelist just for X-byte blocks.

Learned a lesson about easily it is to game a benchmark :)

by msarnoff

5/12/2026 at 2:13:40 PM

This is a useful lesson! Tuning an allocator for a single application reduces to optimizing for that application’s empirical allocation patterns (sizes, lifetimes, access, usage).

by variadix

5/11/2026 at 10:22:47 PM

Implemented my own specialized memory allocator 26+ yrs ago. (Y2K timeframe) Probably older than the most of the CMU students :-(

Use pre-allocated pools with array of indexes, free/allocation idx for alloc and free.

Con: Fixed pool size and fixed amount of memory can be allocated per pool.

Pro: constant cost operations per alloc/free via Atomic inc/dec of idx - no linklist tranversing ; Can be alloc in kernel space and free in user space (linux/QNX) and in multiple user processes when memory pools are in shmem; Run very will in SMP environment without any locks - all memory contentions were handled with atomic +/- alloc/free idx.

Same source code run in QNX, vxworks and linux (kernel and user space) at that time.

by tkinom

5/12/2026 at 4:10:56 AM

I was trying to get my c wasm module down in size and emmalloc is pretty small, but it requires a lot of js glue to make it work, but using a small allocator like walloc requires no glue and it's insanely small. I got my module down from 27kb to 17kb.

I'm gonna read this article and try making my own allocator next.

by tnelsond4

5/12/2026 at 2:47:54 AM

We also did this at Harvard in CS 61, which is the Systems Programming and Machine Organization class currently taught by Eddie Kohler (and has been for a while). It's a good exercise :)

by ethancedwards8

5/11/2026 at 10:39:12 AM

[dead]

by pillmillipedes