summaryrefslogtreecommitdiff
path: root/src/modules/eina/mp/chained_pool/eina_chained_mempool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/eina/mp/chained_pool/eina_chained_mempool.c')
-rw-r--r--src/modules/eina/mp/chained_pool/eina_chained_mempool.c74
1 files changed, 63 insertions, 11 deletions
diff --git a/src/modules/eina/mp/chained_pool/eina_chained_mempool.c b/src/modules/eina/mp/chained_pool/eina_chained_mempool.c
index 6d4facf..261b02c 100644
--- a/src/modules/eina/mp/chained_pool/eina_chained_mempool.c
+++ b/src/modules/eina/mp/chained_pool/eina_chained_mempool.c
@@ -182,16 +182,10 @@ _eina_chained_mempool_usage_cmp(const Eina_Inlist *l1, const Eina_Inlist *l2)
182static void * 182static void *
183_eina_chained_mempool_alloc_in(Chained_Mempool *pool, Chained_Pool *p) 183_eina_chained_mempool_alloc_in(Chained_Mempool *pool, Chained_Pool *p)
184{ 184{
185 void *mem; 185 void *mem = NULL;
186 186
187 if (p->last) 187 // Let's try to first recycle memory
188 { 188 if (p->base)
189 mem = p->last;
190 p->last += pool->item_alloc;
191 if (p->last >= p->limit)
192 p->last = NULL;
193 }
194 else
195 { 189 {
196#ifndef NVALGRIND 190#ifndef NVALGRIND
197 VALGRIND_MAKE_MEM_DEFINED(p->base, pool->item_alloc); 191 VALGRIND_MAKE_MEM_DEFINED(p->base, pool->item_alloc);
@@ -199,6 +193,13 @@ _eina_chained_mempool_alloc_in(Chained_Mempool *pool, Chained_Pool *p)
199 // Request a free pointer 193 // Request a free pointer
200 mem = eina_trash_pop(&p->base); 194 mem = eina_trash_pop(&p->base);
201 } 195 }
196 else if (p->last)
197 {
198 mem = p->last;
199 p->last += pool->item_alloc;
200 if (p->last >= p->limit)
201 p->last = NULL;
202 }
202 203
203 // move to end - it just filled up 204 // move to end - it just filled up
204 if (!p->base && !p->last) 205 if (!p->base && !p->last)
@@ -306,7 +307,7 @@ eina_chained_mempool_malloc(void *data, EINA_UNUSED unsigned int size)
306 307
307 // we have reached the end of the list - no free pools 308 // we have reached the end of the list - no free pools
308 if (!p) 309 if (!p)
309 { 310 {
310 //new chain created ,point it to be the first_fill chain 311 //new chain created ,point it to be the first_fill chain
311 pool->first_fill = _eina_chained_mp_pool_new(pool); 312 pool->first_fill = _eina_chained_mp_pool_new(pool);
312 if (!pool->first_fill) 313 if (!pool->first_fill)
@@ -370,6 +371,56 @@ eina_chained_mempool_free(void *data, void *ptr)
370 return; 371 return;
371} 372}
372 373
374static void *
375eina_chained_mempool_malloc_near(void *data,
376 void *after, void *before,
377 unsigned int size EINA_UNUSED)
378{
379 Chained_Mempool *pool = data;
380 Chained_Pool *p = NULL;
381 void *mem = NULL;
382
383 if (!eina_spinlock_take(&pool->mutex))
384 {
385#ifdef EINA_HAVE_DEBUG_THREADS
386 assert(eina_thread_equal(pool->self, eina_thread_self()));
387#endif
388 }
389
390 if (after)
391 {
392 Eina_Rbtree *r = eina_rbtree_inline_lookup(pool->root, after,
393 0, _eina_chained_mp_pool_key_cmp, NULL);
394
395 if (r)
396 {
397 p = EINA_RBTREE_CONTAINER_GET(r, Chained_Pool);
398
399 if (!p->base && !p->last)
400 p = NULL;
401 }
402 }
403
404 if (before && p == NULL)
405 {
406 Eina_Rbtree *r = eina_rbtree_inline_lookup(pool->root, before,
407 0, _eina_chained_mp_pool_key_cmp, NULL);
408 if (r)
409 {
410 p = EINA_RBTREE_CONTAINER_GET(r, Chained_Pool);
411 if (!p->base && !p->last)
412 p = NULL;
413 }
414 }
415
416 if (p) mem = _eina_chained_mempool_alloc_in(pool, p);
417
418 eina_spinlock_release(&pool->mutex);
419
420 if (!mem) return eina_chained_mempool_malloc(pool, size);
421 return mem;
422}
423
373static Eina_Bool 424static Eina_Bool
374eina_chained_mempool_from(void *data, void *ptr) 425eina_chained_mempool_from(void *data, void *ptr)
375{ 426{
@@ -725,7 +776,8 @@ static Eina_Mempool_Backend _eina_chained_mp_backend = {
725 &eina_chained_mempool_shutdown, 776 &eina_chained_mempool_shutdown,
726 &eina_chained_mempool_repack, 777 &eina_chained_mempool_repack,
727 &eina_chained_mempool_from, 778 &eina_chained_mempool_from,
728 &eina_chained_mempool_iterator_new 779 &eina_chained_mempool_iterator_new,
780 &eina_chained_mempool_malloc_near
729}; 781};
730 782
731Eina_Bool chained_init(void) 783Eina_Bool chained_init(void)