-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpool.h
56 lines (44 loc) · 1.37 KB
/
mpool.h
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
#pragma once
#include "cthing.h"
#define CT_MP_ALLOC(pool, type) \
((pool) != NULL ? ct_mpool_alloc(pool) : malloc(sizeof(type)))
#define CT_MP_FREE(pool, ptr) \
((pool) != NULL ? ct_mpool_free_block(pool, (ptr)) : free(ptr))
#define CT_DEF_MPOOL(name, num, type) \
CT_MPool name; \
if (ct_mpool_init(&(name), (num), sizeof(type))) { \
CT_ERROR("out of memory (mpool)"); \
goto fail; \
}
CT_BEGIN_DECLS
typedef struct CT_MPoolFreeList CT_MPoolFreeList;
typedef struct CT_MPoolList CT_MPoolList;
struct CT_MPoolFreeList {
CT_MPoolFreeList *next;
};
struct CT_MPoolList {
uint8_t *pool;
CT_MPoolList *next;
size_t nextID;
};
typedef struct {
CT_MPoolList *head;
CT_MPoolFreeList *freeList;
size_t blockSize;
size_t numBlocks;
size_t poolID;
} CT_MPool;
typedef struct {
size_t blocks;
size_t pools;
} CT_MPCompactResult;
CT_MPool *ct_mpool_new();
size_t ct_mpool_init(CT_MPool *pool, size_t num, size_t bsize);
void *ct_mpool_alloc(CT_MPool *pool);
void ct_mpool_free_block(CT_MPool *pool, const void *block);
void ct_mpool_reset(CT_MPool *mp);
void ct_mpool_free(CT_MPool *pool);
int ct_mpool_is_valid_block(CT_MPool *pool, void *block);
CT_MPCompactResult ct_mpool_compact(CT_MPool *mp);
void ct_mpool_trace(const CT_MPool *pool);
CT_END_DECLS