TRUE/FALSE -> true/false
[eleutheria.git] / malloc / mpool.h
blobe4203e802463b7ffbfad0d035f9fa5e384648504
1 #ifndef MPOOL_H_
2 #define MPOOL_H_
4 #include <stdlib.h>
5 #include <sys/queue.h>
7 /*#define MP_DEBUG*/
8 #define MP_STATS
10 #ifdef MP_DEBUG
11 #define DPRINTF(a) printf a
12 #else
13 #define DPRINTF(a)
14 #endif
16 #define MP_NODE_AVAIL (1 << 0) /* If not set, node is reserved, else available */
17 #define MP_NODE_LR (1 << 1) /* If not set, node is left buddy, else right buddy */
18 #define MP_NODE_PARENT (1 << 2) /* If not set, parent is left buddy, else right buddy */
20 /* Macro definitions */
21 #define MPOOL_MARK_AVAIL(pnode) pnode->flags |= MP_NODE_AVAIL
22 #define MPOOL_MARK_USED(pnode) pnode->flags &= ~MP_NODE_AVAIL
23 #define MPOOL_MARK_LEFT(pnode) pnode->flags &= ~MP_NODE_LR
24 #define MPOOL_MARK_RIGHT(pnode) pnode->flags |= MP_NODE_LR
26 #define MPOOL_IS_AVAIL(pnode) ((pnode->flags & MP_NODE_AVAIL) != 0)
27 #define MPOOL_IS_USED(pnode) ((pnode->flags & MP_NODE_AVAIL) == 0)
28 #define MPOOL_IS_LEFT(pnode) ((pnode->flags & MP_NODE_LR) == 0)
29 #define MPOOL_IS_RIGHT(pnode) ((pnode->flags & MP_NODE_LR) != 0)
31 typedef struct blknode {
32 unsigned char flags; /* availability, left-right buddiness, inheritance */
33 size_t logsize; /* logarithm of size with base 2 */
34 void *ptr; /* pointer to beginning of free block (what mpool_alloc() returns) */
35 LIST_ENTRY(blknode) next_chunk;
36 } blknode_t;
38 typedef struct mpool {
39 void *mem;
40 size_t nblocks; /* nblocks = logsize + 1 */
41 size_t maxlogsize; /* logarithm of maximum size of chunk with base 2 */
42 size_t minlogsize; /* logarithm of minimum size of chunk with base 2 */
43 #ifdef MP_STATS
44 size_t nsplits; /* number of splits made */
45 size_t nmerges; /* number of merges made */
46 #endif
47 LIST_HEAD(blkhead, blknode) *blktable;
48 } mpool_t;
50 typedef struct blkhead blkhead_t;
52 typedef enum {
53 MPOOL_OK,
54 MPOOL_EBADVAL,
55 MPOOL_ENOMEM
56 } mpret_t;
58 /* Function prototypes */
59 mpret_t mpool_init(mpool_t **mpool, size_t maxlogsize, size_t minlogsize);
60 void *mpool_alloc(mpool_t *mpool, size_t blksize);
61 void mpool_free(mpool_t *mpool, void *ptr);
62 void mpool_destroy(mpool_t *mpool);
64 #endif /* MPOOL_H_ */