1 /* EINA - EFL data type library
2 * Copyright (C) 2008-2010 Cedric BAIL, Vincent Torri
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/>.
26 #ifdef EFL_HAVE_POSIX_THREADS
29 # ifdef EFL_DEBUG_THREADS
34 #ifdef EINA_DEBUG_MALLOC
38 #ifdef EFL_HAVE_WIN32_THREADS
39 # define WIN32_LEAN_AND_MEAN
41 # undef WIN32_LEAN_AND_MEAN
44 #include "eina_inlist.h"
45 #include "eina_error.h"
46 #include "eina_module.h"
47 #include "eina_mempool.h"
48 #include "eina_trash.h"
49 #include "eina_rbtree.h"
50 #include "eina_lock.h"
52 #include "eina_private.h"
55 # include <memcheck.h>
58 #if defined DEBUG || defined EINA_DEBUG_MALLOC
62 static int _eina_chained_mp_log_dom = -1;
67 #define INF(...) EINA_LOG_DOM_INFO(_eina_chained_mp_log_dom, __VA_ARGS__)
70 typedef struct _Chained_Mempool Chained_Mempool;
71 struct _Chained_Mempool
81 #ifdef EINA_DEBUG_MALLOC
84 #ifdef EFL_DEBUG_THREADS
90 typedef struct _Chained_Pool Chained_Pool;
102 static inline Eina_Rbtree_Direction
103 _eina_chained_mp_pool_cmp(const Eina_Rbtree *left, const Eina_Rbtree *right, __UNUSED__ void *data)
105 if (left < right) return EINA_RBTREE_LEFT;
106 return EINA_RBTREE_RIGHT;
110 _eina_chained_mp_pool_key_cmp(const Eina_Rbtree *node, const void *key,
111 __UNUSED__ int length, __UNUSED__ void *data)
113 const Chained_Pool *r = EINA_RBTREE_CONTAINER_GET(node, const Chained_Pool);
115 if (key > (void *) r->limit) return -1;
116 if (key < (void *) r) return 1;
120 static inline Chained_Pool *
121 _eina_chained_mp_pool_new(Chained_Mempool *pool)
125 unsigned int alignof;
128 p = malloc(pool->alloc_size);
131 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
135 #ifdef EINA_DEBUG_MALLOC
139 sz = malloc_usable_size(p);
140 if (sz - pool->minimal_size > 0)
141 INF("Just allocated %0.2f%% to much memory in '%s' for one block of size %i that means %i bytes to much.",
142 ((float)(sz - pool->minimal_size) * 100) / (float) (pool->alloc_size),
145 sz - pool->minimal_size);
149 alignof = eina_mempool_alignof(sizeof(Chained_Pool));
150 ptr = (unsigned char *)p + alignof;
155 p->limit = ptr + pool->item_alloc * pool->pool_size;
158 VALGRIND_MAKE_MEM_NOACCESS(ptr, pool->alloc_size - alignof);
165 _eina_chained_mp_pool_free(Chained_Pool *p)
171 _eina_chained_mempool_usage_cmp(const Eina_Inlist *l1, const Eina_Inlist *l2)
173 const Chained_Pool *p1;
174 const Chained_Pool *p2;
176 p1 = EINA_INLIST_CONTAINER_GET(l1, const Chained_Pool);
177 p2 = EINA_INLIST_CONTAINER_GET(l2, const Chained_Pool);
179 return p2->usage - p1->usage;
183 _eina_chained_mempool_alloc_in(Chained_Mempool *pool, Chained_Pool *p)
190 p->last += pool->item_alloc;
191 if (p->last >= p->limit)
197 VALGRIND_MAKE_MEM_DEFINED(p->base, pool->item_alloc);
199 // Request a free pointer
200 mem = eina_trash_pop(&p->base);
203 // move to end - it just filled up
204 if (!p->base && !p->last)
205 pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
211 VALGRIND_MEMPOOL_ALLOC(pool, mem, pool->item_alloc);
218 _eina_chained_mempool_free_in(Chained_Mempool *pool, Chained_Pool *p, void *ptr)
223 pmem = (void *)(((unsigned char *)p) + sizeof(Chained_Pool));
225 // is it in pool mem?
229 INF("%p is inside the private part of %p pool from %p Chained_Mempool (could be the sign of a buffer underrun).", ptr, p, pool);
234 // freed node points to prev free node
235 eina_trash_push(&p->base, ptr);
236 // next free node is now the one we freed
242 pool->first = eina_inlist_remove(pool->first, EINA_INLIST_GET(p));
243 pool->root = eina_rbtree_inline_remove(pool->root, EINA_RBTREE_GET(p),
244 _eina_chained_mp_pool_cmp, NULL);
245 _eina_chained_mp_pool_free(p);
252 pool->first = eina_inlist_promote(pool->first, EINA_INLIST_GET(p));
259 eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
261 Chained_Mempool *pool = data;
262 Chained_Pool *p = NULL;
265 if (!eina_lock_take(&pool->mutex))
267 #ifdef EFL_DEBUG_THREADS
268 assert(pthread_equal(pool->self, pthread_self()));
272 // Either we have some free space in the first one, or there is no free space.
273 if (pool->first) p = EINA_INLIST_CONTAINER_GET(pool->first, Chained_Pool);
275 // base is not NULL - has a free slot
276 if (p && !p->base && !p->last)
281 EINA_INLIST_FOREACH(pool->first, p)
282 assert(!p->base && !p->last);
285 // we have reached the end of the list - no free pools
288 p = _eina_chained_mp_pool_new(pool);
291 eina_lock_release(&pool->mutex);
295 pool->first = eina_inlist_prepend(pool->first, EINA_INLIST_GET(p));
296 pool->root = eina_rbtree_inline_insert(pool->root, EINA_RBTREE_GET(p),
297 _eina_chained_mp_pool_cmp, NULL);
300 mem = _eina_chained_mempool_alloc_in(pool, p);
302 eina_lock_release(&pool->mutex);
308 eina_chained_mempool_free(void *data, void *ptr)
310 Chained_Mempool *pool = data;
315 if (!eina_lock_take(&pool->mutex))
317 #ifdef EFL_DEBUG_THREADS
318 assert(pthread_equal(pool->self, pthread_self()));
322 // searching for the right mempool
323 r = eina_rbtree_inline_lookup(pool->root, ptr, 0, _eina_chained_mp_pool_key_cmp, NULL);
325 // related mempool not found
329 INF("%p is not the property of %p Chained_Mempool", ptr, pool);
334 p = EINA_RBTREE_CONTAINER_GET(r, Chained_Pool);
336 _eina_chained_mempool_free_in(pool, p, ptr);
342 VALGRIND_MEMPOOL_FREE(pool, ptr);
346 eina_lock_release(&pool->mutex);
351 eina_chained_mempool_repack(void *data,
352 Eina_Mempool_Repack_Cb cb,
355 Chained_Mempool *pool = data;
359 /* FIXME: Improvement - per Chained_Pool lock */
360 if (!eina_lock_take(&pool->mutex))
362 #ifdef EFL_DEBUG_THREADS
363 assert(pthread_equal(pool->self, pthread_self()));
367 pool->first = eina_inlist_sort(pool->first,
368 (Eina_Compare_Cb) _eina_chained_mempool_usage_cmp);
371 idea : remove the almost empty pool at the beginning of the list by
372 moving data in the last pool with empty slot
374 tail = EINA_INLIST_CONTAINER_GET(pool->first->last, Chained_Pool);
375 while (tail && tail->usage == pool->pool_size)
376 tail = EINA_INLIST_CONTAINER_GET((EINA_INLIST_GET(tail)->prev), Chained_Pool);
383 start = EINA_INLIST_CONTAINER_GET(pool->first, Chained_Pool);
385 if (start == tail || start->usage == pool->pool_size)
388 for (src = start->limit - pool->group_size;
390 src += pool->item_alloc)
392 Eina_Bool is_free = EINA_FALSE;
395 /* Do we have something inside that piece of memory */
396 if (start->last != NULL && src >= start->last)
402 Eina_Trash *over = start->base;
404 while (over != NULL && (unsigned char*) over != src)
411 if (is_free) continue ;
413 /* get a new memory pointer from the latest most occuped pool */
414 dst = _eina_chained_mempool_alloc_in(pool, tail);
415 /* move data from one to another */
416 memcpy(dst, src, pool->item_alloc);
418 cb(dst, src, cb_data);
419 /* destroy old pointer */
420 is_dead = _eina_chained_mempool_free_in(pool, start, src);
422 /* search last tail with empty slot */
423 while (tail && tail->usage == pool->pool_size)
424 tail = EINA_INLIST_CONTAINER_GET((EINA_INLIST_GET(tail)->prev),
426 /* no more free space */
427 if (!tail || tail == start) break;
432 /* FIXME: improvement - reorder pool so that the most used one get in front */
433 eina_lock_release(&pool->mutex);
437 eina_chained_mempool_realloc(__UNUSED__ void *data,
438 __UNUSED__ void *element,
439 __UNUSED__ unsigned int size)
445 eina_chained_mempool_init(const char *context,
446 __UNUSED__ const char *option,
453 length = context ? strlen(context) + 1 : 0;
455 mp = calloc(1, sizeof(Chained_Mempool) + length);
459 item_size = va_arg(args, int);
460 mp->pool_size = va_arg(args, int);
464 mp->name = (const char *)(mp + 1);
465 memcpy((char *)mp->name, context, length);
468 #ifdef EINA_DEBUG_MALLOC
469 mp->minimal_size = item_size * mp->pool_size + sizeof(Chained_Pool);
472 mp->item_alloc = eina_mempool_alignof(item_size);
473 mp->group_size = mp->item_alloc * mp->pool_size;
474 mp->alloc_size = mp->group_size + eina_mempool_alignof(sizeof(Chained_Pool));
477 VALGRIND_CREATE_MEMPOOL(mp, 0, 1);
480 #ifdef EFL_DEBUG_THREADS
481 mp->self = pthread_self();
484 eina_lock_new(&mp->mutex);
490 eina_chained_mempool_shutdown(void *data)
494 mp = (Chained_Mempool *)data;
498 Chained_Pool *p = (Chained_Pool *)mp->first;
502 INF("Bad news we are destroying not an empty mempool [%s]\n",
507 mp->first = eina_inlist_remove(mp->first, mp->first);
508 mp->root = eina_rbtree_inline_remove(mp->root, EINA_RBTREE_GET(p),
509 _eina_chained_mp_pool_cmp, NULL);
510 _eina_chained_mp_pool_free(p);
515 INF("Bad news, list of pool and rbtree are out of sync for %p !", mp);
519 VALGRIND_DESTROY_MEMPOOL(mp);
522 eina_lock_free(&mp->mutex);
524 #ifdef EFL_DEBUG_THREADS
525 assert(pthread_equal(mp->self, pthread_self()));
531 static Eina_Mempool_Backend _eina_chained_mp_backend = {
533 &eina_chained_mempool_init,
534 &eina_chained_mempool_free,
535 &eina_chained_mempool_malloc,
536 &eina_chained_mempool_realloc,
539 &eina_chained_mempool_shutdown,
540 &eina_chained_mempool_repack
543 Eina_Bool chained_init(void)
545 #if defined DEBUG || defined EINA_DEBUG_MALLOC
546 _eina_chained_mp_log_dom = eina_log_domain_register("eina_mempool",
547 EINA_LOG_COLOR_DEFAULT);
548 if (_eina_chained_mp_log_dom < 0)
550 EINA_LOG_ERR("Could not register log domain: eina_mempool");
555 return eina_mempool_register(&_eina_chained_mp_backend);
558 void chained_shutdown(void)
560 eina_mempool_unregister(&_eina_chained_mp_backend);
561 #if defined DEBUG || defined EINA_DEBUG_MALLOC
562 eina_log_domain_unregister(_eina_chained_mp_log_dom);
563 _eina_chained_mp_log_dom = -1;
567 #ifndef EINA_STATIC_BUILD_CHAINED_POOL
569 EINA_MODULE_INIT(chained_init);
570 EINA_MODULE_SHUTDOWN(chained_shutdown);
572 #endif /* ! EINA_STATIC_BUILD_CHAINED_POOL */