forked from userpro/MemoryPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memorypool.h
120 lines (90 loc) · 2.39 KB
/
memorypool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef _z_memorypool_h_
#define _z_memorypool_h_
#include <stdlib.h>
#include <stdbool.h>
#define MAX_MEM_SIZE (32 * GB)
#ifndef mem_size_t
#define mem_size_t unsigned long long
#endif
#ifndef CHUNKHEADER
#define CHUNKHEADER sizeof(struct _chunk)
#endif
#ifndef CHUNKEND
#define CHUNKEND sizeof(struct _chunk *)
#endif
#define init_Memory(mm) do { \
mm->alloc_mem = 0; \
mm->alloc_prog_mem = 0; \
mm->free_list = (Chunk *)mm->start; \
mm->free_list->is_free = 1; \
mm->free_list->alloc_mem = mp->mem_pool_size; \
mm->free_list->prev = NULL; \
mm->free_list->next = NULL; \
mm->alloc_list = NULL; \
} while (0)
#define dlinklist_insert_front(head,x) do { \
x->prev = NULL; \
x->next = head; \
if (head) \
head->prev = x; \
head = x; \
} while(0)
#define dlinklist_delete(head,x) do { \
if (!x->prev) { \
head = x->next; \
if (x->next) x->next->prev = NULL; \
} else { \
x->prev->next = x->next; \
if (x->next) x->next->prev = x->prev; \
} \
} while(0)
#define KB (mem_size_t)(1 << 10)
#define MB (mem_size_t)(1 << 20)
#define GB (mem_size_t)(1 << 30)
typedef struct _chunk
{
mem_size_t alloc_mem;
struct _chunk *prev;
struct _chunk *next;
bool is_free;
}Chunk;
typedef struct _mem_pool_list
{
char *start;
unsigned int id;
mem_size_t alloc_mem;
mem_size_t alloc_prog_mem;
Chunk *free_list, *alloc_list;
struct _mem_pool_list *next;
}Memory;
typedef struct _mem_pool
{
unsigned int last_id;
mem_size_t mem_pool_size, total_mem_pool_size;
struct _mem_pool_list *mlist;
bool auto_extend;
}MemoryPool;
/*
* 内部工具函数
*/
// 所有Memory的数量
void get_memory_list_count(MemoryPool *mp, mem_size_t *mlist_len);
// 每个Memory的统计信息
void get_memory_info(Memory *mm, mem_size_t *free_list_len, mem_size_t *alloc_list_len);
int get_memory_id(Memory *mm);
/*
* 内存池API
*/
MemoryPool *MemoryPool_Init(mem_size_t mempoolsize, bool auto_extend);
void *MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize);
bool MemoryPool_Free(MemoryPool *mp, void *p);
MemoryPool *MemoryPool_Clear(MemoryPool *mp);
bool MemoryPool_Destroy(MemoryPool *mp);
/*
* 获取内存池信息
*/
// 实际分配空间
double get_mempool_usage(MemoryPool *mp);
// 真实使用空间
double get_mempool_prog_usage(MemoryPool *mp);
#endif // !_z_memorypool_h_