/********************************************************************************************* Description: In-thread memory allocation with boundary aligned Copyright : All right reserved by NCIC.ICT Author : Zhang Zhonghai Date : 2023/08/23 ***********************************************************************************************/ #ifndef __MEMORY_H #define __MEMORY_H #include #include #include #define MEM_ALIGN_BYTE 8 #define MEM_MOVE_BIT 3 typedef struct _thread_mem_t { size_t occupied; // 已经占用的容量(字节数) 对齐的 size_t capacity; // 总容量(字节数) void *mem; // 申请的内存首地址 } thread_mem_t; // 创建thread_mem_t thread_mem_t *create_thread_mem(); // 初始化 void init_thread_mem(thread_mem_t *tmem); // 初始化并开辟一定量的内存 void thread_mem_init_alloc(thread_mem_t *tmem, size_t byte_cnt); // 请求内存 void *thread_mem_request(thread_mem_t *tmem, size_t byte_cnt); // 请求内存并初始化为零 void *thread_mem_request_and_clean(thread_mem_t *tmem, size_t byte_cnt); // 将不用的内存归还给thread mem void thread_mem_release(thread_mem_t *tmem, size_t byte_cnt); // 彻底释放内存 void thread_mem_free(thread_mem_t *tmem); // 销毁thread_mem_t void destroy_thread_mem(thread_mem_t *tmem); #endif