cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
mem_allocator.h
Go to the documentation of this file.
1
49
50#ifndef MEM_ALLOCATOR_H
51#define MEM_ALLOCATOR_H
52
53#include "base/base_macros.h"
54#include "base/base_types.h"
55#include "memory/mem_arena.h"
56
58
63
70typedef void *(*AllocFn)(void *ctx, usize size);
71
83typedef void (*FreeFn)(void *ctx, void *ptr, usize size);
84
97typedef void *(*ReallocFn)(void *ctx, void *ptr, usize old_size, usize new_size);
98
114
115// Dispatch helpers
116
123function inline void *
125{
126 ASSERT(a != NULL && a->alloc != NULL && "allocator_alloc: invalid allocator");
127 return a->alloc(a->ctx, size);
128}
129
139function inline void
140allocator_free(Allocator *a, void *ptr, usize size)
141{
142 ASSERT(a != NULL && "allocator_free: invalid allocator");
143 if (a->free != NULL) {
144 a->free(a->ctx, ptr, size);
145 }
146}
147
159function inline void *
160allocator_realloc(Allocator *a, void *ptr, usize old_size, usize new_size)
161{
162 ASSERT(a != NULL && a->alloc != NULL && "allocator_realloc: invalid allocator");
163 if (a->realloc != NULL) {
164 return a->realloc(a->ctx, ptr, old_size, new_size);
165 }
166 // Fallback: fresh alloc + copy.
167 void *new_ptr = a->alloc(a->ctx, new_size);
168 if (new_ptr != NULL && ptr != NULL) {
169 MEM_COPY(new_ptr, ptr, MIN(old_size, new_size));
170 }
171 allocator_free(a, ptr, old_size);
172 return new_ptr;
173}
174
175// Arena Allocator Factory
176
178function inline void *
179_arena_alloc_fn(void *ctx, usize size)
180{
181 return arena_push_zero((Arena *)ctx, size);
182}
183
184function inline void
185_arena_free_fn(void *ctx, void *ptr, usize size)
186{
187 (void)ctx;
188 (void)ptr;
189 (void)size;
190}
191
192function inline void *
193_arena_realloc_fn(void *ctx, void *ptr, usize old_size, usize new_size)
194{
195 void *new_ptr = arena_push_zero((Arena *)ctx, new_size);
196 if (new_ptr != NULL && ptr != NULL) {
197 MEM_COPY(new_ptr, ptr, MIN(old_size, new_size));
198 }
199 return new_ptr;
200}
202
219function inline Allocator
221{
222 Allocator a;
223 a.ctx = (void *)arena;
224 a.alloc = _arena_alloc_fn;
225 a.free = _arena_free_fn;
226 a.realloc = _arena_realloc_fn;
227 return a;
228}
229 // end allocator_interface
231
233
234#endif // MEM_ALLOCATOR_H
Context detection, utilities, compiler abstractions, and data structures.
Core type definitions and fixed-width aliases.
function void * allocator_realloc(Allocator *a, void *ptr, usize old_size, usize new_size)
Resizes a previously allocated block through the allocator.
function void * allocator_alloc(Allocator *a, usize size)
Allocates size zero-initialized bytes through the allocator.
function void allocator_free(Allocator *a, void *ptr, usize size)
Frees a previously allocated block through the allocator.
void *(* ReallocFn)(void *ctx, void *ptr, usize old_size, usize new_size)
Function pointer type for a realloc operation.
void *(* AllocFn)(void *ctx, usize size)
Function pointer type for an allocation operation.
void(* FreeFn)(void *ctx, void *ptr, usize size)
Function pointer type for a free operation.
function Allocator arena_allocator(Arena *arena)
Constructs an Allocator interface backed by an existing Arena.
void * arena_push_zero(Arena *arena, usize size)
Allocates size bytes from the arena, zeroed, aligned to ARENA_DEFAULT_ALIGN.
Definition mem_arena.c:89
#define ASSERT(expr)
Standard runtime assertion. Triggers a debug break on failure.
#define function
File-scoped function (invisible to the linker).
#define C_LINKAGE_END
Closes a C linkage block for C++ compilers (Empty in C).
#define C_LINKAGE_BEGIN
Opens a C linkage block for C++ compilers (Empty in C).
#define MEM_COPY(dst, src, size)
Wraps memmove for standard memory copying.
size_t usize
Definition base_types.h:43
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).
High-performance Virtual Memory Linear (Arena) Allocator.
A generic allocator backed by function pointers (vtable).
AllocFn alloc
ReallocFn realloc
Represents a linear memory arena backed by OS virtual memory.
Definition mem_arena.h:83