A portable, libre heap allocator replacement for malloc/free with built-in safety checks. Detects buffer overflows, double frees, invalid frees, and memory leaks.
|
RealFCC
196a84947a
- alloc_tracker_t list + per-node sys_alloc replaced by a flat power-of-two slot table (leak_slot_t), grown geometrically - leak_tracker_add/remove now O(1) avg instead of O(n) scan + mmap per node - remove uses backward-shift deletion to keep probe chains short over long run times (no tombstone buildup) - new leak_tracker_update(ptr, size, file, line) collapses the remove+add pair used by realloc into a single in-place op - alloc.c: lr_realloc_debug uses leak_tracker_update at both grow-in-place and shrink-in-place paths - internal.h: add leak_tracker_update prototype No change to public API or leak report output format. |
||
|---|---|---|
| docs | librealloc v1.0-rc1: portable heap allocator with safety features | |
| examples | Revert "Restructure allocator to glibc-style ptmalloc arena" | |
| include | Revert "Restructure allocator to glibc-style ptmalloc arena" | |
| src | leak: replace O(n) linked-list tracker with open-addressing hash table | |
| .gitignore | librealloc v1.0-rc1: portable heap allocator with safety features | |
| CLAUDE.md | librealloc v1.0-rc1: portable heap allocator with safety features | |
| CONTRIBUTING.md | librealloc v1.0-rc1: portable heap allocator with safety features | |
| LICENSE | librealloc v1.0-rc1: portable heap allocator with safety features | |
| Makefile | leak: replace O(n) linked-list tracker with open-addressing hash table | |
| README.md | chore: Fixed the docs | |
librealloc
A portable, libre heap allocator replacement for malloc/free with built-in safety features. Designed for POSIX systems (Linux, macOS, BSD).
Features
- Fast segregated free lists for near O(1) best-fit allocation
- Thread-safe with pthread mutex protection
- Buffer overflow detection via canaries on every block, crashes on corruption
- Double-free detection with immediate abort and source location
- Leak detection that crashes at exit if any allocations were not freed
- Source tracking records file and line number for every allocation
- No external dependencies beyond POSIX syscalls (mmap)
- Portable across Linux (glibc, musl), macOS, and BSD
Documentation
- Installation
- API Reference
- Examples
- Building
- Internals
- Design Decisions
- Comparison
- Security Policy
- Porting Guide
- FAQ
- Man page
Quick Start
make # Build library and example
make test # Build and run safety tests
Usage
#include "librealloc.h"
int main() {
char *ptr = lr_malloc(100);
strcpy(ptr, "Hello!");
ptr = lr_realloc(ptr, 200);
lr_free(ptr);
return 0;
}
Link with: -llibrealloc -lpthread
API Reference
Allocation
| Function | Description |
|---|---|
lr_malloc(size) |
Allocate size bytes. Records source file and line. |
lr_calloc(nmemb, size) |
Allocate zeroed memory. Checks for overflow. |
lr_realloc(ptr, size) |
Resize allocation. Copies existing data. |
lr_free(ptr) |
Free memory. Checks for errors, aborts on violation. |
Diagnostics
| Function | Description |
|---|---|
lr_alloc_dump_stats() |
Print allocation statistics to stdout. |
lr_alloc_check_integrity() |
Verify heap integrity. |
lr_alloc_verbose(enable) |
Enable verbose debug output to stderr. |
lr_force_leak_check() |
Manually trigger leak detection. |
lr_alloc_size(ptr) |
Return usable size of allocated block. |
lr_alloc_block_check(ptr) |
Verify canaries and magic on a block. |
Safety Guarantees
- Buffer overflow detection - Canaries before and after user data. Detected when the block is freed. Program aborts with file and line number.
- Double-free prevention - Blocks track allocation state. Second free causes immediate abort with source location.
- Invalid free detection - Magic number validation on every free. Stack pointers and garbage addresses are caught.
- Memory leak detection - All allocations tracked. At exit, detailed report printed and program aborts.
Limitations
librealloc makes C code safer but cannot prevent:
- Use-after-free through stale pointers
- Dangling pointer aliases after freeing
- Null pointer dereferences
- Stack buffer overflows
- Data races on shared data (only the allocator itself is thread-safe)
Performance
Segregated free lists with 12 size classes (32 bytes to 64KB). Coalescing on free reduces fragmentation. Per-block overhead is 64 bytes.
Platform Support
- Linux (glibc, musl)
- macOS
- FreeBSD, OpenBSD, NetBSD
Windows is not supported.
Reporting Bugs
Bugs are tracked on Codeberg. When reporting a bug, please include:
- Operating system and version
- libc implementation (glibc, musl, etc.)
- Compiler and version
- Steps to reproduce the issue
- Any relevant error messages or backtraces
Open an issue at: https://codeberg.org/RealFCC/librealloc/issues
Submitting Patches
See CONTRIBUTING.md for guidelines.
Pull requests: https://codeberg.org/RealFCC/librealloc/pulls
License
GNU Affero General Public License v3.0 or later. See LICENSE.md for the full text.
Copyright (C) 2026 Theron York theron.york@cloudnuke.org