|
Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
|
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>
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. | |
| Buffer * | buffer_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. | |
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:
| 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.
| arena | Pointer to the ArenaAllocator. |
| size | Number of bytes to allocate. |
| alignment | Memory alignment requirement. |
| 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.
| arena | Pointer to the ArenaAllocator to initialize. |
| initial_size | Initial buffer size to allocate (will be clamped to minimum size). |
| void arena_destroy | ( | ArenaAllocator * | arena | ) |
Frees all buffers and resets the arena allocator.
Frees all memory used by the arena.
| arena | Pointer to the ArenaAllocator to destroy. |
| 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.
| arena | Pointer to the ArenaAllocator. |
| 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.
| arena | Pointer to the ArenaAllocator. |
| 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:
| arena | Pointer to the ArenaAllocator. |
| ptr | Pointer to previously allocated memory (can be NULL). |
| old_size | Size of the previous allocation (required for validation). |
| new_size | New size requested. |
| alignment | Memory alignment requirement. |
| 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.
| arena | Pointer to the ArenaAllocator to reset. |
| char * arena_strdup | ( | ArenaAllocator * | arena, |
| const char * | src | ||
| ) |
Duplicates a null-terminated string into arena-allocated memory.
Duplicates a string into arena-managed memory.
| arena | Pointer to the ArenaAllocator. |
| src | Null-terminated source string. |
| 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.
| s | Size of usable memory requested.s |
| alignment | Alignment requirement for the usable memory. |
| noreturn void die | ( | const char * | fmt, |
| ... | |||
| ) |
Prints a fatal error message and exits the program.
| fmt | Format string for the error message. |
| ... | Additional arguments for the format string. |
| 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.
| arr | Pointer to the GrowableArray to initialize. |
| arena | Pointer to the ArenaAllocator for backing storage. |
| initial_capacity | Initial number of elements to allocate space for. |
| item_size | Size in bytes of each element. |
| 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.
| arr | Pointer to the GrowableArray. |
| 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.
| nr | Number of elements to allocate. |
| size | Size of each element. |
| 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.
| size | Number of bytes to allocate. |
| char * xstrdup | ( | const char * | str | ) |
Duplicates a string and checks for allocation failure.
| str | The string to duplicate. |