Memory Allocation

group Memory Allocator

Memory allocation subsystem for Chimera-SDK.

This module provides dynamic memory allocation functionality for the memory island. It implements a simple linked-list based allocator that manages heap memory regions.

The allocator uses a first-fit algorithm with a freelist to track available memory blocks. Memory is aligned to 4-byte boundaries for optimal performance.

Defines

ALLOC_ALIGNMENT

Memory alignment value in bytes.

All allocated memory blocks are aligned to this boundary for optimal access performance.

ALLOC_ALIGN(size)

Macro to align a size to the memory alignment boundary.

Parameters:
  • size – Size in bytes to align.

Returns:

Aligned size in bytes.

Functions

void *memory_island_malloc(size_t size)

Allocates memory from the memory island heap.

This function allocates a block of memory of the specified size from the memory island heap. The allocated memory is aligned to ALLOC_ALIGNMENT bytes. The memory is not initialized.

Note

The allocated memory should be freed using memory_island_free() when no longer needed.

Parameters:

size – Number of bytes to allocate.

Returns:

Pointer to the allocated memory block, or NULL if allocation fails.

void memory_island_free(void *ptr)

Frees memory previously allocated by memory_island_malloc().

This function returns a memory block to the free list, making it available for future allocations. The memory block is added to the head of the freelist for efficient reuse.

Note

The pointer must have been returned by a previous call to memory_island_malloc().

Note

After calling this function, the memory pointed to by ptr should not be accessed.

Parameters:

ptr – Pointer to the memory block to free. If NULL, the function does nothing.

void *memory_island_calloc(size_t num, size_t size)

Allocates and zero-initializes memory from the memory island heap.

This function allocates memory for an array of num elements, each of size bytes, and initializes all bytes to zero.

Note

The allocated memory should be freed using memory_island_free() when no longer needed.

Parameters:
  • num – Number of elements to allocate.

  • size – Size of each element in bytes.

Returns:

Pointer to the allocated and zero-initialized memory block, or NULL if allocation fails.

void *memory_island_realloc(void *ptr, size_t size)

Reallocates memory from the memory island heap.

This function changes the size of the memory block pointed to by ptr to size bytes. The contents are unchanged up to the minimum of the old and new sizes. If the new size is larger, the additional memory is uninitialized.

Note

If reallocation fails, the original memory block is unchanged.

Note

The returned pointer may be different from the input pointer.

Parameters:
Returns:

Pointer to the reallocated memory block, or NULL if reallocation fails.

size_t memory_island_heap_size(void)

Gets the total size of the memory island heap.

Returns:

Total heap size in bytes.

size_t memory_island_heap_free(void)

Gets the amount of free memory available in the memory island heap.

This function calculates the total amount of free memory by traversing the freelist and summing the sizes of all free blocks, plus any remaining unallocated heap space.

Returns:

Amount of free memory in bytes.

size_t memory_island_heap_used(void)

Gets the amount of allocated memory in the memory island heap.

Returns:

Amount of allocated memory in bytes.

bool memory_island_ptr_valid(void *ptr)

Checks if a pointer was allocated by the memory island allocator.

This function verifies if the given pointer points to a memory block that was allocated by memory_island_malloc() and is within the heap bounds.

Parameters:

ptr – Pointer to check.

Returns:

true if the pointer is valid and within heap bounds, false otherwise.

bool memory_island_heap_check(void)

Performs a consistency check on the memory island heap.

This function validates the integrity of the heap data structures, including:

  • Freelist consistency

  • Memory block header validity

  • Heap boundary checks

Note

This function is intended for debugging and should not be used in production code due to its performance impact.

Returns:

true if the heap is consistent, false if corruption is detected.

struct MemoryBlock
#include <alloc.h>

Memory block structure for the freelist allocator.

This structure is used internally to track free memory blocks in a linked list. Each allocated block has this header prepended to store metadata.

Public Members

struct MemoryBlock *next

Pointer to the next free block in the list.

size_t size

Size of the memory block in bytes.