1
0
Fork
You've already forked librealloc
0
A portable, libre heap allocator replacement for malloc/free with built-in safety checks. Detects buffer overflows, double frees, invalid frees, and memory leaks.
  • C 90.8%
  • Makefile 9.2%
Find a file
RealFCC 196a84947a
leak: replace O(n) linked-list tracker with open-addressing hash table
- 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.
2026年06月30日 13:17:35 -05:00
docs librealloc v1.0-rc1: portable heap allocator with safety features 2026年06月27日 17:51:15 -05:00
examples Revert "Restructure allocator to glibc-style ptmalloc arena" 2026年06月30日 01:17:45 -05:00
include Revert "Restructure allocator to glibc-style ptmalloc arena" 2026年06月30日 01:17:45 -05:00
src leak: replace O(n) linked-list tracker with open-addressing hash table 2026年06月30日 13:17:35 -05:00
.gitignore librealloc v1.0-rc1: portable heap allocator with safety features 2026年06月27日 17:51:15 -05:00
CLAUDE.md librealloc v1.0-rc1: portable heap allocator with safety features 2026年06月27日 17:51:15 -05:00
CONTRIBUTING.md librealloc v1.0-rc1: portable heap allocator with safety features 2026年06月27日 17:51:15 -05:00
LICENSE librealloc v1.0-rc1: portable heap allocator with safety features 2026年06月27日 17:51:15 -05:00
Makefile leak: replace O(n) linked-list tracker with open-addressing hash table 2026年06月30日 13:17:35 -05:00
README.md chore: Fixed the docs 2026年06月28日 10:49:42 -05:00

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

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

  1. Buffer overflow detection - Canaries before and after user data. Detected when the block is freed. Program aborts with file and line number.
  2. Double-free prevention - Blocks track allocation state. Second free causes immediate abort with source location.
  3. Invalid free detection - Magic number validation on every free. Stack pointers and garbage addresses are caught.
  4. 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