diff options
author | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2008-09-08 14:06:38 +0000 |
---|---|---|
committer | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2008-09-08 14:06:38 +0000 |
commit | 250ee8cdd43ffa4ba9ecab7548b18c4fc91f849d (patch) | |
tree | aa16fe0bd3fe6e5c3c54a3bfb3efd04a033bdcc4 | |
parent | 0ae0293b4fde3c450967fe083bdf7a87b38e27d3 (diff) | |
download | ChibiOS-250ee8cdd43ffa4ba9ecab7548b18c4fc91f849d.tar.gz ChibiOS-250ee8cdd43ffa4ba9ecab7548b18c4fc91f849d.tar.bz2 ChibiOS-250ee8cdd43ffa4ba9ecab7548b18c4fc91f849d.zip |
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@428 35acf78f-673a-0410-8e92-d51de3d6d3f4
-rw-r--r-- | src/chmempools.c | 21 | ||||
-rw-r--r-- | src/include/mempools.h | 7 | ||||
-rw-r--r-- | test/testpools.c | 6 |
3 files changed, 21 insertions, 13 deletions
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);
diff --git a/test/testpools.c b/test/testpools.c index a86eecedd..83614fcd5 100644 --- a/test/testpools.c +++ b/test/testpools.c @@ -32,7 +32,7 @@ static char *pools1_gettest(void) { static void pools1_setup(void) {
- chPoolInit(&mp1, UserStackSize(THREADS_STACK_SIZE));
+ chPoolInit(&mp1, UserStackSize(THREADS_STACK_SIZE), FALSE);
}
static void pools1_teardown(void) {
@@ -47,10 +47,10 @@ static void pools1_execute(void) { /* Empting the pool again. */
for (i = 0; i < 5; i++)
- test_assert(chPoolAlloc(&mp1, FALSE) != NULL, "pool list empty");
+ test_assert(chPoolAlloc(&mp1) != NULL, "pool list empty");
/* Now must be empty. */
- test_assert(chPoolAlloc(&mp1, FALSE) == NULL, "pool list not empty");
+ test_assert(chPoolAlloc(&mp1) == NULL, "pool list not empty");
}
const struct testcase testpools1 = {
|