BFC-Compiler
A C23 Brainfuck compiler
Loading...
Searching...
No Matches
bfc_memory.c File Reference

Checked array-allocation implementation. More...

#include "bfc_memory.h"
#include <stdckdint.h>
#include <stdlib.h>

Functions

void * bfc_malloc_array (size_t count, size_t element_size)
 Allocates an uninitialized typed array after checked size multiplication.
void * bfc_calloc_array (size_t count, size_t element_size)
 Allocates a zero-filled typed array after checked size multiplication.
void * bfc_realloc_array (void *allocation, size_t count, size_t element_size)
 Resizes a typed array after checked size multiplication.

Detailed Description

Checked array-allocation implementation.

Uses C23 checked arithmetic to reject element-count multiplication overflow before allocation.

Function Documentation

◆ bfc_calloc_array()

void * bfc_calloc_array ( size_t count,
size_t element_size )

Allocates a zero-filled typed array after checked size multiplication.

Allocates a zero-initialized array after checking count multiplication.

Allocates a zero-filled typed array after checked size multiplication.

Parameters
[in]countNumber of elements.
[in]element_sizeSize of each element in bytes.
Returns
A zero-initialized block, or nullptr on overflow or allocation failure.
Note
The returned allocation must be released with free().

◆ bfc_malloc_array()

void * bfc_malloc_array ( size_t count,
size_t element_size )

Allocates an uninitialized typed array after checked size multiplication.

Allocates an uninitialized array after checking count multiplication.

Allocates an uninitialized typed array after checked size multiplication.

Parameters
[in]countNumber of elements.
[in]element_sizeSize of each element in bytes.
Returns
A newly allocated block, or nullptr on overflow or allocation failure.
Note
The returned allocation must be released with free().

◆ bfc_realloc_array()

void * bfc_realloc_array ( void * allocation,
size_t count,
size_t element_size )

Resizes a typed array after checked size multiplication.

Resizes a typed array after checking count multiplication.

Resizes a typed array after checked size multiplication.

Parameters
[in,out]allocationExisting allocation, or nullptr.
[in]countRequested number of elements.
[in]element_sizeSize of each element in bytes.
Returns
The resized block, or nullptr on overflow or allocation failure.
Note
On failure, the original allocation remains valid.