2 * This is an implementation of a buddy memory allocator.
3 * For a description of such a system, please refer to:
5 * The Art of Computer Programming Vol. I, by Donald E. Knuth
10 #include <limits.h> /* for CHAR_BIT */
14 /* Function prototypes */
15 static void mpool_printblks(const mpool_t
*mpool
);
17 mpret_t
mpool_init(mpool_t
**mpool
, size_t maxlogsize
, size_t minlogsize
)
23 if (maxlogsize
> sizeof(size_t) * CHAR_BIT
)
25 if (maxlogsize
< minlogsize
|| (size_t)(1 << minlogsize
) <= sizeof *pblknode
)
28 /* Allocate memory for memory pool data structure */
29 if ((*mpool
= malloc(sizeof **mpool
)) == NULL
)
32 /* Initialize mpool members */
33 (*mpool
)->maxlogsize
= maxlogsize
;
34 (*mpool
)->minlogsize
= minlogsize
;
35 (*mpool
)->nblocks
= maxlogsize
- minlogsize
+ 1;
37 (*mpool
)->nsplits
= 0;
38 (*mpool
)->nmerges
= 0;
41 /* Allocate the actual memory of the pool */
42 if (((*mpool
)->mem
= malloc((size_t)(1 << maxlogsize
))) == NULL
) {
46 DPRINTF(("maxlogsize = %u\tminlogsize = %u\tnblocks = %u\n" \
47 "mpool->mem = %p\tsizeof(blknode) = 0x%x\n",
53 DPRINTF(("Allocated %u bytes for pool\n", 1 << maxlogsize
));
55 /* Allocate memory for block lists */
56 if (((*mpool
)->blktable
= malloc((*mpool
)->nblocks
*
57 sizeof *(*mpool
)->blktable
)) == NULL
) {
63 /* Initialize block lists */
64 for (i
= 0; i
< (*mpool
)->nblocks
; i
++)
65 LIST_INIT(&(*mpool
)->blktable
[i
]);
68 * Initially, before any storage has been requested, we have a single
69 * available block of length 2^maxlogsize in blktable[0].
71 MPOOL_BLOCK_INIT(pblknode
,
73 (char *)(*mpool
)->mem
+ sizeof *pblknode
,
75 MPOOL_BLOCK_LEFT
, /* irrelevant */
76 MPOOL_BLOCK_PARENT
, /* irrelevant */
79 /* Insert block to the first block list */
80 LIST_INSERT_HEAD(&(*mpool
)->blktable
[0], pblknode
, next_chunk
);
81 mpool_printblks(*mpool
);
86 void *mpool_alloc(mpool_t
*mpool
, size_t blksize
)
90 blknode_t
*pavailnode
;
92 size_t i
, newpos
, size
;
96 * Total size is the sum of the user's request plus the overhead of a
97 * blknode_t data structure. Be aware for the particular scenario, when
98 * requested size is of the form 2^j. The allocator will then return
99 * the next bigger memory chunk, leading to high internal fragmentation.
101 size
= blksize
+ sizeof *pnode
;
103 DPRINTF(("\n;--------------------------------------------------------;\n"));
104 DPRINTF(("Searching for block of bytes: %u + %u = %u\n",
105 blksize
, sizeof *pnode
, size
));
108 * Find the most suitable 2^j bytes block for the requested size of bytes.
109 * The condition 2^j >= size must be satisfied for the smallest possible
110 * value of j and the block must be marked as available ofcourse.
113 for (i
= 0; i
< mpool
->nblocks
; i
++) {
114 DPRINTF(("Searching block: %u\n", i
));
115 phead
= &mpool
->blktable
[i
];
116 if ((pnode
= LIST_FIRST(phead
)) != NULL
) {
117 if ((size_t)(1 << pnode
->logsize
) >= size
) {
118 LIST_FOREACH(pnode
, phead
, next_chunk
) {
119 if (MPOOL_IS_AVAIL(pnode
)) {
121 goto NEXT_BLOCK_LIST
;
129 /* Failure, no available block */
130 if (pavailnode
== NULL
) {
131 DPRINTF(("No available block found\n"));
134 DPRINTF(("Found block of bytes %u\n", 1 << pavailnode
->logsize
));
136 /* Is a split required ? */
138 DPRINTF(("size = %u\tp = %u\tp-1 = %u\n",
140 1 << pavailnode
->logsize
,
141 1 << (pavailnode
->logsize
- 1)));
144 * We don't need to split the chunk we just found,
145 * if one at least of the following statements is true:
147 * - ``size'' bytes fit exactly in the chunk
148 * - ``size'' bytes won't fit in the splitted chunk
149 * - ``minlogsize'' constraint will be violated if we split
151 * NOTE: log2(size/2) = log2(size) - log2(2) = log2(size) - 1
153 if ((size
== (size_t)(1 << pavailnode
->logsize
))
154 || (size
> (size_t)(1 << (pavailnode
->logsize
- 1)))
155 || (mpool
->minlogsize
> (pavailnode
->logsize
- 1))) {
156 DPRINTF(("No split required\n"));
157 MPOOL_MARK_USED(pavailnode
);
158 mpool_printblks(mpool
);
159 return pavailnode
->ptr
;
162 DPRINTF(("Splitting...\n"));
167 /* Remove old chunk */
168 DPRINTF(("Removing old chunk from list\n"));
169 LIST_REMOVE(pavailnode
, next_chunk
);
170 mpool_printblks(mpool
);
172 /* Calculate new size */
173 pavailnode
->logsize
--;
174 DPRINTF(("New size is now: %u bytes\n", 1 << pavailnode
->logsize
));
177 flag
= pavailnode
->flags
;
178 if (MPOOL_IS_RIGHT(pavailnode
))
179 MPOOL_MARK_PARENT(pavailnode
);
181 MPOOL_MARK_NOTPARENT(pavailnode
);
182 MPOOL_MARK_LEFT(pavailnode
);
184 /* Calculate new position of chunk and insert it there */
185 newpos
= mpool
->maxlogsize
- pavailnode
->logsize
;
186 DPRINTF(("Moving old chunk to new position: %u\n", newpos
));
187 LIST_INSERT_HEAD(&mpool
->blktable
[newpos
], pavailnode
, next_chunk
);
188 mpool_printblks(mpool
);
191 DPRINTF(("Will add new item with bytes: %u (0x%x)\n",
192 1 << pavailnode
->logsize
,
193 1 << pavailnode
->logsize
));
195 MPOOL_BLOCK_INIT(pnewnode
,
196 MPOOL_GET_RIGHT_BUDDY_OF(pavailnode
),
197 (char *)pnewnode
+ sizeof *pnewnode
,
200 (flag
& MPOOL_NODE_PARENT
) ? MPOOL_BLOCK_PARENT
: -1,
201 pavailnode
->logsize
);
203 LIST_INSERT_HEAD(&mpool
->blktable
[newpos
], pnewnode
, next_chunk
);
204 mpool_printblks(mpool
);
210 void mpool_free(mpool_t
*mpool
, void *ptr
)
213 blknode_t
*pnode
, *pbuddy
, *pmerged
;
216 DPRINTF(("[ Freeing ptr: %p ]\n", ptr
));
218 #ifdef MPOOL_OPT_FOR_SECURITY
219 /* Search all nodes to find the one that points to ``ptr'' */
222 for (i
= 0; i
< mpool
->nblocks
; i
++) {
223 DPRINTF(("Searching for ptr %p in block: %u\n", ptr
, i
));
224 phead
= &mpool
->blktable
[i
];
225 LIST_FOREACH(pnode
, phead
, next_chunk
) {
226 if (pnode
->ptr
== ptr
) {
227 DPRINTF(("Found chunk at block: %u\t"
228 "Block has chunks with bytes: %u\n",
229 i
, 1 << pnode
->logsize
));
236 * Chunk isn't in our pool, this is probably bad.
238 * It means that either the user has provided an invalid pointer to free or
239 * the allocator exhibited buggy behaviour and corrupted itself. Either way,
240 * return immediately.
242 DPRINTF(("Chunk %p was not found in the pool\n", ptr
));
245 pnode
= (blknode_t
*)((char *)ptr
- sizeof *pnode
);
249 /* Are we top level ? */
250 if (pnode
->logsize
== mpool
->maxlogsize
) {
251 if (MPOOL_IS_AVAIL(pnode
) == 0)
252 MPOOL_MARK_AVAIL(pnode
);
256 /* Calculate possible buddy of chunk */
257 DPRINTF(("Searching for buddy of %p...\n", pnode
->ptr
));
259 /* ``pnode'' is a right buddy, so ``pbuddy'' is a left buddy */
260 if (MPOOL_IS_RIGHT(pnode
)) {
261 pbuddy
= MPOOL_GET_LEFT_BUDDY_OF(pnode
);
262 if ((void *)pbuddy
< (void *)mpool
->mem
) {
263 DPRINTF(("buddy out of pool\n"));
267 /* ``pnode'' is a left buddy, so ``pbuddy'' is a right buddy */
269 pbuddy
= MPOOL_GET_RIGHT_BUDDY_OF(pnode
);
271 (void *)((char *)mpool
->mem
+ (1 << mpool
->maxlogsize
) - 1)) {
272 DPRINTF(("buddy out of pool\n"));
277 /* Buddies must be of the same size */
278 if (pbuddy
->logsize
!= pnode
->logsize
)
282 * If there is no buddy of ``pnode'' or if there is, but it's unavailable,
283 * just free ``pnode'' and we are done.
285 if (pbuddy
== NULL
|| (pbuddy
!= NULL
&& MPOOL_IS_USED(pbuddy
))) {
286 DPRINTF(("Not found or found but unavailable\n"));
287 DPRINTF(("Freeing chunk %p (marking it as available)\n", pnode
->ptr
));
288 MPOOL_MARK_AVAIL(pnode
);
289 mpool_printblks(mpool
);
293 * There is a buddy, and it's available for sure. Coalesce.
296 DPRINTF(("Buddy %p exists and it's available. Coalesce.\n",
301 /* Remove both ``pnode'' and ``pbuddy'' from block lists */
302 DPRINTF(("Removing chunk %p and buddy %p from old position %u\n",
303 pnode
->ptr
, pbuddy
->ptr
, mpool
->maxlogsize
- pnode
->logsize
));
304 LIST_REMOVE(pnode
, next_chunk
);
305 LIST_REMOVE(pbuddy
, next_chunk
);
306 mpool_printblks(mpool
);
309 if (MPOOL_IS_LEFT(pnode
)) {
310 /* ``pnode'' is left buddy */
313 if (MPOOL_IS_PARENT(pnode
))
314 MPOOL_MARK_RIGHT(pmerged
);
316 MPOOL_MARK_LEFT(pmerged
);
318 if (MPOOL_IS_PARENT(pbuddy
))
319 MPOOL_MARK_PARENT(pmerged
);
321 MPOOL_MARK_NOTPARENT(pmerged
);
323 else if (MPOOL_IS_LEFT(pbuddy
)) {
324 /* ``pbuddy'' is right buddy */
327 if (MPOOL_IS_PARENT(pbuddy
))
328 MPOOL_MARK_RIGHT(pmerged
);
330 MPOOL_MARK_LEFT(pmerged
);
332 if (MPOOL_IS_PARENT(pnode
))
333 MPOOL_MARK_PARENT(pmerged
);
335 MPOOL_MARK_NOTPARENT(pmerged
);
338 DPRINTF(("Chunk %p and buddy = %p have wrong LR relation\n",
339 pnode
->ptr
, pbuddy
->ptr
));
343 /* Calculate new size */
344 pmerged
->logsize
= pnode
->logsize
+ 1;
346 /* Mark ``pnode'' as available */
347 MPOOL_MARK_AVAIL(pmerged
);
349 /* Insert ``pnode'' to appropriate position in block table */
350 newpos
= mpool
->maxlogsize
- pmerged
->logsize
;
351 phead
= &mpool
->blktable
[newpos
];
352 LIST_INSERT_HEAD(phead
, pmerged
, next_chunk
);
353 mpool_printblks(mpool
);
361 void mpool_destroy(mpool_t
*mpool
)
363 free(mpool
->blktable
);
368 static void mpool_printblks(const mpool_t
*mpool
)
370 const blkhead_t
*phead
;
371 const blknode_t
*pnode
;
374 for (i
= 0; i
< mpool
->nblocks
; i
++) {
375 DPRINTF(("Block (%p): %u\t", mpool
->blktable
[i
], i
));
376 phead
= &mpool
->blktable
[i
];
377 LIST_FOREACH(pnode
, phead
, next_chunk
) {
378 DPRINTF(("ch(ad = %p, by = %u, av = %d, lr = %d, pa = %d)\t",
380 (unsigned) (1 << pnode
->logsize
),
381 MPOOL_IS_AVAIL(pnode
) ? 1 : 0,
382 MPOOL_IS_RIGHT(pnode
) ? 1 : 0,
383 MPOOL_IS_PARENT(pnode
) ? 1 : 0));