-
Notifications
You must be signed in to change notification settings - Fork 3
/
memory_pool.c
219 lines (170 loc) · 4.01 KB
/
memory_pool.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*************************************************************************
> File Name: memory_pool.c
> Author: Ukey
> Mail: [email protected]
> Created Time: 2018年02月07日 星期三 11时23分39秒
************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include "memory_pool.h"
static void *ukey_palloc_block(ukey_pool_t *pool, int size);
static void *ukey_palloc_large(ukey_pool_t *pool, int size);
ukey_pool_t *ukey_create_pool(int size) {
ukey_pool_t *p;
int page_size = getpagesize();
int type_len = sizeof(ukey_pool_t);
if (size <= type_len) {
fprintf(stderr, "create too small memory pool\n");
return NULL;
}
p = memalign(UKEY_POOL_ALIGNMENT, size);
if (p == NULL) {
return NULL;
}
p->small.last = (char *)p + type_len;
p->small.end = (char *)p + size;
p->small.next = NULL;
p->small.failed = 0;
size -= type_len;
p->max = (size < page_size) ? size : page_size;
page_size = p->max;
p->current = p;
p->large = NULL;
return p;
}
void ukey_destroy_pool(ukey_pool_t *pool) {
ukey_pool_t *p, *n;
ukey_pool_large_t *l;
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
free(l->alloc);
l->alloc = NULL;
}
}
for (p = pool, n = pool->small.next; ; p = n, n = n->small.next) {
free(p);
if (n == NULL) {
break;
}
}
}
void ukey_reset_pool(ukey_pool_t *pool) {
ukey_pool_t *p;
ukey_pool_large_t *l;
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
free(l->alloc);
l->alloc = NULL;
}
}
pool->large = NULL;
for (p = pool; p; p = p->small.next) {
p->small.last = (char *)p + sizeof(ukey_pool_t);
}
}
void *ukey_palloc(ukey_pool_t *pool, int size) {
ukey_pool_t *p;
char *m = NULL;
if (size <= pool->max) {
p = pool->current;
do {
m = (char *)((unsigned long)((p->small.last) + (UKEY_POOL_ALIGNMENT - 1)) & (unsigned long)~(UKEY_POOL_ALIGNMENT - 1));
if ((int)(p->small.end - m) >= size) {
p->small.last = m + size;
return m;
}
p = p->small.next;
} while (p);
return ukey_palloc_block(pool, size);
}
return ukey_palloc_large(pool, size);
}
static void *ukey_palloc_block(ukey_pool_t *pool, int size) {
char *m;
int psize;
ukey_pool_t *p, *new, *current;
psize = (int)(pool->small.end - (char *)pool);
m = memalign(UKEY_POOL_ALIGNMENT, psize);
if (m == NULL) {
return NULL;
}
new = (ukey_pool_t *)m;
new->small.end = m + psize;
new->small.next = NULL;
new->small.failed = 0;
m += sizeof(ukey_pool_data_t);
m = (char *)((unsigned long)(m + (UKEY_POOL_ALIGNMENT - 1)) & (unsigned long)~(UKEY_POOL_ALIGNMENT - 1));
new->small.last = m + size;
current = pool->current;
for (p = current; p->small.next; p = p->small.next) {
if (p->small.failed++ > 4) {
current = p->small.next;
}
}
p->small.next = new;
pool->current = current ? current : new;
return m;
}
static void *ukey_palloc_large(ukey_pool_t *pool, int size) {
void *p;
ukey_pool_large_t *l;
p = malloc(size);
if (p == NULL) {
return NULL;
}
for (l = pool->large; l; l = l->next) {
if (l->alloc == NULL) {
l->alloc = p;
return p;
}
}
l = ukey_palloc(pool, sizeof(ukey_pool_large_t));
if (l == NULL) {
free(p);
p = NULL;
return NULL;
}
l->alloc = p;
l->next = pool->large;
pool->large = l;
return p;
}
void *ukey_pnalloc(ukey_pool_t *pool, int size) {
char *m;
ukey_pool_t *p;
if (size <= pool->max) {
p = pool->current;
do {
m = p->small.last;
if ((int)(p->small.end - m) >= size) {
p->small.last = m + size;
return m;
}
p = p->small.next;
} while (p);
return ukey_palloc_block(pool, size);
}
return ukey_palloc_large(pool, size);
}
void *ukey_pcalloc(ukey_pool_t *pool, int size) {
void *p;
p = ukey_palloc(pool, size);
if (p) {
memset(p, 0, size);
}
return p;
}
int ukey_pfree(ukey_pool_t *pool, void *p) {
ukey_pool_large_t *l;
for (l = pool->large; l; l = l->next) {
if (p == l->alloc) {
free(p);
l->alloc = NULL;
return 0;
}
}
return -1;
}