Files
cKit/Memory/c_PoolAllocator.h
2026-08-30 01:48:03 +08:00

87 lines
2.3 KiB
C

#ifndef INCLUDED_C_POOLALLOCATOR_H
#define INCLUDED_C_POOLALLOCATOR_H
#ifndef INCLUDED_C_ALLOCATOR_H
#include <c_Allocator.h>
#endif /*INCLUDED_C_ALLOCATOR_H*/
#ifndef INCLUDED_C_POOL_H
#include <c_Pool.h>
#endif /*INCLUDED_C_POOL_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
c_Pool_t* pool;
}c_PoolAllocator_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
void c_PoolAllocator_Init(c_PoolAllocator_t* self, c_Pool_t* pool) {
self->pool = pool;
}
C_STATIC_FORCE_INLINE
void c_PoolAllocator_Destroy(c_PoolAllocator_t* self) {
c_Pool_Destroy(self->pool);
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
C_ALLOCATOR_ALLOC_FN(c_PoolAllocator_Alloc) {
C_UNUSED(nBytes);
c_PoolAllocator_t* self = (c_PoolAllocator_t*)ud;
if (!self || !self->pool) return NULL;
return c_Pool_Alloc(self->pool);
}
C_STATIC_FORCE_INLINE
C_ALLOCATOR_REALLOC_FN(c_PoolAllocator_Realloc) {
c_PoolAllocator_t* self = (c_PoolAllocator_t*)ud;
if (!self || !self->pool) return NULL;
assert(nNewSize==self->pool->objSize);
if (!ptr) {
return c_Pool_Alloc(self->pool);
}
return ptr;
}
C_STATIC_FORCE_INLINE
C_ALLOCATOR_FREE_FN(c_PoolAllocator_Free) {
c_PoolAllocator_t* self = (c_PoolAllocator_t*)ud;
if (!self || !self->pool) return;
c_Pool_Free(self->pool, ptr);
}
C_STATIC_FORCE_INLINE
C_ALLOCATOR_DTOR_FN(c_PoolAllocator_Dtor) {
c_PoolAllocator_t* self = (c_PoolAllocator_t*)ud;
if (!self || !self->pool) return;
c_Pool_Destroy(self->pool);
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
c_Allocator_t c_PoolAllocator_Build(c_PoolAllocator_t* self, c_Pool_t* pool) {
c_Allocator_t allocator;
self->pool = pool;
allocator.ud = self;
allocator.alloc = c_PoolAllocator_Alloc;
allocator.realloc = c_PoolAllocator_Realloc;
allocator.free = c_PoolAllocator_Free;
allocator.calloc = 0;
allocator.dtor = c_PoolAllocator_Dtor;
return allocator;
}
#endif /*INCLUDED_C_POOLALLOCATOR_H*/