git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@428 35acf78f-673a-0410-8e92-d51de3d6d3f4

This commit is contained in:
gdisirio 2008-09-08 14:06:38 +00:00
parent 0ae0293b4f
commit 250ee8cdd4
3 changed files with 21 additions and 13 deletions

View File

@ -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

View File

@ -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);

View File

@ -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 = {