Page 1 of 1
Memory Pool
Posted: Tue Mar 19, 2013 11:17 am
by carnecro
I have following issue
Structure:
Code: Select all
typedef struct {
uint8_t counter ;
uint8_t buff[MAX_LENGTH] ;
} MSG_BUFF ;
Memory Pool:
Code: Select all
MemoryPool msg_pool;
static MSG_BUFF msg_pool_buffer[MAX_MESSAGES] __attribute__((aligned(sizeof(stkalign_t)))) ;
Initialisation:
Code: Select all
chPoolInit(&msg_pool, sizeof(MSG_BUFF), NULL );
for (i = 0; i < (MAX_MESSAGES); i++) {
chPoolFree(&msg_pool, &msg_pool_buffer[i]);
}
Looks like chPoolFree() overwrites parts of the structure memory. I initialised the structure with data and after return from chPoolFree() some of structure bytes has been modified. Is it as expected or do I anything wrong (version 2.5.1)
Re: Memory Pool
Posted: Tue Mar 19, 2013 12:07 pm
by Giovanni
Hi,
The memory pools manager uses the first sizeof(void *) bytes of the object to store a pointer to the next object when the object is in the pool. You can't have pools of pre-initialized objects unless you put a reserved pointer as first field of the structure.
Giovanni
Re: Memory Pool
Posted: Tue Mar 19, 2013 6:40 pm
by carnecro
From the documentation it's not obvious, thank you for the hint. In my case the solution was
Code: Select all
typedef struct {
struct pool_header *phdr;
uint8_t counter ;
uint8_t buff[MAX_LENGTH] ;
} MSG_BUFF ;
in this post
http://forum.chibios.org/phpbb/viewtopic.php?f=2&t=722&p=7288&hilit=memory+pool#p7288 is another example of the right structure.
Re: Memory Pool
Posted: Sat Mar 23, 2013 9:09 pm
by skute
I use a wrapper around all of the ChibiOS functions. In the case of MemoryPools, I also have some helper macros/functions to ensure the correct alignment and pool size.
See below (the usage example explains how I use it), note that some of the defines are internal to specific files, others are exported in headers which is why some of the macro's look to have duplicated code:
Code: Select all
// alignment must be a power of 2
#define ALIGN_UP(val, alignment) (((val) + ((alignment) - 1)) & ~((alignment) - 1)
#define MAX(a, b) (((a) > (b)) ? (a) : (b)
#define OS_POOL_DATA_SIZE(elementSize, numElements) \
(ALIGN_UP(MAX(elementSize, sizeof(struct pool_header)), sizeof(uintptr_t)) * numElements)
#define POOL_ENTRY_SIZE(entrySize) \
(ALIGN_UP(MAX(entrySize, sizeof(struct pool_header)), sizeof(uintptr_t)))
#define MAX_POOL_ENTRIES(poolSize, entrySize) \
(poolSize / POOL_ENTRY_SIZE(entrySize))
#define OS_CHECK_ARG(arg) { \
assert(NULL != arg); \
if (NULL == arg) { \
return OS_ERROR_BAD_ARG; \
} \
}
typedef MemoryPool OS_MemPool_t;
typedef uint8_t OS_MemPoolData_t;
/*
* usage:
* struct poolElement
* {
* uint8_t code;
* uint16_t somethingElse;
* };
*
* #define NUM_ELEMENTS (16u)
*
*
* uint32_t numElements = NUM_ELEMENTS;
* OS_MemPool_t pool;
* OS_MemPoolData_t poolData[OS_POOL_DATA_SIZE(sizeof(struct poolElement), NUM_ELEMENTS)];
*
* OS_MemPoolInit(&pool, poolData, sizeof(poolData), sizeof(struct poolElement), &numElements);
*/
OS_Result_t
OS_MemPoolInit(OS_MemPool_t *pMemPool,
OS_MemPoolData_t *pMemPoolData,
uint32_t poolDataSize,
uint32_t poolObjectSize,
uint32_t *numPoolObjects)
{
size_t numEntries;
OS_CHECK_ARG(pMemPool);
OS_CHECK_ARG(pMemPoolData);
OS_CHECK_ARG(numPoolObjects);
assert(*numPoolObjects == MAX_POOL_ENTRIES(poolDataSize, poolObjectSize));
numEntries = MAX_POOL_ENTRIES(poolDataSize, poolObjectSize);
chPoolInit(pMemPool, POOL_ENTRY_SIZE(poolObjectSize), NULL);
chPoolLoadArray(pMemPool, pMemPoolData, numEntries);
*numPoolObjects = (uint32_t)numEntries;
return OS_SUCCESS;
}
With this, you don't need to add the struct pool_header *phdr to your MSG_BUFF.