infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
arena.c File Reference

Implements a simple, fast arena (or region-based) allocator. More...

#include "common/infix_internals.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for arena.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

c23_nodiscard infix_arena_tinfix_arena_create (size_t initial_size)
 Creates a new memory arena.
 
void infix_arena_destroy (infix_arena_t *arena)
 Destroys an arena and frees all memory allocated from it.
 
c23_nodiscard void * infix_arena_alloc (infix_arena_t *arena, size_t size, size_t alignment)
 Allocates a block of memory from an arena.
 
c23_nodiscard void * infix_arena_calloc (infix_arena_t *arena, size_t num, size_t size, size_t alignment)
 Allocates and zero-initializes a block of memory from an arena.
 

Detailed Description

Implements a simple, fast arena (or region-based) allocator.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

Arenas provide a mechanism for fast, grouped memory allocations that can all be freed at once with a single call. This allocation strategy is also known as region-based memory management.

An arena works by pre-allocating a large, contiguous block of memory (the "region"). Subsequent allocation requests are satisfied by simply "bumping" a pointer within this block. This "bump allocation" is extremely fast as it involves only pointer arithmetic and avoids the overhead of system calls (malloc/free) for each small allocation.

This model is used extensively by the infix type system to manage the lifetime of infix_type object graphs. When a type is created from a signature or via the Manual API, all its constituent nodes are allocated from a single arena. When the type is no longer needed, destroying the arena frees all associated memory at once, preventing memory leaks and eliminating the need for complex reference counting or garbage collection.