memory_arena
Loading...
Searching...
No Matches
Functions
arena_calloc.c File Reference

Zero-initialized memory allocation functions for arenas. More...

#include "arena.h"
#include <string.h>
+ Include dependency graph for arena_calloc.c:

Go to the source code of this file.

Functions

void * arena_calloc (t_arena *arena, size_t count, size_t size)
 
void * arena_calloc_aligned (t_arena *arena, size_t count, size_t size, size_t alignment)
 
void * arena_calloc_aligned_labeled (t_arena *arena, size_t count, size_t size, size_t alignment, const char *label)
 
void * arena_calloc_labeled (t_arena *arena, size_t count, size_t size, const char *label)
 
static void arena_record_failed_alloc (t_arena *arena)
 Increment the arena's failed allocation counter in a thread-safe manner.
 
static bool validate_arena_calloc_input (t_arena *arena, size_t count, size_t size)
 Validate input parameters for a calloc-style arena allocation.
 

Detailed Description

Zero-initialized memory allocation functions for arenas.

Author
Toonsa
Date
2025/04/24

This file implements calloc-style memory allocation within a memory arena. It provides functions to allocate memory blocks that are automatically zero-initialized, with optional support for alignment and debug labeling.

These functions are analogous to calloc in the C standard library, but operate on arenas to enable scoped, fast allocations without direct use of the heap.

Supported use cases:

Internal helpers are used to validate parameters, detect overflows, and track failed allocation attempts in a thread-safe manner.

Note
These functions are part of the public allocation API, but depend on internal helpers for safety checks and statistics updates.

Definition in file arena_calloc.c.

Function Documentation

◆ arena_calloc()

void * arena_calloc ( t_arena *  arena,
size_t  count,
size_t  size 
)

Definition at line 96 of file arena_calloc.c.

97{
98 return arena_calloc_labeled(arena, count, size, "arena_calloc_zero");
99}
void * arena_calloc_labeled(t_arena *arena, size_t count, size_t size, const char *label)

References arena_calloc_labeled().

◆ arena_calloc_aligned()

void * arena_calloc_aligned ( t_arena *  arena,
size_t  count,
size_t  size,
size_t  alignment 
)

Definition at line 148 of file arena_calloc.c.

149{
150 return arena_calloc_aligned_labeled(arena, count, size, alignment, "arena_calloc_zero");
151}
void * arena_calloc_aligned_labeled(t_arena *arena, size_t count, size_t size, size_t alignment, const char *label)

References arena_calloc_aligned_labeled().

◆ arena_calloc_aligned_labeled()

void * arena_calloc_aligned_labeled ( t_arena *  arena,
size_t  count,
size_t  size,
size_t  alignment,
const char *  label 
)

Definition at line 267 of file arena_calloc.c.

268{
269 if (!label)
270 label = "arena_calloc_zero";
271
272 if (!validate_arena_calloc_input(arena, count, size))
273 {
274 if (arena)
276 return NULL;
277 }
278
279 size_t total;
280 if (would_overflow_mul(count, size, &total))
281 {
283 arena_report_error(arena,
284 "arena_calloc failed: multiplication overflow "
285 "(count = %zu, size = %zu)",
286 count, size);
287 return NULL;
288 }
289
290 return arena_alloc_internal(arena, total, alignment, label);
291}
static void arena_record_failed_alloc(t_arena *arena)
Increment the arena's failed allocation counter in a thread-safe manner.
static bool validate_arena_calloc_input(t_arena *arena, size_t count, size_t size)
Validate input parameters for a calloc-style arena allocation.
void * arena_alloc_internal(t_arena *arena, size_t size, size_t alignment, const char *label)
Core internal allocator with alignment and labeling support.
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
static bool would_overflow_mul(size_t a, size_t b, size_t *result)
Perform a checked multiplication of two size_t values.
Definition arena_math.h:79

References arena_alloc_internal(), arena_record_failed_alloc(), arena_report_error(), validate_arena_calloc_input(), and would_overflow_mul().

◆ arena_calloc_labeled()

void * arena_calloc_labeled ( t_arena *  arena,
size_t  count,
size_t  size,
const char *  label 
)

Definition at line 203 of file arena_calloc.c.

204{
205 return arena_calloc_aligned_labeled(arena, count, size, ARENA_DEFAULT_ALIGNMENT, label);
206}
#define ARENA_DEFAULT_ALIGNMENT
Default alignment value supported.

References arena_calloc_aligned_labeled(), and ARENA_DEFAULT_ALIGNMENT.

◆ arena_record_failed_alloc()

static void arena_record_failed_alloc ( t_arena *  arena)
inlinestatic

Increment the arena's failed allocation counter in a thread-safe manner.

This internal utility increments the failed_allocations field in the arena’s statistics structure. It is used to track the number of failed allocation attempts (due to invalid inputs, overflows, or out-of-memory conditions).

The function uses locking (via ARENA_LOCK / ARENA_UNLOCK) to ensure thread safety when statistics are modified, if thread safety is enabled.

If the arena pointer is NULL, the function exits silently.

Parameters
arenaPointer to the t_arena whose statistics should be updated.
Note
This function is typically called from within allocation functions that detect errors before allocating memory.
See also
validate_arena_calloc_input
arena_calloc_aligned_labeled
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_calloc.c.

Definition at line 375 of file arena_calloc.c.

376{
377 if (!arena)
378 return;
379 ARENA_LOCK(arena);
380 arena->stats.failed_allocations++;
381 ARENA_UNLOCK(arena);
382}
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.

References ARENA_LOCK, and ARENA_UNLOCK.

◆ validate_arena_calloc_input()

static bool validate_arena_calloc_input ( t_arena *  arena,
size_t  count,
size_t  size 
)
inlinestatic

Validate input parameters for a calloc-style arena allocation.

This internal helper verifies that the parameters provided to arena_calloc and its variants are valid before attempting memory allocation.

Specifically, it ensures:

  • The arena pointer is not NULL.
  • The count of elements to allocate is non-zero.
  • The size of each element is non-zero.

If any condition fails, the function reports a descriptive error using arena_report_error and returns false. Otherwise, it returns true.

Parameters
arenaPointer to the arena being used for allocation.
countNumber of elements to allocate.
sizeSize (in bytes) of each element.
Returns
true if all inputs are valid, false otherwise.
See also
arena_calloc
arena_calloc_aligned
arena_calloc_labeled
arena_calloc_aligned_labeled
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_calloc.c.

Definition at line 326 of file arena_calloc.c.

327{
328 if (!arena)
329 {
330 arena_report_error(NULL, "arena_calloc failed: NULL arena provided");
331 return false;
332 }
333
334 if (count == 0)
335 {
336 arena_report_error(arena, "arena_calloc failed: zero count (count = %zu)", count);
337 return false;
338 }
339
340 if (size == 0)
341 {
342 arena_report_error(arena, "arena_calloc failed: zero element size (size = %zu)", size);
343 return false;
344 }
345
346 return true;
347}

References arena_report_error().