Memory API
The memory module implements efficient memory management systems, with a focus on the Arena Allocator.
Arena Allocator
The Arena Allocator is a powerful, region-based memory allocator that simplifies memory management in C applications.
Types
-
type myrtx_arena_t
Opaque structure representing an arena allocator.
typedef struct myrtx_arena { // Internal details } myrtx_arena_t;
-
type myrtx_scratch_arena_t
Container for temporary arena memory regions with automatic reset.
typedef struct myrtx_scratch_arena { myrtx_arena_t* arena; // Arena being used myrtx_arena_t* parent; // Parent arena, if any size_t marker; // Position for reset } myrtx_scratch_arena_t;
Initialization and Cleanup
-
bool myrtx_arena_init(myrtx_arena_t *arena, size_t block_size)
Initializes an arena allocator.
- Parameters:
arena – Pointer to an uninitialized arena structure
block_size – Size of each memory block in bytes (0 for default size)
- Returns:
true on success, false on error
-
void myrtx_arena_reset(myrtx_arena_t *arena)
Resets an arena by freeing all allocated memory blocks.
- Parameters:
arena – Pointer to an initialized arena
-
void myrtx_arena_free(myrtx_arena_t *arena)
Frees all resources used by the arena.
- Parameters:
arena – Pointer to an initialized arena
Memory Allocation
-
void *myrtx_arena_alloc(myrtx_arena_t *arena, size_t size)
Allocates memory from the arena.
- Parameters:
arena – Pointer to an initialized arena
size – Number of bytes to allocate
- Returns:
Pointer to the allocated memory or NULL on error
-
void *myrtx_arena_calloc(myrtx_arena_t *arena, size_t size)
Allocates memory from the arena and initializes it to zero.
- Parameters:
arena – Pointer to an initialized arena
size – Number of bytes to allocate
- Returns:
Pointer to the allocated memory or NULL on error
-
void *myrtx_arena_realloc(myrtx_arena_t *arena, void *ptr, size_t old_size, size_t new_size)
Resizes a previously allocated memory block.
- Parameters:
arena – Pointer to an initialized arena
ptr – Pointer to previously allocated memory
old_size – Size of the previously allocated memory block
new_size – New size in bytes
- Returns:
Pointer to the newly allocated memory or NULL on error
-
void *myrtx_arena_alloc_aligned(myrtx_arena_t *arena, size_t size, size_t alignment)
Allocates aligned memory from the arena.
- Parameters:
arena – Pointer to an initialized arena
size – Number of bytes to allocate
alignment – Alignment in bytes (must be a power of two)
- Returns:
Pointer to the allocated aligned memory or NULL on error
Temporary Arenas
-
size_t myrtx_arena_temp_begin(myrtx_arena_t *arena)
Marks the current state of an arena for temporary use.
- Parameters:
arena – Pointer to an initialized arena
- Returns:
Marker for the current position
-
void myrtx_arena_temp_end(myrtx_arena_t *arena, size_t marker)
Resets an arena to a previously marked state.
- Parameters:
arena – Pointer to an initialized arena
marker – Marker position returned by myrtx_arena_temp_begin
Scratch Arenas
-
void myrtx_scratch_begin(myrtx_scratch_arena_t *scratch, myrtx_arena_t *parent)
Begins a scratch arena session with an existing arena.
- Parameters:
scratch – Pointer to an uninitialized scratch arena structure
parent – Pointer to a parent arena (or NULL for a new arena)
-
void myrtx_scratch_end(myrtx_scratch_arena_t *scratch)
Ends a scratch arena session and frees temporary memory.
- Parameters:
scratch – Pointer to an initialized scratch arena structure
Helper Functions
-
char *myrtx_arena_strdup(myrtx_arena_t *arena, const char *str)
Duplicates a string into arena memory.
- Parameters:
arena – Pointer to an initialized arena
str – Null-terminated string to duplicate
- Returns:
Pointer to the duplicated string or NULL on error
-
char *myrtx_arena_strndup(myrtx_arena_t *arena, const char *str, size_t n)
Duplicates the first n characters of a string into arena memory.
- Parameters:
arena – Pointer to an initialized arena
str – String to duplicate
n – Maximum number of characters to duplicate
- Returns:
Pointer to the duplicated string or NULL on error
-
void *myrtx_arena_memdup(myrtx_arena_t *arena, const void *ptr, size_t size)
Duplicates a memory block into arena memory.
- Parameters:
arena – Pointer to an initialized arena
ptr – Memory block to duplicate
size – Size of the memory block in bytes
- Returns:
Pointer to the duplicated memory block or NULL on error
Statistics and Information
-
size_t myrtx_arena_total_allocated(const myrtx_arena_t *arena)
Returns the total size of memory allocated by an arena.
- Parameters:
arena – Pointer to an initialized arena
- Returns:
Total size in bytes