1 /* EINA - EFL data type library
2 * Copyright (C) 2009 Jorge Luis Zapata Muga
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
20 * This is a naive 'buddy' allocator following Knuth's documentation.
21 * The main difference is that we dont store the block information
22 * on the block memory itself but on another malloc'd area.
23 * This is useful for managing memory which isn't as fast as the main
24 * memory like the video memory
25 * The algorithm uses an area to store the linked list of blocks.
26 * Each block size is equal to the minimum allocatable block size for
27 * the memory pool and the number of blocks is equal to the size of the
28 * memory pool divided by the block size.
36 #include "eina_types.h"
37 #include "eina_inlist.h"
38 #include "eina_module.h"
39 #include "eina_mempool.h"
40 #include "eina_private.h"
45 Eina_Bool available : 1;
46 unsigned short int order : 7; /* final order is order + min_order */
51 void *heap; /* start address of the heap */
52 size_t size; /* total size in bytes of the heap */
53 unsigned int min_order; /* minimum size is 1 << min_order */
54 unsigned int max_order; /* maximum size is 1 << max_order */
55 unsigned int num_order; /* number of orders */
56 Eina_Inlist **areas; /* one area per order */
57 Block *blocks; /* the allocated block information */
60 /* get the minimum order greater or equal to size */
61 static inline unsigned int _get_order(Buddy *b, size_t size)
66 bytes = 1 << b->min_order;
67 for (i = 0; bytes < size && i < b->num_order; i++)
71 //printf("order for size %d is %d\n", size, i + b->min_order);
75 static inline void *_get_offset(Buddy *b, Block *block)
79 ret = (char *)b->heap + ((block - &b->blocks[0]) << b->min_order);
83 static void *_init(__UNUSED__ const char *context,
84 __UNUSED__ const char *options,
94 heap = va_arg(args, void *);
95 size = va_arg(args, size_t);
96 min_order = va_arg(args, int);
97 /* the minimum order we support is 15 (32K) */
98 min_order = min_order < 15 ? 15 : min_order;
99 bytes = 1 << min_order;
100 for (i = 0; bytes <= size; i++)
107 b = malloc(sizeof(Buddy));
110 b->min_order = min_order;
111 b->max_order = min_order + i - 1;
113 b->areas = calloc(b->num_order, sizeof(Eina_Inlist *));
114 b->blocks = calloc(1 << (b->num_order - 1), sizeof(Block));
115 /* setup the initial free area */
116 b->blocks[0].available = EINA_TRUE;
117 b->areas[b->num_order - 1] = EINA_INLIST_GET(&(b->blocks[0]));
122 static void _shutdown(void *data)
131 static void _free(void *data, void *element)
134 Block *block, *buddy;
138 offset = (unsigned char *)element - (unsigned char *)b->heap;
139 if (offset > b->size)
142 idx = offset >> b->min_order;
143 block = &b->blocks[idx];
145 //printf("free %x idx = %d order = %d buddy = %d\n", offset, idx, block->order, idx ^ (1 << block->order));
146 /* we should always work with the buddy at right */
147 if (idx & (1 << block->order))
151 idx = idx ^ (1 << block->order);
152 left = &b->blocks[idx];
153 if (!left->available)
159 b->areas[block->order] = eina_inlist_remove(b->areas[block->order],
160 EINA_INLIST_GET(block));
166 /* already on the last order */
167 if (block->order + b->min_order == b->max_order)
169 goto end; /* get the buddy */
173 buddy = &b->blocks[idx ^ (1 << block->order)];
174 if (!buddy->available)
176 goto end; /* merge two blocks */
180 b->areas[block->order] = eina_inlist_remove(b->areas[block->order],
181 EINA_INLIST_GET(buddy));
185 /* add the block to the free list */
186 block->available = EINA_TRUE;
187 b->areas[block->order] = eina_inlist_append(b->areas[block->order],
188 EINA_INLIST_GET(block));
191 static void *_alloc(void *data, unsigned int size)
194 Block *block, *buddy;
197 k = j = _get_order(b, size);
198 /* get a free list of order k where k <= j <= max_order */
199 while ((j < b->num_order) && !b->areas[j])
201 /* check that the order is on our range */
202 if (j + b->min_order > b->max_order)
205 /* get a free element on this order, if not, go splitting until we find one */
206 //printf("getting order %d (%d) for size %d\n", j, k, size);
212 block = EINA_INLIST_CONTAINER_GET(b->areas[j], Block);
213 block->available = EINA_FALSE;
215 /* remove the block from the list */
216 b->areas[j] = eina_inlist_remove(b->areas[j], EINA_INLIST_GET(block));
217 ret = _get_offset(b, block);
222 block = EINA_INLIST_CONTAINER_GET(b->areas[j], Block);
224 b->areas[j] = eina_inlist_remove(b->areas[j], EINA_INLIST_GET(block));
226 b->areas[j] = eina_inlist_append(b->areas[j], EINA_INLIST_GET(block));
227 buddy = block + (1 << j);
229 buddy->available = EINA_TRUE;
230 b->areas[j] = eina_inlist_append(b->areas[j], EINA_INLIST_GET(buddy));
235 static void _statistics(void *data)
240 printf("Information:\n");
242 "size = %zu, min_order = %d, max_order = %d, num_order = %d, num_blocks = %d (%uKB)\n",
248 ((1 << (b->num_order)) * sizeof(Block)) / 1024);
249 printf("Area dumping:");
250 /* iterate over the free lists and dump the maps */
251 for (i = 0; i < b->num_order; i++)
255 printf("\n2^%d:", b->min_order + i);
256 EINA_INLIST_FOREACH(b->areas[i], block)
258 printf(" %d", (block - &b->blocks[0]));
261 printf("\nBlocks dumping:\n");
264 static Eina_Mempool_Backend _backend = {
270 NULL, /* garbage collect */
276 Eina_Bool buddy_init(void)
278 return eina_mempool_register(&_backend);
281 void buddy_shutdown(void)
283 eina_mempool_unregister(&_backend);
287 #ifndef EINA_STATIC_BUILD_BUDDY
289 EINA_MODULE_INIT(buddy_init);
290 EINA_MODULE_SHUTDOWN(buddy_shutdown);
292 #endif /* ! EINA_STATIC_BUILD_BUDDY */