Skip to content

Memory Allocator

You'll write your own malloc, free and realloc on top of raw pages from the kernel, then make a real program run on it without noticing the difference.

About

Every call to malloc you've ever made went to a chunk of code somebody wrote, and it's shorter than you'd guess. You'll ask the kernel for pages with mmap or sbrk, carve them up with a free list, merge neighbors on free, and keep a header in front of every block. Then you'll slip it under a real program with LD_PRELOAD and watch it keep running. Fragmentation and alignment are where the hours go, and measuring them is half the badge. Don't preload your allocator into anything you care about; a scratch shell in a VM is the place.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Three weeks
Skill level
Advanced (years of practice)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Get memory from the kernel with mmap or sbrk and hand out blocks with a bump allocator. Ask for one large region, keep a pointer to the next free byte, and return 16-byte aligned addresses. No free yet. Run a test that allocates a thousand blocks and writes to each. Take a screenshot of the test and of the region in /proc/self/maps or vmmap. Photo
  2. 2
    Add a header to each block and a free list, so freed blocks get reused. Store the size and a free flag in a header just before the user's pointer. On free, mark the block and push it on the list. On malloc, walk the list for a fit before taking fresh space. Take a screenshot of a test that frees a block and gets the same address back on the next allocation. Photo
  3. 3
    Split large free blocks and merge adjacent free blocks to fight fragmentation. When a free block is bigger than needed, cut off the remainder as a new free block. When you free a block next to another free one, merge them. Allocate and free in a pattern that would fragment a naive allocator, and show your heap stays small. Take a screenshot of the heap size before and after a 100,000 alloc and free loop. Photo
  4. 4
    Implement realloc and calloc, and run a randomized test that checks every byte. realloc must grow in place when the neighbor is free and copy otherwise. calloc must zero the bytes. Write a test that mixes all four calls with random sizes 100,000 times, fills each block with a pattern, and checks the pattern before freeing. Take a screenshot of the test passing. Photo
  5. 5
    Load your allocator under a real program with LD_PRELOAD and show it works. Build it as a shared library and run LD_PRELOAD=./mymalloc.so ls -la, then something bigger like python3 or vim. On a Mac use DYLD_INSERT_LIBRARIES, which needs a few more hoops. Put one global lock around malloc and free before you try anything that uses threads, or it will crash in a way that has nothing to do with your free list. Film a short clip of a real program running on your allocator. Video
  6. 6
    Benchmark against the system malloc and write down where yours loses. Time a million small allocations and frees in both. Yours will be slower, and the interesting question is why: a linear free list walk, no size classes, no per-thread cache. Write a paragraph on what you'd add next. Take a screenshot of the timings and the paragraph. Photo