Skip to content

bustyanimebabesdotcom/arena_alloc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: Unlicense Language Status Version

arena_alloc

Version: 1.0.0
License: The Unlicense
Author: bustyanimebabesdotcom


What is this?

arena_alloc is a minimal, fast, branch-predictable arena allocator for C.
It uses a linear bump pointer and optional rewinding or resetting to allocate memory without malloc overhead, fragmentation, or cache-thrashing.

Perfect for high-performance applications where you allocate a bunch of memory and free it all at once — like parsing, rendering, or any time you want maximum memory control without juggling malloc() around.


Features

  • Branch-predictable with __builtin_expect() on failure paths.
  • 16-byte alignment by default.
  • Cold-path-optimized: no bloated control flow.
  • Zero overhead when preallocating buffers.
  • Rewind or reset the arena at any time.
  • Minimal: 1 .c and 1 .h file.
  • Zero dependencies. Drop-in ready.
  • Works anywhere C99 is supported.

Functions

void   arena_init      ( arena_s *arena, void *buffer, size_t size );
void  *arena_alloc     ( arena_s *arena, size_t size );

size_t arena_checkpoint( arena_s *arena );
void   arena_rewind    ( arena_s *arena, size_t checkpoint );

void   arena_reset     ( arena_s *arena );

size_t arena_used      ( arena_s *arena );
size_t arena_remaining ( arena_s *arena );

Example Usage

#include <stdio.h>
#include <stdlib.h>
#include <arena_alloc/arena_alloc.h>

#define ARENA_SIZE ( 1024 )

int main ( void ) {

	arena_s arena;
	void *buf = malloc( ARENA_SIZE );
	if ( !buf ) return 1;

	arena_init( &arena, buf, ARENA_SIZE );

	char *input = arena_alloc( &arena, 128 );
	if ( fgets( input, 256, stdin )) {
		printf( "You typed: %s", input );
	}

	free(buf);
	return 0;
}

Building

Drop arena_alloc.c and arena_alloc.h into your project.
Or use make if you want a .o or .a file for linking.


License

The Unlicense
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

About

A minimal-overhead, branch-predictable, high performance arena allocator in C.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published