CNum 0.2.1
CPU-optimized ML library for C++
Loading...
Searching...
No Matches
Arena.h File Reference
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  linked_list_node
 A node in a linked list. More...
struct  arena
 A thread local mini "heap" used for thread local memory allocation. More...
struct  arena_view
 A view of memory that was allocated in an arena. More...

Macros

#define ARENA_BLOCK_SIZE   64
 The block size of the arena.

Typedefs

typedef struct arena arena_t
typedef struct arena_view arena_view_t

Functions

arena_tarena_init (uint32_t blocks_to_allocate)
 Initialize Arena.
arena_view_t arena_malloc (arena_t *arena, size_t bytes, size_t type_size)
 Arena manual allocation with malloc fallback.
void arena_view_reset (arena_view_t arena_view)
 Free memory associated with an arena_view (within the arena itself).
void arena_clear (arena_t *arena)
 Zero out an arena's memory.
void arena_free (arena_t *arena)
 Free all memory associated with an arena.

Macro Definition Documentation

◆ ARENA_BLOCK_SIZE

#define ARENA_BLOCK_SIZE   64

The block size of the arena.

Used for detirmining padding of allocated memory. It is best to keep this the size of a cache line generally

Typedef Documentation

◆ arena_t

typedef struct arena arena_t

◆ arena_view_t

typedef struct arena_view arena_view_t

Function Documentation

◆ arena_clear()

void arena_clear ( arena_t * arena)

Zero out an arena's memory.

This function does not use memset on the entire range of the arena's memory, but rather zeros the memory up to the internal pointer that keeps track of where the in the arena's memory the next allocation would take place. As this arena's implementation is intentionally simple, allocations are linear, so the memory behind this pointer is all of the previously allocated blocks.

Parameters
arenaPointer to an arena

◆ arena_free()

void arena_free ( arena_t * arena)

Free all memory associated with an arena.

◆ arena_init()

arena_t * arena_init ( uint32_t blocks_to_allocate)

Initialize Arena.

Parameters
blocks_to_allocateThe size of the arena in blocks
Returns
A pointer to an arena

◆ arena_malloc()

arena_view_t arena_malloc ( arena_t * arena,
size_t bytes,
size_t type_size )

Arena manual allocation with malloc fallback.

This function attempts to allocated memory in the heap and return a view that memory, but in the case that the arena is out of space will fall back on the standard malloc, but still return an arena_view_t

Parameters
arenaPointer to an arena
bytesThe number of bytes to allocate
type_sizeThe size of the data type being stored (for the view)
Returns
A view of the allocated memory

◆ arena_view_reset()

void arena_view_reset ( arena_view_t arena_view)

Free memory associated with an arena_view (within the arena itself).