11#include "memorypool.h"
22
3+ #define MP_CHUNKHEADER sizeof(struct _mp_chunk)
4+ #define MP_CHUNKEND sizeof(struct _mp_chunk *)
5+
6+ #define MP_LOCK (flag , lockobj ) do { \
7+ if (flag) pthread_mutex_lock(&lockobj->lock); \
8+ } while (0)
9+ #define MP_UNLOCK (flag , lockobj ) do { \
10+ if (flag) pthread_mutex_unlock(&lockobj->lock); \
11+ } while (0)
12+
13+ #define MP_ALIGN_SIZE (_n ) (_n + sizeof(long) - ((sizeof(long)-1)&_n))
14+
15+ #define MP_INIT_MEMORY_STRUCT (mm , mem_sz ) do { \
16+ mm->mem_pool_size = mem_sz; \
17+ mm->alloc_mem = 0; \
18+ mm->alloc_prog_mem = 0; \
19+ mm->free_list = (_MP_Chunk *)mm->start; \
20+ mm->free_list->is_free = 1; \
21+ mm->free_list->alloc_mem = mem_sz; \
22+ mm->free_list->prev = NULL; \
23+ mm->free_list->next = NULL; \
24+ mm->alloc_list = NULL; \
25+ } while (0)
26+
27+ #define MP_DLINKLIST_INS_FRT (head ,x ) do { \
28+ x->prev = NULL; \
29+ x->next = head; \
30+ if (head) \
31+ head->prev = x; \
32+ head = x; \
33+ } while(0)
34+
35+ #define MP_DLINKLIST_DEL (head ,x ) do { \
36+ if (!x->prev) { \
37+ head = x->next; \
38+ if (x->next) x->next->prev = NULL; \
39+ } else { \
40+ x->prev->next = x->next; \
41+ if (x->next) x->next->prev = x->prev; \
42+ } \
43+ } while(0)
44+
45+
346void
447get_memory_list_count (MemoryPool * mp , mem_size_t * mlist_len ) {
548 MP_LOCK (mp -> thread_safe , mp );
@@ -211,6 +254,15 @@ MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize)
211254 return NULL ;
212255}
213256
257+ void *
258+ MemoryPool_Calloc (MemoryPool * mp , mem_size_t wantsize ) {
259+ char * m = (char * )MemoryPool_Alloc (mp , wantsize );
260+ if (!m ) return NULL ;
261+ _MP_Chunk * ck = (_MP_Chunk * )(m - MP_CHUNKHEADER );
262+ memset ((void * )m , ck -> alloc_mem - MP_CHUNKHEADER - MP_CHUNKEND , 0 );
263+ return m ;
264+ }
265+
214266int
215267MemoryPool_Free (MemoryPool * mp , void * p )
216268{
@@ -312,3 +364,11 @@ get_mempool_prog_usage(MemoryPool *mp) {
312364 return (float )get_prog_memory (mp ) / mp -> total_mem_pool_size ;
313365}
314366
367+ #undef MP_CHUNKHEADER
368+ #undef MP_CHUNKEND
369+ #undef MP_LOCK
370+ #undef MP_UNLOCK
371+ #undef MP_ALIGN_SIZE
372+ #undef MP_INIT_MEMORY_STRUCT
373+ #undef MP_DLINKLIST_INS_FRT
374+ #undef MP_DLINKLIST_DEL
0 commit comments