Tech News
← Back to articles

Show HN: I wrote a minimal memory allocator in C

read original related products more articles

Custom Memory Allocator

A custom implementation of malloc, calloc, realloc, and free in C.

Overview

This project implements a memory allocator from scratch using sbrk for small allocations and mmap for large allocations. It includes optimizations like block splitting to reduce fragmentation and coalescing to merge adjacent free blocks. Please note that this allocator is not thread-safe. Concurrent calls to malloc/free/realloc will cause undefined behavior. I've also written a blog post (~20 minute read) explaining step by step the process behind writing this memory allocator project, if that's of interest, you can read it here!

Building

Prerequisites

GCC compiler

Make

POSIX-compliant system (Linux, macOS) because we're using sbrk() and mmap() <- won't work on Windows

Quick Start

... continue reading