Files
cKit/Memory/c_Allocator.h
T

97 lines
3.2 KiB
C
Raw Normal View History

2026-08-30 01:48:03 +08:00
#ifndef INCLUDED_C_ALLOCATOR_H
#define INCLUDED_C_ALLOCATOR_H
#ifndef INCLUDED_C_TYPES_H
#include <c_Types.h>
#endif /*INCLUDED_C_TYPES_H*/
#ifndef INCLUDED_ASSERT_H
#define INCLUDED_ASSERT_H
#include <assert.h>
#endif /*INCLUDED_ASSERT_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef void* (*c_Allocator_AllocFn_t)(c_size_t nBytes, void* ud);
typedef void* (*c_Allocator_ReallocFn_t)(void* ptr, c_size_t nOldSize, c_size_t nNewSize, void* ud);
typedef void* (*c_Allocator_CallocFn_t)(c_size_t nCount, c_size_t nBytes, void* ud);
typedef void (*c_Allocator_FreeFn_t)(void* ptr, void* ud);
typedef void (*c_Allocator_DtorFn_t)(void* ud);
typedef struct c_Allocator_t{
c_Allocator_AllocFn_t alloc;
c_Allocator_ReallocFn_t realloc;
c_Allocator_CallocFn_t calloc;
c_Allocator_FreeFn_t free;
c_Allocator_DtorFn_t dtor;
void* ud;
}c_Allocator_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#define C_ALLOCATOR_ALLOC_FN(name) void* name(c_size_t nBytes, void* ud)
#define C_ALLOCATOR_REALLOC_FN(name) void* name(void* ptr, c_size_t nOldSize, c_size_t nNewSize, void* ud)
#define C_ALLOCATOR_CALLOC_FN(name) void* name(c_size_t nCount, c_size_t nBytes, void* ud)
#define C_ALLOCATOR_FREE_FN(name) void name(void* ptr, void* ud)
#define C_ALLOCATOR_DTOR_FN(name) void name(void* ud)
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
extern c_Allocator_t c_DefaultAllocator;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
void c_Allocator_Init(c_Allocator_t* self,
const c_Allocator_AllocFn_t alloc_fn,
const c_Allocator_ReallocFn_t realloc_fn,
const c_Allocator_CallocFn_t calloc_fn,
const c_Allocator_FreeFn_t free_fn,
const c_Allocator_DtorFn_t dtor_fn,
void* ud) {
self->alloc = alloc_fn;
self->realloc = realloc_fn;
self->calloc = calloc_fn;
self->free = free_fn;
self->dtor = dtor_fn;
self->ud = ud;
}
C_STATIC_FORCE_INLINE
void* c_Allocator_Alloc(c_Allocator_t* self, c_size_t nBytes) {
if (!self || nBytes==0 || self->alloc==NULL) return NULL;
return self->alloc(nBytes, self->ud);
}
C_STATIC_FORCE_INLINE
void* c_Allocator_Realloc(c_Allocator_t* self, void* ptr, c_size_t nOldSize, c_size_t nNewSize) {
if (!self || !ptr || nNewSize==0 || !self->realloc) return NULL;
return self->realloc(ptr, nOldSize, nNewSize, self->ud);
}
C_STATIC_FORCE_INLINE
void* c_Allocator_Calloc(c_Allocator_t* self, c_size_t count, c_size_t nBytes) {
if (!self || count==0 || nBytes==0 || !self->calloc) return NULL;
return self->calloc(count, nBytes, self->ud);
}
C_STATIC_FORCE_INLINE
void c_Allocator_Free(c_Allocator_t* self, void* ptr) {
if (!self || !ptr || !self->free) return;
self->free(ptr, self->ud);
}
C_STATIC_FORCE_INLINE
void c_Allocator_Destroy(c_Allocator_t* self) {
if (!self || !self->dtor) return;
self->dtor(self->ud);
}
#endif /*INCLUDED_C_ALLOCATOR_H*/