/****************************************************************************** 版权所有 (C), 2018-2099, Radkil ****************************************************************************** 文 件 名 : rd_mempool.h 版 本 号 : 初稿 作 者 : Radkil 生成日期 : 2026年3月10日星期二 最近修改 : 功能描述 : rd_mempool.c 的头文件 修改历史 : 1.日 期 : 2026年3月10日星期二 作 者 : Radkil 修改内容 : 创建文件 ******************************************************************************/ /*----------------------------------------------* * 外部变量说明 * *----------------------------------------------*/ /*----------------------------------------------* * 外部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 内部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 全局变量 * *----------------------------------------------*/ /*----------------------------------------------* * 模块级变量 * *----------------------------------------------*/ /*----------------------------------------------* * 常量定义 * *----------------------------------------------*/ /*----------------------------------------------* * 宏定义 * *----------------------------------------------*/ #ifndef __RD_MEMPOOL_H__ #define __RD_MEMPOOL_H__ #ifdef __cplusplus #if __cplusplus extern "C"{ #endif #endif /* __cplusplus */ /*==============================================* * include header files * *----------------------------------------------*/ #include #include #include #include "common.h" /*==============================================* * constants or macros define * *----------------------------------------------*/ // 【关键宏】使能此宏将把内存池分配到堆上,否则在全局区(类似栈/静态区) // #define USE_HEAP_FOR_MEMPOOL // 1. 内存池总大小 #ifndef MEM_POOL_TOTAL_SIZE #define MEM_POOL_TOTAL_SIZE (800 * 1024) #endif // 2. 最小分配单元 (必须满足对齐要求,通常 8 或 16 字节) // 包含 Header 的最小尺寸,防止切分出太小的碎片 #define MIN_ALLOC_UNIT 16 #ifdef __RTTHREAD__ #include #define MP_MALLOC rt_malloc #define MP_CALLOC rt_calloc #define MP_REALLOC rt_realloc #define MP_FREE rt_free #define MP_MEMCPY rt_memcpy #define MP_MEMSET rt_memset // ... RT 实现 #else #include #define MP_MALLOC malloc #define MP_CALLOC calloc #define MP_REALLOC realloc #define MP_FREE free #include #define MP_MEMCPY memcpy #define MP_MEMSET memset // ... Linux 实现 #endif /*==============================================* * project-wide global variables * *----------------------------------------------*/ /*==============================================* * routines' or functions' implementations * *----------------------------------------------*/ void Rd_MemDoInit(void); void Rd_MemDoDeinit(void); void* Rd_MemPoolMalloc(size_t size); void Rd_MemPoolFree(void* ptr); size_t Rd_MemPoolGetFreeCount(void); void* Rd_MemPoolCalloc(size_t n, size_t size); void* Rd_MemPoolRealloc(void* ptr, size_t new_size); #ifdef __cplusplus #if __cplusplus } #endif #endif /* __cplusplus */ #endif /* __RD_MEMPOOL_H__ */