Module: xenomai-forge Branch: master Commit: fbb0c1c4ee4ed7bcc323c61de6f685ca87949318 URL: http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=fbb0c1c4ee4ed7bcc323c61de6f685ca87949318
Author: Philippe Gerum <r...@xenomai.org> Date: Tue Dec 13 17:55:03 2011 +0100 copperplate/tlsf: do not grow local heaps, fix main pool override The original TLSF implementation attempts to grow local heaps upon shortage, which does not fit our needs, we want the size mentioned at pool creation to be a hard limit. This patch enforces this. Additionally, we fix what looks like a bug in the original implementation, preventing the main pool pointer to be overriden by multiple calls to init_memory_pool(), which does not make any sense. This fix does not remove the race of multiple threads calling tlsf_alloc() concurrently with no active main pool, but we won't be hit by this issue (heapobj_pkg_init will do the first tlsf_alloc(), racelessly). The several bugs related to multi-threading support found in TLSF tend to make me nervous. At some point, we should investigate switching to tlsf.baisoku.org, or provide a home-made version to fix the broken TLSF interface to avoid these issues. --- lib/copperplate/tlsf/tlsf.c | 16 +++++++--------- 1 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/copperplate/tlsf/tlsf.c b/lib/copperplate/tlsf/tlsf.c index 0d89604..53ee1ce 100644 --- a/lib/copperplate/tlsf/tlsf.c +++ b/lib/copperplate/tlsf/tlsf.c @@ -451,7 +451,7 @@ static __inline__ bhdr_t *process_area(void *area, size_t size) /******************** Begin of the allocator code *****************/ /******************************************************************/ -static char *mp = NULL; /* Default memory pool. */ +static char *mp; /* Default memory pool. */ /******************************************************************/ size_t init_memory_pool(size_t mem_pool_size, void *mem_pool) @@ -461,24 +461,21 @@ size_t init_memory_pool(size_t mem_pool_size, void *mem_pool) bhdr_t *b, *ib; if (!mem_pool || !mem_pool_size || mem_pool_size < sizeof(tlsf_t) + BHDR_OVERHEAD * 8) { - ERROR_MSG("init_memory_pool (): memory_pool invalid\n"); + ERROR_MSG("init_memory_pool(): invalid pool\n"); return -1; } if (((unsigned long) mem_pool & PTR_MASK)) { - ERROR_MSG("init_memory_pool (): mem_pool must be aligned to a word\n"); + ERROR_MSG("init_memory_pool(): pool must be aligned to a word\n"); return -1; } tlsf = (tlsf_t *) mem_pool; /* Check if already initialised */ if (tlsf->tlsf_signature == TLSF_SIGNATURE) { - mp = mem_pool; - b = GET_NEXT_BLOCK(mp, ROUNDUP_SIZE(sizeof(tlsf_t))); - return b->size & BLOCK_SIZE; + ERROR_MSG("init_memory_pool(): already initialized\n"); + return -1; } - mp = mem_pool; - /* Zeroing the memory pool */ memset(mem_pool, 0, sizeof(tlsf_t)); @@ -633,6 +630,7 @@ void *tlsf_malloc(size_t size) if (area == ((void *) ~0)) return NULL; /* Not enough system memory */ init_memory_pool(area_size, area); + mp = area; } #endif @@ -712,7 +710,7 @@ void *malloc_ex(size_t size, void *mem_pool) so they are not longer valid when the function fails */ b = FIND_SUITABLE_BLOCK(tlsf, &fl, &sl); #if USE_MMAP || USE_SBRK - if (!b) { + if (!b && mem_pool == mp) { /* Don't grow private pools */ size_t area_size; void *area; /* Growing the pool size when needed */ _______________________________________________ Xenomai-git mailing list Xenomai-git@gna.org https://mail.gna.org/listinfo/xenomai-git