Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
Loading...
Searching...
No Matches
Functions
memory.c File Reference

Implementation of arena allocator and growable array utilities. More...

#include "memory.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdnoreturn.h>
#include <string.h>
#include <stdint.h>
Include dependency graph for memory.c:

Functions

noreturn void die (const char *fmt,...)
 Prints a fatal error message and exits the program.
 
void * xmalloc (size_t size)
 Allocates memory and checks for allocation failure. If !size it will still return a valid pointer that can be freed.
 
void * xcalloc (size_t nr, size_t size)
 Allocates zero-init memory and checks for allocation failure. If !size it will still return a valid pointer.
 
char * xstrdup (const char *str)
 Duplicates a string and checks for allocation failure.
 
Bufferbuffer_create (size_t s, size_t alignment)
 Creates a new aligned buffer for the arena allocator.
 
int arena_allocator_init (ArenaAllocator *arena, size_t initial_size)
 Initializes an ArenaAllocator with a specified initial buffer size.
 
void * arena_alloc (ArenaAllocator *arena, size_t size, size_t alignment)
 Allocates memory from the arena allocator with automatic buffer growth.
 
void * arena_realloc (ArenaAllocator *arena, void *ptr, size_t old_size, size_t new_size, size_t alignment)
 Reallocates memory from the arena allocator.
 
void arena_reset (ArenaAllocator *arena)
 Resets the arena allocator, making all buffers reusable.
 
void arena_destroy (ArenaAllocator *arena)
 Frees all buffers and resets the arena allocator.
 
size_t arena_get_total_allocated (ArenaAllocator *arena)
 Returns the total number of bytes allocated across all buffers.
 
void arena_print_stats (ArenaAllocator *arena)
 Prints statistics about the arena allocator to stderr.
 
char * arena_strdup (ArenaAllocator *arena, const char *src)
 Duplicates a null-terminated string into arena-allocated memory.
 
bool growable_array_init (GrowableArray *arr, ArenaAllocator *arena, size_t initial_capacity, size_t item_size)
 Initializes a GrowableArray backed by an arena allocator.
 
void * growable_array_push (GrowableArray *arr)
 Pushes a new element onto the growable array, resizing if necessary.
 

Detailed Description

Implementation of arena allocator and growable array utilities.

This module implements a memory arena allocator that reduces overhead by allocating large buffers and serving sequential allocations from them. It also provides a growable array type that uses the arena for backing storage.

Features:

See also
memory.h

Function Documentation

◆ arena_alloc()

void * arena_alloc ( ArenaAllocator arena,
size_t  size,
size_t  alignment 
)

Allocates memory from the arena allocator with automatic buffer growth.

Allocates memory from the arena.

Attempts to allocate aligned memory from current buffers. If insufficient space, it moves to the next buffer or allocates a new one.

Large allocations create dedicated buffers.

Parameters
arenaPointer to the ArenaAllocator.
sizeNumber of bytes to allocate.
alignmentMemory alignment requirement.
Returns
Pointer to allocated memory or NULL on failure.

◆ arena_allocator_init()

int arena_allocator_init ( ArenaAllocator arena,
size_t  initial_size 
)

Initializes an ArenaAllocator with a specified initial buffer size.

Initializes an arena allocator.

Allocates the first buffer and sets initial parameters.

Parameters
arenaPointer to the ArenaAllocator to initialize.
initial_sizeInitial buffer size to allocate (will be clamped to minimum size).
Returns
0 on success, -1 on failure.

◆ arena_destroy()

void arena_destroy ( ArenaAllocator arena)

Frees all buffers and resets the arena allocator.

Frees all memory used by the arena.

Parameters
arenaPointer to the ArenaAllocator to destroy.

◆ arena_get_total_allocated()

size_t arena_get_total_allocated ( ArenaAllocator arena)

Returns the total number of bytes allocated across all buffers.

Gets the total number of bytes allocated across all buffers.

Parameters
arenaPointer to the ArenaAllocator.
Returns
Total allocated bytes, or 0 if arena is NULL.

◆ arena_print_stats()

void arena_print_stats ( ArenaAllocator arena)

Prints statistics about the arena allocator to stderr.

Prints statistics about arena memory usage.

Includes number of buffers, total allocated memory, current buffer and offset, and the planned size for the next buffer.

Parameters
arenaPointer to the ArenaAllocator.

◆ arena_realloc()

void * arena_realloc ( ArenaAllocator arena,
void *  ptr,
size_t  old_size,
size_t  new_size,
size_t  alignment 
)

Reallocates memory from the arena allocator.

WARNING: This implementation has limitations due to arena allocator design:

  • Can only shrink or grow the MOST RECENTLY allocated block in the current buffer
  • Cannot reclaim space from shrunk allocations
  • Falls back to allocate-and-copy for other cases
Parameters
arenaPointer to the ArenaAllocator.
ptrPointer to previously allocated memory (can be NULL).
old_sizeSize of the previous allocation (required for validation).
new_sizeNew size requested.
alignmentMemory alignment requirement.
Returns
Pointer to reallocated memory or NULL on failure.

◆ arena_reset()

void arena_reset ( ArenaAllocator arena)

Resets the arena allocator, making all buffers reusable.

Resets the arena, keeping buffers but making them empty.

Does not free memory; resets the current allocation offset and buffer pointer.

Parameters
arenaPointer to the ArenaAllocator to reset.

◆ arena_strdup()

char * arena_strdup ( ArenaAllocator arena,
const char *  src 
)

Duplicates a null-terminated string into arena-allocated memory.

Duplicates a string into arena-managed memory.

Parameters
arenaPointer to the ArenaAllocator.
srcNull-terminated source string.
Returns
Pointer to the duplicated string in arena memory, or NULL on failure.

◆ buffer_create()

Buffer * buffer_create ( size_t  s,
size_t  alignment 
)

Creates a new aligned buffer for the arena allocator.

Creates a new buffer with given size and alignment.

Allocates memory for a Buffer struct plus usable space aligned according to the alignment.

Parameters
sSize of usable memory requested.s
alignmentAlignment requirement for the usable memory.
Returns
Pointer to the new Buffer on success, or NULL on failure.

◆ die()

noreturn void die ( const char *  fmt,
  ... 
)

Prints a fatal error message and exits the program.

Parameters
fmtFormat string for the error message.
...Additional arguments for the format string.

◆ growable_array_init()

bool growable_array_init ( GrowableArray arr,
ArenaAllocator arena,
size_t  initial_capacity,
size_t  item_size 
)

Initializes a GrowableArray backed by an arena allocator.

Initializes a growable array using an arena allocator.

Allocates initial storage for the array and sets metadata.

Parameters
arrPointer to the GrowableArray to initialize.
arenaPointer to the ArenaAllocator for backing storage.
initial_capacityInitial number of elements to allocate space for.
item_sizeSize in bytes of each element.
Returns
true on success, false on failure.

◆ growable_array_push()

void * growable_array_push ( GrowableArray arr)

Pushes a new element onto the growable array, resizing if necessary.

Pushes a new element onto the growable array.

Automatically doubles the array capacity and copies existing elements into new arena memory when full.

Parameters
arrPointer to the GrowableArray.
Returns
Pointer to the newly allocated element slot, or NULL on failure.

◆ xcalloc()

void * xcalloc ( size_t  nr,
size_t  size 
)

Allocates zero-init memory and checks for allocation failure. If !size it will still return a valid pointer.

Parameters
nrNumber of elements to allocate.
sizeSize of each element.
Returns
Pointer to the allocated memory on success.

◆ xmalloc()

void * xmalloc ( size_t  size)

Allocates memory and checks for allocation failure. If !size it will still return a valid pointer that can be freed.

Parameters
sizeNumber of bytes to allocate.
Returns
Pointer to the allocated memory on success.

◆ xstrdup()

char * xstrdup ( const char *  str)

Duplicates a string and checks for allocation failure.

Parameters
strThe string to duplicate.
Returns
Pointer to the duplicated string on success.