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.