This commit is contained in:
2026-08-30 01:48:03 +08:00
parent 84ebd52c24
commit 7940b67827
170 changed files with 2704 additions and 21276 deletions
+51
View File
@@ -0,0 +1,51 @@
#include <c_Allocator.h>
#include <stdlib.h>
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
void* c_DefaultAllocator_Alloc(c_size_t nBytes, void* ud) {
C_UNUSED(ud);
return malloc(nBytes);
}
C_STATIC_FORCE_INLINE
C_ALLOCATOR_REALLOC_FN(c_DefaultAllocator_Realloc) {
C_UNUSED(ud);
C_UNUSED(nOldSize);
return realloc(ptr, nNewSize);
}
C_STATIC_FORCE_INLINE
void* c_DefaultAllocator_Calloc(c_size_t nCount, c_size_t nBytes, void* ud) {
C_UNUSED(ud);
return calloc(nCount, nBytes);
}
C_STATIC_FORCE_INLINE
void c_DefaultAllocator_Free(void* ptr, void* ud) {
C_UNUSED(ud);
if (ptr) {
free(ptr);
}
}
C_STATIC_FORCE_INLINE
void c_DefaultAllocator_Dtor(void* ud) {
C_UNUSED(ud);
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
c_Allocator_t c_DefaultAllocator=(c_Allocator_t){
c_DefaultAllocator_Alloc,
c_DefaultAllocator_Realloc,
c_DefaultAllocator_Calloc,
c_DefaultAllocator_Free,
c_DefaultAllocator_Dtor,
0
};
+96
View File
@@ -0,0 +1,96 @@
#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*/
+1
View File
@@ -0,0 +1 @@
#include <c_BuddyAllocator.h>
+96
View File
@@ -0,0 +1,96 @@
#ifndef INCLUDED_C_BUDDYALLOCATOR_H
#define INCLUDED_C_BUDDYALLOCATOR_H
#ifndef INCLUDED_C_BUDDY_H
#include <c_Buddy.h>
#endif /*INCLUDED_C_BUDDY_H*/
#ifndef INCLUDED_C_ALLOCATOR_H
#include <c_Allocator.h>
#endif /*INCLUDED_C_ALLOCATOR_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
c_Buddy_t* buddy;
}c_BuddyAllocator_t;
C_STATIC_FORCE_INLINE
void c_BuddyAllocator_Init(c_BuddyAllocator_t* allocator, c_Buddy_t* buddy) {
allocator->buddy = buddy;
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
C_ALLOCATOR_ALLOC_FN(c_BuddyAllocator_Alloc) {
c_BuddyAllocator_t* self = (c_BuddyAllocator_t*)ud;
if (!self || !self->buddy) return NULL;
return c_Buddy_Alloc(self->buddy, (int)nBytes);
}
C_STATIC_FORCE_INLINE
C_ALLOCATOR_FREE_FN(c_BuddyAllocator_Free) {
c_BuddyAllocator_t* self = (c_BuddyAllocator_t*)ud;
if (!self || !self->buddy) return;
c_Buddy_Free(self->buddy, ptr);
}
C_STATIC_FORCE_INLINE
C_ALLOCATOR_REALLOC_FN(c_BuddyAllocator_Realloc) {
c_BuddyAllocator_t* self = (c_BuddyAllocator_t*)ud;
if (!self || !self->buddy) return NULL;
// 情况 1: 指针为空,直接分配新内存
if (!ptr) {
return c_Buddy_Alloc(self->buddy, (int)nNewSize);
}
// 情况 2: 新大小为 0,直接释放内存
if (nNewSize==0) {
c_Buddy_Free(self->buddy, ptr);
return NULL;
}
// 核心优化:利用 Header 逆向获取该块在底层伙伴系统中的真实物理大小
const c_BuddyBlock_t *block = (c_BuddyBlock_t *) ((uint8_t *) ptr - self->buddy->alignment);
const int actual_block_size = block->size;
// 【就地复用判定】:如果新申请的大小没有超过当前伙伴块的物理承载上限,并且新大小不至于小到需要缩容割裂
// 直接返回原指针,零拷贝,零内存搬迁!
if ((int)nNewSize <= actual_block_size && (int)nNewSize > (actual_block_size / 2)) {
return ptr;
}
// 情况 3: 伙伴块大小不匹配(需要升级或降级阶数),申请新块
void* new_ptr = c_Buddy_Alloc(self->buddy, (int)nNewSize);
if (!new_ptr) return NULL;
// 计算安全的拷贝长度
const c_size_t copy_size = ((c_size_t)actual_block_size < nNewSize) ? (c_size_t)actual_block_size : nNewSize;
// 数据迁移
memcpy(new_ptr, ptr, copy_size);
// 释放旧伙伴块
c_Buddy_Free(self->buddy, ptr);
return new_ptr;
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE
c_Allocator_t c_BuddyAllocator_Build(c_BuddyAllocator_t* self, c_Buddy_t* buddy) {
c_BuddyAllocator_Init(self, buddy);
c_Allocator_t allocator={0};
allocator.ud = self;
allocator.alloc = c_BuddyAllocator_Alloc;
allocator.free = c_BuddyAllocator_Free;
allocator.realloc = c_BuddyAllocator_Realloc;
return allocator;
}
#endif /*INCLUDED_C_BUDDYALLOCATOR_H*/
+1
View File
@@ -0,0 +1 @@
#include <c_PoolAllocator.h>
+86
View File
@@ -0,0 +1,86 @@
#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*/