-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcldMemPool.c
More file actions
290 lines (242 loc) · 8.44 KB
/
cldMemPool.c
File metadata and controls
290 lines (242 loc) · 8.44 KB
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
/*
*
*/
#include "cldMemPool.h"
/*_cldMemBlock_create
* Create a memory block
*param:
* nUnitSize : allocation unit of Memory blocks size
* num : allocation unit of Memory blocks num
*return:
* NULL: failed
* other: sucessful
*/
static cldMemBlock * _cldMemBlock_create(int nUnitSize,int num)
{
cldMemBlock *pMemBlock = NULL;
int i = 0;
char *offset = NULL;
pMemBlock = (cldMemBlock *)malloc(CLD_MEMBLOCK_HEAD + num * nUnitSize);
if(!pMemBlock){
return NULL;
}
pMemBlock->next = NULL;
pMemBlock->nFirst = 1;
pMemBlock->nFree = num - 1;
pMemBlock->nSize = num*nUnitSize;
/*Each allocation unit next allocation unit serial number written on the
*current unit in the first four bytes
*/
offset = pMemBlock->aData;
for(i = 1;i < num; i++){
*((int *)offset) = i;
offset += nUnitSize;
}
// printf("###%s,create OK!\n",__func__);
return pMemBlock;
}
/*_cldMemPool_init
* Create a memory Pool and init it
*param:
* nUnitSize : allocation unit of Memory blocks size
* nInitNum : init allocation unit of Memory blocks num
* nGrowNum:
*return:
* NULL: failed, param error or malloc failed;
* other: sucessful,return cldMemPool pointer;
*/
cldMemPool * _cldMemPool_init(int nUnitSize,int nInitNum,int nGrowNum)
{
cldMemPool *pMemPool = NULL;
if(!nInitNum || !nGrowNum){
printf("###%s, init failed\n",__func__);
return NULL;
}
pMemPool = (cldMemPool *) malloc(sizeof(cldMemPool));
if(!pMemPool){
return NULL;
}
memset(pMemPool,0,sizeof(cldMemPool));
/*Guarantee deposit 4 bytes (memory allocation unit number)*/
if(nUnitSize < sizeof(int)){
pMemPool->nUnitSize = sizeof(int);
}
/*Deposit guarantee 8 bytes alignment*/
pMemPool->nUnitSize = (nUnitSize + (CLD_MEMPOOL_ALIGNMENT - 1))&~(CLD_MEMPOOL_ALIGNMENT -1);
pMemPool->nInitNum = nInitNum;
pMemPool->nGrowNum = nGrowNum;
pMemPool->nMemBlockNum = 0;
pMemPool->head = NULL;
printf("###%s,init OK!\n",__func__);
return pMemPool;
}
/*_cldMemPool_exit
* free memory block add memory pool
*param:
* pMemPool : memory Pool pointer
*/
void _cldMemPool_exit(cldMemPool *pMemPool)
{
cldMemBlock *pMemBlock = NULL;
cldMemBlock *ptemp = NULL;
if(!pMemPool){
return;
}
pMemBlock = pMemPool->head;
while(pMemBlock){
ptemp = pMemBlock;
pMemBlock = pMemBlock->next;
free(ptemp);
}
free(pMemPool);
pMemPool = NULL;
printf("###%s,exit OK!\n",__func__);
}
/*_cldMem_malloc
* Traverse the memory pool memory blocks, find unused memory unit.If the memory
* unit is not found, malloc a new memory block, and added to the
* memory pool memory block list of the first node
*param:
* pMemPool : memory Pool pointer
*return:
* NULL: failed.
* other:sucessful, return a pointer to the malloc memory
*/
void * _cldMem_malloc(cldMemPool *pMemPool)
{
cldMemBlock *pMemBlock = NULL;
if(!pMemPool){
printf("###%s() line[%d] parameter is NULL\n",__func__,__LINE__);
return NULL;
}
pMemBlock = pMemPool->head;
if(!pMemBlock){/*the head is NULL,first create Memory block*/
pMemBlock = _cldMemBlock_create(pMemPool->nUnitSize,pMemPool->nInitNum);
if(!pMemBlock){
printf("###%s() line[%d] _cldMemBlock_create failed!\n",__func__,__LINE__);
return NULL;
}
pMemPool->head = pMemBlock;
pMemPool->nMemBlockNum ++;
return pMemBlock->aData;
}
/*find the free allocation unit */
while(pMemBlock && pMemBlock->nFree == 0){
pMemBlock = pMemBlock->next;
}
if(pMemBlock){/*have allocation unit*/
/*find the free allocation unit address*/
char *pMemtep = pMemBlock->aData + (pMemBlock->nFirst)*(pMemPool->nUnitSize);
pMemBlock->nFirst = *((int *)pMemtep);
pMemBlock->nFree--;
return pMemtep;
} else {
/*Not find free memory Block,alloc the new add add to the list head*/
pMemBlock = _cldMemBlock_create(pMemPool->nUnitSize,pMemPool->nGrowNum);
if(!pMemBlock){
printf("###%s() line[%d] _cldMemBlock_create failed!\n",__func__,__LINE__);
return NULL;
}
/*add to list head*/
pMemBlock->next = pMemPool->head;
pMemPool->head = pMemBlock;
pMemPool->nMemBlockNum ++;
return pMemBlock->aData;
}
}
/*cldMem_free
* Recycling memory allocation unit.Recycling, if the current memory
* block is free state (memory units are not allocated) and number of
* memory blocks is greater than 1, free memory brock.If memory pool
* first node has full distribution of the current memory blocks to the first node
*param:
* pMemPool : memory Pool pointer
*return:
* -1: failed.
* 0:sucessful, return a pointer to the malloc memory
*/
int cldMem_free(cldMemPool * pMemPool,void *pFree)
{
cldMemBlock *pMemBlock = NULL;
cldMemBlock *ptemp = NULL;
if(!pMemPool || !pFree){
printf("###%s() line[%d] pMemPool or pFree is free\n",__func__,__LINE__);
return -1;
}
pMemBlock = pMemPool->head;
while(pMemBlock && (pMemBlock->aData > pFree || pFree >= (pMemBlock->aData + pMemBlock->nSize))){
ptemp = pMemBlock;
pMemBlock = pMemBlock->next;
}
if(!pMemBlock){/*something error*/
printf("###%s() line[%d] pfree[%p] is not in the cldMemPool!\n",__func__,__LINE__,pFree);
return -1;
}
pMemBlock->nFree++;
*((int *)pFree) = pMemBlock->nFirst;
pMemBlock->nFirst = ((char *)pFree - pMemBlock->aData)/(pMemPool->nUnitSize);
/*Whether to release the memory block*/
if(pMemBlock->nSize == pMemBlock->nFree * pMemPool->nUnitSize){
if(pMemPool->nMemBlockNum > 1 && pMemPool->head->nFree){/*Ensure that at least a memory block*/
if(pMemBlock == pMemPool->head){
pMemPool->head = pMemBlock->next;
} else {
ptemp->next = pMemBlock->next;
}
free(pMemBlock);
pMemPool->nMemBlockNum--;
}
} else if(pMemPool->head->nFree == 0){
/*Memory block move to the first node list*/
ptemp->next = pMemBlock->next;
pMemBlock->next = pMemPool->head;
pMemPool->head = pMemBlock;
}
/*Recycling is successful, the pointer to NULL*/
pFree = NULL;
return 0;
}
void print_MemoryBlockInfoByPointer(cldMemPool *pMemPool, void *ptr)
{
cldMemBlock *pMemBlock = NULL;
if(!pMemPool){
printf("###%s() line[%d] pMemPool is NULL!\n",__func__,__LINE__);
return;
}
if(!ptr){
printf("###%s() line[%d] ptr is NULL!\n",__func__,__LINE__);
return;
}
pMemBlock = pMemPool->head;
while(pMemBlock && (pMemBlock->aData > ptr || ptr >= (pMemBlock->aData + pMemBlock->nSize))){
pMemBlock = pMemBlock->next;
}
if(!pMemBlock){/*something error*/
printf("###%s() line[%d] pfree[%p] is not in the cldMemPool!\n",__func__,__LINE__,ptr);
return;
} else {
printf("###%s() line[%d] pfree[%p] is in pMemBlock[%p]!\n",__func__,__LINE__,ptr,pMemBlock);
printf("**********pMemBlock info**************\n");
printf("*\tnSize = %d\n*\tnFree = %d\n*\tnFirst = %d\n*\taData = %p\n",pMemBlock->nSize,pMemBlock->nFree,pMemBlock->nFirst,pMemBlock->aData);
printf("**************************************\n");
}
}
void Print_MemoryPool_INFO(cldMemPool *pMemPool)
{
cldMemBlock *pMemBlock = NULL;
int i = 1;
if(!pMemPool){
return;
}
printf("************PMemPool Info*****************\n");
printf("*\tnUnitSize = %d\n*\tnMemBlockNum = %d\n*\tnInitNum = %d\n*\tnGrowNum = %d\n",pMemPool->nUnitSize,pMemPool->nMemBlockNum,pMemPool->nInitNum,pMemPool->nGrowNum);
printf("******************************************\n");
pMemBlock = pMemPool->head;
if(pMemBlock){
for(; pMemBlock != NULL;pMemBlock = pMemBlock->next){
printf("**********pMemBlock info[%d]**************\n",i++);
printf("*\tnSize = %d\n*\tnFree = %d\n*\tnFirst = %d\n*\taData = %p\n",pMemBlock->nSize,pMemBlock->nFree,pMemBlock->nFirst,pMemBlock->aData);
printf("*****************************************\n");
}
}
}