infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
Memory Management

APIs for memory management, including custom allocators and arenas. More...

Collaboration diagram for Memory Management:

Classes

struct  infix_allocator_t
 

Macros

#define infix_malloc(n)   infix_allocator.malloc(n)
 A macro that can be defined to override infix's allocator at compile time.
 
#define infix_calloc(nelem, size)   infix_allocator.calloc(nelem, size)
 A macro that can be defined to override infix's allocator at compile time.
 
#define infix_realloc(ptr, n)   infix_allocator.realloc(ptr, n)
 A macro that can be defined to override infix's allocator at compile time.
 
#define infix_free(ptr)   infix_allocator.free(ptr)
 A macro that can be defined to override infix's allocator at compile time.
 
#define infix_memcpy   memcpy
 A macro that can be defined to override the default memcpy function.
 
#define infix_memset   memset
 A macro that can be defined to override the default memset function.
 

Functions

void infix_set_allocator (const infix_allocator_t *allocator)
 Replaces the allocator infix uses for all internal heap allocations.
 
INFIX_API INFIX_NODISCARD infix_arena_tinfix_arena_create (size_t)
 Creates a new memory arena.
 
INFIX_API void infix_arena_destroy (infix_arena_t *)
 Destroys an arena and frees all memory allocated from it.
 
INFIX_API INFIX_NODISCARD void * infix_arena_alloc (infix_arena_t *, size_t, size_t)
 Allocates a block of memory from an arena.
 
INFIX_API INFIX_NODISCARD void * infix_arena_calloc (infix_arena_t *, size_t, size_t, size_t)
 Allocates and zero-initializes a block of memory from an arena.
 

Variables

infix_allocator_t infix_allocator
 The allocator currently used for all of infix's internal heap allocations.
 

Detailed Description

APIs for memory management, including custom allocators and arenas.

Macro Definition Documentation

◆ infix_calloc

#define infix_calloc (   nelem,
  size 
)    infix_allocator.calloc(nelem, size)

A macro that can be defined to override infix's allocator at compile time.

◆ infix_free

#define infix_free (   ptr)    infix_allocator.free(ptr)

A macro that can be defined to override infix's allocator at compile time.

◆ infix_malloc

#define infix_malloc (   n)    infix_allocator.malloc(n)

A macro that can be defined to override infix's allocator at compile time.

infix routes every internal heap allocation through the infix_malloc, infix_calloc, infix_realloc, and infix_free macros. By default they dispatch to the runtime allocator installed with infix_set_allocator().

If you prefer a compile-time override (no indirect call at all), define these macros before including infix.h. You must then define all four consistently, and anything that frees infix-owned memory must use infix_free.

#define infix_malloc my_custom_malloc
#define infix_calloc my_custom_calloc
#define infix_free my_custom_free
#define infix_realloc my_custom_realloc
#include <infix/infix.h>
The public interface for the infix FFI library.

◆ infix_memcpy

#define infix_memcpy   memcpy

A macro that can be defined to override the default memcpy function.

◆ infix_memset

#define infix_memset   memset

A macro that can be defined to override the default memset function.

◆ infix_realloc

#define infix_realloc (   ptr,
 
)    infix_allocator.realloc(ptr, n)

A macro that can be defined to override infix's allocator at compile time.

Function Documentation

◆ infix_arena_alloc()

INFIX_API INFIX_NODISCARD void * infix_arena_alloc ( infix_arena_t arena,
size_t  size,
size_t  alignment 
)

Allocates a block of memory from an arena.

Parameters
[in]arenaThe arena to allocate from.
[in]sizeThe number of bytes to allocate.
[in]alignmentThe required alignment for the allocation. Must be a power of two.
Returns
A pointer to the allocated memory, or nullptr on failure.

◆ infix_arena_calloc()

INFIX_API INFIX_NODISCARD void * infix_arena_calloc ( infix_arena_t arena,
size_t  num,
size_t  size,
size_t  alignment 
)

Allocates and zero-initializes a block of memory from an arena.

Parameters
[in]arenaThe arena to allocate from.
[in]numThe number of elements.
[in]sizeThe size of each element.
[in]alignmentThe required alignment. Must be a power of two.
Returns
A pointer to the zero-initialized memory, or nullptr on failure.

◆ infix_arena_create()

INFIX_API INFIX_NODISCARD infix_arena_t * infix_arena_create ( size_t  initial_size)

Creates a new memory arena.

An arena is a fast, region-based allocator. All objects allocated from it are freed at once when the arena is destroyed. This is the required mechanism for creating types for the Manual API.

Parameters
[in]initial_sizeThe initial capacity of the arena in bytes.
Returns
A pointer to the new arena, or nullptr on failure. The caller must free this with infix_arena_destroy.

◆ infix_arena_destroy()

INFIX_API void infix_arena_destroy ( infix_arena_t arena)

Destroys an arena and frees all memory allocated from it.

Parameters
[in]arenaThe arena to destroy. Safe to call with nullptr.

◆ infix_set_allocator()

void infix_set_allocator ( const infix_allocator_t allocator)

Replaces the allocator infix uses for all internal heap allocations.

Parameters
[in]allocatorA pointer to a valid infix_allocator_t, or NULL to restore the default C library allocator.

infix copies the callback pointers, so the caller may pass a temporary struct. Call this before creating any trampolines (typically during host initialization) and before infix is used from more than one thread, since the table is not internally synchronized.

Every pointer infix returns must be freed through the same allocator: callers that release infix-owned memory (such as the buffer returned by emit_get_binary) must use infix_free or the equivalent entry point of the allocator they installed rather than the C library's free.

Variable Documentation

◆ infix_allocator

infix_allocator_t infix_allocator
extern

The allocator currently used for all of infix's internal heap allocations.

Initialized to the C library's malloc/calloc/realloc/free. Do not assign to it directly. Use infix_set_allocator() so the documentation and any future locking stay in one place.