-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool_alloc.c
310 lines (293 loc) · 7.41 KB
/
pool_alloc.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <lua.h>
#include "lauxlib.h"
#define CHUNK_COUNT 15
#define MAX_BLOCK_SIZE 640
#define CHUNK_SIZE 16 * 1024 //16k for one chunk,from 1k to 16k,the larger the faster(my test)
#define BOOL int
#define TRUE 1
#define FALSE 0
struct PoolChunkInfo {
size_t blockSize;
size_t blockCount;
};
struct PoolChunkInfo blockSizeMap[CHUNK_COUNT] =
{
{8,0},//can not be less than 8 bytes
{16,0},
{32,0},
{64,0},
{96,0},
{128,0},
{160,0},
{192,0},
{224,0},
{256,0},
{320,0},
{384,0},
{448,0},
{512,0},
{MAX_BLOCK_SIZE,0},
};
struct PoolStat {
size_t iCreate;
size_t iFree;
size_t iHitCreate;
size_t iHitFree;
//size_t iChunkCount;
};
struct PoolStat Stats[CHUNK_COUNT];
int SizeToChunkId[MAX_BLOCK_SIZE + 1];
struct PoolChunk {
size_t blockSize;
size_t blockCount;
size_t totalSize;
size_t chunkCount;
struct PoolChunk* next;
char* blocks;
};
struct PoolChunk* ChunkList[CHUNK_COUNT];
struct PoolBlock {//use for free blocks
struct PoolBlock* next;
};
struct PoolBlock* FreeBlocks[CHUNK_COUNT];
void init_chunk(struct PoolChunk* chunk,int iChunk) {
chunk->blockSize = blockSizeMap[iChunk].blockSize;
chunk->blockCount = CHUNK_SIZE / chunk->blockSize;
chunk->totalSize = chunk->blockSize * chunk->blockCount;
chunk->next = NULL;
chunk->blocks = malloc(chunk->totalSize);
if (chunk->blocks != NULL) {
//not necessary,just for debug convenient,you can comment this line
memset((void*)chunk->blocks,0xEF,chunk->totalSize);
}
for (int iBlock = 0; iBlock < chunk->blockCount ; ++iBlock) {
int iPos = iBlock * chunk->blockSize;
char* currBlock = (chunk->blocks + iPos);
if (iBlock < chunk->blockCount - 1) {
void* nextBlock = (currBlock + chunk->blockSize);
if (currBlock != NULL) {
((struct PoolBlock*)currBlock)->next = nextBlock;
}
}
else {
if (currBlock != NULL) {
((struct PoolBlock*)currBlock)->next = NULL;
}
}
}
}
void init_pool_alloc() {
memset(Stats,0,sizeof(struct PoolStat) * CHUNK_COUNT);
//init size map
{
int iChunk = 0;
for (int iSize = 1; iSize <= MAX_BLOCK_SIZE; ++iSize) {
SizeToChunkId[iSize] = iChunk;
if (iSize == blockSizeMap[iChunk].blockSize) {
++iChunk;
}
}
}
//init chunks
{
for (int iChunk = 0; iChunk < CHUNK_COUNT; ++iChunk) {
struct PoolChunk* chunk = malloc(sizeof(struct PoolChunk));
ChunkList[iChunk] = chunk;
init_chunk(chunk,iChunk);
if (chunk != NULL) {
FreeBlocks[iChunk] = chunk->blocks;
}
}
}
}
void pool_alloc_stat(size_t osize, size_t nsize, BOOL hitCache)
{
if (nsize == 0) {
int isize = (int)osize;
if (isize <= MAX_BLOCK_SIZE) {
if (hitCache) {
Stats[SizeToChunkId[isize]].iHitFree += 1;
}
else {
Stats[SizeToChunkId[isize]].iFree += 1;
}
}
}
else {
int isize = (int)nsize;
if (isize <= MAX_BLOCK_SIZE) {
if (hitCache) {
Stats[SizeToChunkId[isize]].iHitCreate += 1;
}
else {
Stats[SizeToChunkId[isize]].iCreate += 1;
}
}
}
}
static int totalAllocTimes = 0;
void* simple_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
(void)ud; /* not used */
//++totalAllocTimes;
pool_alloc_stat(osize,nsize,FALSE);
if (nsize == 0) {
free(ptr);
return NULL;
}
else {
return realloc(ptr, nsize);
}
}
void* pool_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
//++totalAllocTimes;
//printf("totalAllocTimes:%d\n",totalAllocTimes);
(void)ud; /* not used */
if (nsize == 0) {
if (ptr != NULL) {
int isize = (int)osize;
if (isize <= MAX_BLOCK_SIZE) {
int iChunk = SizeToChunkId[isize];
struct PoolBlock* block = FreeBlocks[iChunk];
((struct PoolBlock*)ptr)->next = block;
FreeBlocks[iChunk] = ptr;
pool_alloc_stat(osize,nsize,TRUE);
return NULL;
}
else {
pool_alloc_stat(osize,nsize,FALSE);
free(ptr);
return NULL;
}
}
else {
return NULL;
}
}
else {
int i_nsize = (int)nsize;
if (i_nsize <= MAX_BLOCK_SIZE) {
int iChunk = SizeToChunkId[i_nsize];
struct PoolBlock* block = FreeBlocks[iChunk];
if (block == NULL) {
struct PoolChunk* newChunk = malloc(sizeof(struct PoolChunk));
if (newChunk != NULL) {
init_chunk(newChunk,iChunk);
newChunk->next = ChunkList[iChunk];
ChunkList[iChunk] = newChunk;
FreeBlocks[iChunk] = newChunk->blocks;
block = newChunk->blocks;
ChunkList[iChunk]->chunkCount += 1;
}
}
else {
//FreeBlocks[iChunk] = ((struct PoolBlock*)block)->next;
}
if (block != NULL) {
FreeBlocks[iChunk] = block->next;
if (ptr != NULL) {
//luaM_shrinkvector_ lua table会缩容,所以这里要取最小值
memcpy(block,ptr,osize < nsize? osize : nsize);
//printf("block ptr:%p (%d/%lld)\n",block,isize,ChunkList[iChunk].blockSize);
}
else {
//memset(block,0xFF,ChunkList[iChunk]->blockSize);
//printf("block ptr:%p (%d/%lld)\n",block,isize,ChunkList[iChunk].blockSize);
}
pool_alloc_stat(osize,nsize,TRUE);
return block;
}
else {
//fatal,impossible
lua_assert(false);
return NULL;
}
}
else {
if (ptr != NULL) {
int i_osize = (int)osize;
if (i_osize <= MAX_BLOCK_SIZE) {
//old memory also comes from pool
void* new_ptr = malloc(nsize);
if (new_ptr != NULL) {
memcpy(new_ptr,ptr,osize);
}
int iChunk = SizeToChunkId[i_osize];
struct PoolBlock* block = FreeBlocks[iChunk];
((struct PoolBlock*)ptr)->next = block;
FreeBlocks[iChunk] = ptr;
pool_alloc_stat(osize,nsize,TRUE);
return new_ptr;
}
else {
return realloc(ptr, nsize);
}
}
else {
return realloc(ptr, nsize);
}
}
}
}
void* alloc_entry(void *ud, void *ptr, size_t osize, size_t nsize)
{
#ifdef USE_LUA_ALLOC_POOL
return pool_alloc(ud,ptr,osize,nsize);
#else
return simple_alloc(ud,ptr,osize,nsize);
#endif
}
int alloc_getStat(lua_State* L)
{
lua_newtable(L);
size_t totalCaheMem = 0;
for (int iChunk = 0; iChunk < CHUNK_COUNT; ++iChunk) {
lua_pushinteger(L,iChunk + 1);
lua_newtable(L);
lua_pushstring(L,"blockSize");
lua_pushinteger(L,blockSizeMap[iChunk].blockSize);
lua_settable(L,-3);
lua_pushstring(L,"blockCount");
lua_pushinteger(L,ChunkList[iChunk]->blockCount);
lua_settable(L,-3);
lua_pushstring(L,"iCreate");
lua_pushinteger(L,Stats[iChunk].iCreate);
lua_settable(L,-3);
lua_pushstring(L,"iFree");
lua_pushinteger(L,Stats[iChunk].iFree);
lua_settable(L,-3);
lua_pushstring(L,"iHitCreate");
lua_pushinteger(L,Stats[iChunk].iHitCreate);
lua_settable(L,-3);
lua_pushstring(L,"iHitFree");
lua_pushinteger(L,Stats[iChunk].iHitFree);
lua_settable(L,-3);
lua_pushstring(L,"iChunkCount");
lua_pushinteger(L,ChunkList[iChunk]->chunkCount);
lua_settable(L,-3);
size_t mem = ChunkList[iChunk]->chunkCount * CHUNK_SIZE;
totalCaheMem += mem;
lua_pushstring(L,"chunkMem");
lua_pushinteger(L,mem);
lua_settable(L,-3);
lua_settable(L,-3);
}
lua_pushstring(L,"cacheMem");
lua_pushinteger(L,totalCaheMem);
lua_settable(L,-3);
return 1;
}
static const luaL_Reg lib[] = {
{"getStat",alloc_getStat },
{NULL, NULL}
};
LUAMOD_API int luaopen_alloc (lua_State *L) {
luaL_newlib(L, lib);
return 1;
}