Skip to content

Commit 5d74e2e

Browse files
committed
code format & add calloc
1 parent 7fd1214 commit 5d74e2e

4 files changed

Lines changed: 71 additions & 44 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
#include "memorypool.h"
2222
#include <stdio.h>
2323

24-
struct TAT {
24+
struct TAT
25+
{
2526
int T_T;
2627
};
27-
mem_size_t max_mem = 2*GB + 500*MB + 500*KB;
28+
29+
mem_size_t max_mem = 2*GB + 1000*MB + 1000*KB;
2830
mem_size_t mem_pool_size = 1*GB + 500*MB + 500*KB;
2931

3032
int main()
@@ -33,6 +35,8 @@ int main()
3335
struct TAT *tat = (struct TAT *)MemoryPool_Alloc(mp, sizeof(struct TAT));
3436
tat->T_T = 2333;
3537
printf("%d\n", tat->T_T);
38+
int *a = (int *)MemoryPool_Calloc(mp, sizeof(int));
39+
printf("%d\n", *a);
3640
MemoryPool_Free(mp, tat);
3741
MemoryPool_Clear(mp);
3842
MemoryPool_Destroy(mp);
@@ -65,6 +69,7 @@ int main()
6569
~~~c
6670
MemoryPool* MemoryPool_Init (mem_size_t maxmempoolsize, mem_size_t mempoolsize, int thread_safe);
6771
void* MemoryPool_Alloc (MemoryPool *mp, mem_size_t wantsize);
72+
void* MemoryPool_Calloc (MemoryPool *mp, mem_size_t wantsize);
6873
int MemoryPool_Free (MemoryPool *mp, void *p);
6974
MemoryPool* MemoryPool_Clear (MemoryPool *mp);
7075
int MemoryPool_Destroy(MemoryPool *mp);

example.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ int main()
1515
struct TAT *tat = (struct TAT *)MemoryPool_Alloc(mp, sizeof(struct TAT));
1616
tat->T_T = 2333;
1717
printf("%d\n", tat->T_T);
18+
int *a = (int *)MemoryPool_Calloc(mp, sizeof(int));
19+
printf("%d\n", *a);
1820
MemoryPool_Free(mp, tat);
1921
MemoryPool_Clear(mp);
2022
MemoryPool_Destroy(mp);

memorypool.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
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+
346
void
447
get_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+
214266
int
215267
MemoryPool_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

memorypool.h

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,14 @@
11
#ifndef _Z_MEMORYPOOL_H_
22
#define _Z_MEMORYPOOL_H_
33
#include <stdlib.h>
4+
#include <string.h>
45
#include <pthread.h>
56

67
#define mem_size_t unsigned long long
7-
#define MP_CHUNKHEADER sizeof(struct _mp_chunk)
8-
#define MP_CHUNKEND sizeof(struct _mp_chunk *)
9-
108
#define KB (mem_size_t)(1 << 10)
119
#define MB (mem_size_t)(1 << 20)
1210
#define GB (mem_size_t)(1 << 30)
1311

14-
#define MP_LOCK(flag, lockobj) do { \
15-
if (flag) pthread_mutex_lock(&lockobj->lock); \
16-
} while (0)
17-
#define MP_UNLOCK(flag, lockobj) do { \
18-
if (flag) pthread_mutex_unlock(&lockobj->lock); \
19-
} while (0)
20-
21-
#define MP_ALIGN_SIZE(_n) (_n + sizeof(long) - ((sizeof(long)-1)&_n))
22-
23-
#define MP_INIT_MEMORY_STRUCT(mm, mem_sz) do { \
24-
mm->mem_pool_size = mem_sz; \
25-
mm->alloc_mem = 0; \
26-
mm->alloc_prog_mem = 0; \
27-
mm->free_list = (_MP_Chunk *)mm->start; \
28-
mm->free_list->is_free = 1; \
29-
mm->free_list->alloc_mem = mem_sz; \
30-
mm->free_list->prev = NULL; \
31-
mm->free_list->next = NULL; \
32-
mm->alloc_list = NULL; \
33-
} while (0)
34-
35-
#define MP_DLINKLIST_INS_FRT(head,x) do { \
36-
x->prev = NULL; \
37-
x->next = head; \
38-
if (head) \
39-
head->prev = x; \
40-
head = x; \
41-
} while(0)
42-
43-
#define MP_DLINKLIST_DEL(head,x) do { \
44-
if (!x->prev) { \
45-
head = x->next; \
46-
if (x->next) x->next->prev = NULL; \
47-
} else { \
48-
x->prev->next = x->next; \
49-
if (x->next) x->next->prev = x->prev; \
50-
} \
51-
} while(0)
52-
5312
typedef struct _mp_chunk
5413
{
5514
mem_size_t alloc_mem;
@@ -95,6 +54,7 @@ int get_memory_id(_MP_Memory *mm);
9554

9655
MemoryPool* MemoryPool_Init (mem_size_t maxmempoolsize, mem_size_t mempoolsize, int thread_safe);
9756
void* MemoryPool_Alloc (MemoryPool *mp, mem_size_t wantsize);
57+
void* MemoryPool_Calloc (MemoryPool *mp, mem_size_t wantsize);
9858
int MemoryPool_Free (MemoryPool *mp, void *p);
9959
MemoryPool* MemoryPool_Clear (MemoryPool *mp);
10060
int MemoryPool_Destroy(MemoryPool *mp);

0 commit comments

Comments
 (0)