From 250ee8cdd43ffa4ba9ecab7548b18c4fc91f849d Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 8 Sep 2008 14:06:38 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@428 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- src/chmempools.c | 21 +++++++++++++-------- src/include/mempools.h | 7 +++++-- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/chmempools.c b/src/chmempools.c index ae726122b..04d97a1f4 100644 --- a/src/chmempools.c +++ b/src/chmempools.c @@ -30,25 +30,30 @@ * Initializes a memory pool. * @param mp pointer to a \p MemoryPool structure * @param size the size of the objects contained in this memory pool + * @param allow_growth if \p TRUE then the memory pool can allocate + * more space from the heap when needed + * @note The parameter \p allow_growth is ignored if the \p CH_USE_HEAP + * configuration option is not enabled. */ -void chPoolInit(MemoryPool *mp, size_t size) { +void chPoolInit(MemoryPool *mp, size_t size, bool_t allow_growth) { chDbgAssert((mp != NULL) && (size >= sizeof(void *)), "chpools.c, chPoolFree()"); mp->mp_next = NULL; mp->mp_object_size = size; +#ifdef CH_USE_HEAP + mp->mp_grow = allow_growth; +#endif /* CH_USE_HEAP */ } /** * Allocates an object from a memory pool. * @param mp pointer to a \p MemoryPool structure - * @param allow_growth if \p TRUE then the object is allocated by using - * \p chHeapAlloc() in case the memory pool is empty * @return the pointer to the allocated object or \p NULL if the memory is * exhausted */ -void *chPoolAlloc(MemoryPool *mp, bool_t allow_growth) { +void *chPoolAlloc(MemoryPool *mp) { void *p; chDbgAssert(mp != NULL, "chpools.c, chPoolAlloc()"); @@ -57,7 +62,7 @@ void *chPoolAlloc(MemoryPool *mp, bool_t allow_growth) { if (mp->mp_next == NULL) { #ifdef CH_USE_HEAP - if (allow_growth) { + if (mp->mp_grow) { chSysUnlock(); return chHeapAlloc(mp->mp_object_size); @@ -73,7 +78,7 @@ void *chPoolAlloc(MemoryPool *mp, bool_t allow_growth) { } /** - * Releases (or adds) an object into a memory pool. + * Releases (or adds) an object into (to) a memory pool. * @param mp pointer to a \p MemoryPool structure * @param objp the pointer to the object to be released or added * @note the object is assumed to be of the right size for the specified @@ -99,12 +104,12 @@ void chPoolFree(MemoryPool *mp, void *objp) { * @param mp pointer to a \p MemoryPool structure * @note It is assumed that all the object are allocated using the heap * allocator, do not use this function if the pool contains other kind - * of objects. + * of objects, as example static areas. */ void chPoolRelease(MemoryPool *mp) { void *p; - while ((p = chPoolAlloc(mp, FALSE)) != NULL) + while ((p = chPoolAlloc(mp)) != NULL) chHeapFree(p); } #endif diff --git a/src/include/mempools.h b/src/include/mempools.h index 2dbf6bf37..8311a757a 100644 --- a/src/include/mempools.h +++ b/src/include/mempools.h @@ -34,13 +34,16 @@ struct pool_header { typedef struct { struct pool_header *mp_next; size_t mp_object_size; +#ifdef CH_USE_HEAP + bool_t mp_grow; +#endif /* CH_USE_HEAP */ } MemoryPool; #ifdef __cplusplus extern "C" { #endif - void chPoolInit(MemoryPool *mp, size_t size); - void *chPoolAlloc(MemoryPool *mp, bool_t grow); + void chPoolInit(MemoryPool *mp, size_t size, bool_t allow_growth); + void *chPoolAlloc(MemoryPool *mp); void chPoolFree(MemoryPool *mp, void *objp); #ifdef CH_USE_HEAP void chPoolRelease(MemoryPool *mp); -- cgit v1.2.3