-
Notifications
You must be signed in to change notification settings - Fork 3
/
mem.c
220 lines (183 loc) · 4.61 KB
/
mem.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
/**
* 内存池
* @file mem.c
* @author zhaowei
* @ingroup memlink
* @{
*/
#include <stdlib.h>
#include <string.h>
#include "zzmalloc.h"
#include "logfile.h"
#include "mem.h"
#include "common.h"
//MemPool *g_mpool;
MemPool*
mempool_create()
{
MemPool *mp;
mp = (MemPool*)zz_malloc(sizeof(MemPool));
if (NULL == mp) {
DERROR("malloc MemPool error!\n");
return NULL;
}
memset(mp, 0, sizeof(MemPool));
mp->size = MEMLINK_MEM_NUM;
mp->freemem = (MemItem*)zz_malloc(sizeof(MemItem) * mp->size);
if (NULL == mp->freemem) {
DERROR("malloc MemItem error!\n");
zz_free(mp);
return NULL;
}
memset(mp->freemem, 0, sizeof(MemItem) * mp->size);
//g_mpool = mp;
return mp;
}
DataBlock*
mempool_get(MemPool *mp, int blocksize)
{
int i;
DataBlock *dbk = NULL, *dbn;
int find = 0;
//DNOTE("used: %d, blocsize: %d\n", mp->used, blocksize);
for (i = 0; i < mp->used; i++) {
if (mp->freemem[i].memsize == blocksize) {
dbk = mp->freemem[i].data;
find = 1;
break;
}
}
if (NULL == dbk) {
dbk = (DataBlock*)zz_malloc(blocksize);
if (NULL == dbk) {
DERROR("malloc DataBlock error!\n");
MEMLINK_EXIT;
return NULL;
}
memset(dbk, 0x0, blocksize);
if (find != 1) {
if (i < mp->size) {
mp->freemem[i].memsize = blocksize;
mp->used += 1;
} else {
if (mempool_expand(mp) == -1)
return NULL;
mp->freemem[mp->used].memsize = blocksize;
mp->used += 1;
i = mp->used;
}
}
mp->freemem[i].total += 1;
return dbk;
}
mp->blocks--;
mp->freemem[i].block_count -= 1;
dbn = dbk->next;
mp->freemem[i].data = dbn;
//memset(dbk, 0, sizeof(DataBlock) + g_cf->block_data_count * blocksize);
memset(dbk, 0, blocksize);
//mp->freemem[i].ht_use = mp->freemem[i].ht_use + 1;
return dbk;
}
DataBlock*
mempool_get2(MemPool *mp, int blocks, int datalen)
{
//DINFO("get size:%d, blocks:%d, datalen:%d\n", sizeof(DataBlock) + blocks * datalen, blocks, datalen);
DataBlock *bk = mempool_get(mp, sizeof(DataBlock) + blocks * datalen);
bk->data_count = blocks;
return bk;
}
int
mempool_put(MemPool *mp, DataBlock *dbk, int blocksize)
{
int i;
zz_check(dbk);
dbk->data_count = 0;
dbk->prev = NULL;
for (i = 0; i < mp->used; i++) {
if (mp->freemem[i].memsize == blocksize) {
dbk->next = mp->freemem[i].data;
mp->freemem[i].data = dbk;
mp->blocks++;
mp->freemem[i].block_count += 1;
return 0;
}
}
dbk->next = NULL;
if (i < mp->size) {
mp->freemem[i].data = dbk;
mp->freemem[i].memsize = blocksize;
mp->used += 1;
}else{
if (mempool_expand(mp) == -1)
return -2;
mp->freemem[mp->used].memsize = blocksize;
mp->freemem[mp->used].data = dbk;
mp->used += 1;
zz_check(dbk);
}
mp->blocks++;
mp->freemem[i].block_count += 1;
return 0;
}
int
mempool_put2(MemPool *mp, DataBlock *dbk, int datalen)
{
return mempool_put(mp, dbk, sizeof(DataBlock) + dbk->data_count * datalen);
}
int
mempool_expand(MemPool *mp)
{
int newnum = mp->size * 2;
MemItem *newitems = (MemItem*)zz_malloc(sizeof(MemItem) * newnum);
if (NULL == newitems) {
DERROR("malloc error!\n");
MEMLINK_EXIT;
return -1;
}
memcpy(newitems, mp->freemem, sizeof(MemItem) * mp->used);
zz_free(mp->freemem);
mp->freemem = newitems;
mp->size = newnum;
return 0;
}
void
mempool_free(MemPool *mp, int blocksize)
{
int i;
for (i = 0; i < mp->used; i++) {
if (mp->freemem[i].memsize == blocksize) {
DataBlock *dbk = mp->freemem[i].data;
DataBlock *tmp;
while (dbk) {
tmp = dbk;
dbk = dbk->next;
zz_free(tmp);
mp->blocks--;
}
mp->freemem[i].data = NULL;
}
}
}
void
mempool_destroy(MemPool *mp)
{
if (NULL == mp)
return;
int i;
for (i = 0; i < mp->used; i++) {
DataBlock *dbk = mp->freemem[i].data;
DataBlock *tmp;
while (dbk) {
tmp = dbk;
dbk = dbk->next;
zz_free(tmp);
}
mp->freemem[i].data = NULL;
}
zz_free(mp->freemem);
zz_free(mp);
}
/**
* @}
*/