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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <fcntl.h>



#define MP_ALIGNMENT            32
#define MP_PAGE_SIZE            4096
#define MP_MAX_ALLOC_FROM_POOL  (MP_PAGE_SIZE-1)

#define mp_align(n, alignment) (((n)+(alignment-1)) & ~(alignment-1))
#define mp_align_ptr(p, alignment) (void *)((((size_t)p)+(alignment-1)) & ~(alignment-1))





struct mp_large_s {
    struct mp_large_s *next;
    void *alloc;
};

struct mp_node_s {

    unsigned char *last;
    unsigned char *end;
   
    struct mp_node_s *next;
    size_t failed;
};

struct mp_pool_s {

    size_t max;

    struct mp_node_s *current;
    struct mp_large_s *large;

    struct mp_node_s head[0];

};

struct mp_pool_s *mp_create_pool(size_t size);
void mp_destory_pool(struct mp_pool_s *pool);
void *mp_alloc(struct mp_pool_s *pool, size_t size);
void *mp_nalloc(struct mp_pool_s *pool, size_t size);
void *mp_calloc(struct mp_pool_s *pool, size_t size);
void mp_free(struct mp_pool_s *pool, void *p);


struct mp_pool_s *mp_create_pool(size_t size) {

    struct mp_pool_s *p;
    int ret = posix_memalign((void **)&p, MP_ALIGNMENT, size + sizeof(struct mp_pool_s) + sizeof(struct mp_node_s));
    if (ret) {
        return NULL;
    }
   
    p->max = (size < MP_MAX_ALLOC_FROM_POOL) ? size : MP_MAX_ALLOC_FROM_POOL;
    p->current = p->head;
    p->large = NULL;

    p->head->last = (unsigned char *)p + sizeof(struct mp_pool_s) + sizeof(struct mp_node_s);
    p->head->end = p->head->last + size;

    p->head->failed = 0;

    return p;

}

void mp_destory_pool(struct mp_pool_s *pool) {

    struct mp_node_s *h, *n;
    struct mp_large_s *l;

    for (l = pool->large; l; l = l->next) {
        if (l->alloc) {
            free(l->alloc);
        }
    }

    h = pool->head->next;

    while (h) {
        n = h->next;
        free(h);
        h = n;
    }

    free(pool);

}

void mp_reset_pool(struct mp_pool_s *pool) {

    struct mp_node_s *h;
    struct mp_large_s *l;

    for (l = pool->large; l; l = l->next) {
        if (l->alloc) {
            free(l->alloc);
        }
    }

    pool->large = NULL;

    for (h = pool->head; h; h = h->next) {
        h->last = (unsigned char *)h + sizeof(struct mp_node_s);
    }

}

static void *mp_alloc_block(struct mp_pool_s *pool, size_t size) {

    unsigned char *m;
    struct mp_node_s *h = pool->head;
    size_t psize = (size_t)(h->end - (unsigned char *)h);
   
    int ret = posix_memalign((void **)&m, MP_ALIGNMENT, psize);
    if (ret) return NULL;

    struct mp_node_s *p, *new_node, *current;
    new_node = (struct mp_node_s*)m;

    new_node->end = m + psize;
    new_node->next = NULL;
    new_node->failed = 0;

    m += sizeof(struct mp_node_s);
    m = mp_align_ptr(m, MP_ALIGNMENT);
    new_node->last = m + size;

    current = pool->current;

    for (p = current; p->next; p = p->next) {
        if (p->failed++ > 4) {
            current = p->next;
        }
    }
    p->next = new_node;

    pool->current = current ? current : new_node;

    return m;

}

static void *mp_alloc_large(struct mp_pool_s *pool, size_t size) {

    void *p = malloc(size);
    if (p == NULL) return NULL;

    size_t n = 0;
    struct mp_large_s *large;
    for (large = pool->large; large; large = large->next) {
        if (large->alloc == NULL) {
            large->alloc = p;
            return p;
        }
        if (n ++ > 3) break;
    }

    large = mp_alloc(pool, sizeof(struct mp_large_s));
    if (large == NULL) {
        free(p);
        return NULL;
    }

    large->alloc = p;
    large->next = pool->large;
    pool->large = large;

    return p;
}

void *mp_memalign(struct mp_pool_s *pool, size_t size, size_t alignment) {

    void *p;
   
    int ret = posix_memalign(&p, alignment, size);
    if (ret) {
        return NULL;
    }

    struct mp_large_s *large = mp_alloc(pool, sizeof(struct mp_large_s));
    if (large == NULL) {
        free(p);
        return NULL;
    }

    large->alloc = p;
    large->next = pool->large;
    pool->large = large;

    return p;
}




void *mp_alloc(struct mp_pool_s *pool, size_t size) {

    unsigned char *m;
    struct mp_node_s *p;

    if (size <= pool->max) {

        p = pool->current;

        do {
           
            m = mp_align_ptr(p->last, MP_ALIGNMENT);
            if ((size_t)(p->end - m) >= size) {
                p->last = m + size;
                return m;
            }
            p = p->next;
        } while (p);

        return mp_alloc_block(pool, size);
    }

    return mp_alloc_large(pool, size);
   
}


void *mp_nalloc(struct mp_pool_s *pool, size_t size) {

    unsigned char *m;
    struct mp_node_s *p;

    if (size <= pool->max) {
        p = pool->current;

        do {
            m = p->last;
            if ((size_t)(p->end - m) >= size) {
                p->last = m+size;
                return m;
            }
            p = p->next;
        } while (p);

        return mp_alloc_block(pool, size);
    }

    return mp_alloc_large(pool, size);
   
}

void *mp_calloc(struct mp_pool_s *pool, size_t size) {

    void *p = mp_alloc(pool, size);
    if (p) {
        memset(p, 0, size);
    }

    return p;
   
}

void mp_free(struct mp_pool_s *pool, void *p) {

    struct mp_large_s *l;
    for (l = pool->large; l; l = l->next) {
        if (p == l->alloc) {
            free(l->alloc);
            l->alloc = NULL;

            return ;
        }
    }
   
}


int main(int argc, char *argv[]) {

    int size = 1 << 12;

    struct mp_pool_s *p = mp_create_pool(size);

    int i = 0;
    for (i = 0;i < 10;i ++) {

        void *mp = mp_alloc(p, 512);
//      mp_free(mp);
    }

    //printf("mp_create_pool: %ld\n", p->max);
    printf("mp_align(123, 32): %d, mp_align(17, 32): %d\n", mp_align(24, 32), mp_align(17, 32));
    //printf("mp_align_ptr(p->current, 32): %lx, p->current: %lx, mp_align(p->large, 32): %lx, p->large: %lx\n", mp_align_ptr(p->current, 32), p->current, mp_align_ptr(p->large, 32), p->large);

    int j = 0;
    for (i = 0;i < 5;i ++) {

        char *pp = mp_calloc(p, 32);
        for (j = 0;j < 32;j ++) {
            if (pp[j]) {
                printf("calloc wrong\n");
            }
            printf("calloc success\n");
        }
    }

    //printf("mp_reset_pool\n");

    for (i = 0;i < 5;i ++) {
        void *l = mp_alloc(p, 8192);
        mp_free(p, l);
    }

    mp_reset_pool(p);

    //printf("mp_destory_pool\n");
    for (i = 0;i < 58;i ++) {
        mp_alloc(p, 256);
    }

    mp_destory_pool(p);

    return 0;

}