11 #define DPRINTF(a) printf a
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_MAKE_AVAIL(pnode) pnode->flags |= MP_NODE_AVAIL
22 #define MPOOL_MAKE_USED(pnode) pnode->flags &= ~MP_NODE_AVAIL
23 #define MPOOL_MAKE_LEFT(pnode) pnode->flags &= ~MP_NODE_LR
24 #define MPOOL_MAKE_RIGHT(pnode) pnode->flags |= MP_NODE_LR
26 #define MPOOL_IS_AVAIL(pnode) (pnode->flags & MP_NODE_AVAIL)
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)
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
;
38 typedef struct mpool
{
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 */
44 size_t nsplits
; /* number of splits made */
45 size_t nmerges
; /* number of merges made */
47 LIST_HEAD(blkhead
, blknode
) *blktable
;
50 typedef struct blkhead blkhead_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
);