7 /*#define MPOOL_DEBUG*/
8 /*#define MPOOL_OPT_FOR_SECURITY*/
12 #define DPRINTF(a) printf a
17 /* `flags' bits in blknode data structure */
18 #define MPOOL_NODE_AVAIL (1 << 0) /* If not set, node is reserved, else available */
19 #define MPOOL_NODE_LR (1 << 1) /* If not set, node is left buddy, else right buddy */
20 #define MPOOL_NODE_PARENT (1 << 2) /* If not set, parent is left buddy, else right buddy */
22 #define MPOOL_BLOCK_USED 0 /* Block is used */
23 #define MPOOL_BLOCK_AVAIL 1 /* Block is available */
24 #define MPOOL_BLOCK_LEFT 2 /* Block is left buddy */
25 #define MPOOL_BLOCK_RIGHT 3 /* Block is right buddy */
26 #define MPOOL_BLOCK_PARENT 4 /* Block's parent is right buddy */
28 /* Macro definitions */
29 #define MPOOL_MARK_AVAIL(pnode) pnode->flags |= MPOOL_NODE_AVAIL
30 #define MPOOL_MARK_USED(pnode) pnode->flags &= ~MPOOL_NODE_AVAIL
31 #define MPOOL_MARK_LEFT(pnode) pnode->flags &= ~MPOOL_NODE_LR
32 #define MPOOL_MARK_RIGHT(pnode) pnode->flags |= MPOOL_NODE_LR
33 #define MPOOL_MARK_PARENT(pnode) pnode->flags |= MPOOL_NODE_PARENT
34 #define MPOOL_MARK_NOTPARENT(pnode) pnode->flags &= ~MPOOL_NODE_PARENT
36 #define MPOOL_IS_AVAIL(pnode) ((pnode->flags & MPOOL_NODE_AVAIL) != 0)
37 #define MPOOL_IS_USED(pnode) ((pnode->flags & MPOOL_NODE_AVAIL) == 0)
38 #define MPOOL_IS_LEFT(pnode) ((pnode->flags & MPOOL_NODE_LR) == 0)
39 #define MPOOL_IS_RIGHT(pnode) ((pnode->flags & MPOOL_NODE_LR) != 0)
40 #define MPOOL_IS_PARENT(pnode) ((pnode->flags & MPOOL_NODE_PARENT) != 0)
42 /* This macro is provided for easy initialization of a blknode structure */
43 #define MPOOL_BLOCK_INIT(_node, _base, _ptr, _avail, _lr, _parent, _logsize) \
47 _node->logsize = _logsize; \
50 if ((_avail) == MPOOL_BLOCK_AVAIL) \
51 _node->flags |= MPOOL_NODE_AVAIL; \
53 _node->flags &= ~MPOOL_NODE_AVAIL; \
55 /* Left-Right relationship */ \
56 if ((_lr) == MPOOL_BLOCK_RIGHT) \
57 _node->flags |= MPOOL_NODE_LR; \
59 _node->flags &= ~MPOOL_NODE_LR; \
61 /* Parent L-R relationship */ \
62 if ((_parent) == MPOOL_BLOCK_PARENT) \
63 _node->flags |= MPOOL_NODE_PARENT; \
65 _node->flags &= ~MPOOL_NODE_PARENT; \
68 typedef struct blknode
{
69 unsigned char flags
; /* availability, left-right buddiness, inheritance */
70 size_t logsize
; /* logarithm of size with base 2 */
71 void *ptr
; /* pointer to beginning of free block (what mpool_alloc() returns) */
72 LIST_ENTRY(blknode
) next_chunk
;
75 typedef struct mpool
{
77 size_t nblocks
; /* nblocks = logsize + 1 */
78 size_t maxlogsize
; /* logarithm of maximum size of chunk with base 2 */
79 size_t minlogsize
; /* logarithm of minimum size of chunk with base 2 */
81 size_t nsplits
; /* number of splits made */
82 size_t nmerges
; /* number of merges made */
84 LIST_HEAD(blkhead
, blknode
) *blktable
;
87 typedef struct blkhead blkhead_t
;
96 /* Function prototypes */
97 mpret_t
mpool_init(mpool_t
**mpool
, size_t maxlogsize
, size_t minlogsize
);
98 void *mpool_alloc(mpool_t
*mpool
, size_t blksize
);
99 void mpool_free(mpool_t
*mpool
, void *ptr
);
100 void mpool_destroy(mpool_t
*mpool
);
102 #endif /* MPOOL_H_ */