From 6d419676cde900b91f8de3cbfd8664ffa81232d7 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Wed, 4 Oct 2017 07:54:36 +0000 Subject: Alignment capability for memory pools. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10759 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/common/oslib/include/chfactory.h | 3 +- os/common/oslib/include/chfifo.h | 12 ++++--- os/common/oslib/include/chmempools.h | 68 ++++++++++++++++++++++++++++-------- os/common/oslib/src/chfactory.c | 9 +++-- os/common/oslib/src/chmempools.c | 19 +++++++--- 5 files changed, 85 insertions(+), 26 deletions(-) (limited to 'os/common/oslib') diff --git a/os/common/oslib/include/chfactory.h b/os/common/oslib/include/chfactory.h index 43b389cc4..e89d0814b 100644 --- a/os/common/oslib/include/chfactory.h +++ b/os/common/oslib/include/chfactory.h @@ -333,7 +333,8 @@ extern "C" { #if (CH_CFG_FACTORY_OBJ_FIFOS == TRUE) || defined(__DOXIGEN__) dyn_objects_fifo_t *chFactoryCreateObjectsFIFO(const char *name, size_t objsize, - size_t objn); + size_t objn, + unsigned objalign); dyn_objects_fifo_t *chFactoryFindObjectsFIFO(const char *name); void chFactoryReleaseObjectsFIFO(dyn_objects_fifo_t *dofp); #endif diff --git a/os/common/oslib/include/chfifo.h b/os/common/oslib/include/chfifo.h index 9f3c0924b..1df57ef41 100644 --- a/os/common/oslib/include/chfifo.h +++ b/os/common/oslib/include/chfifo.h @@ -110,22 +110,26 @@ extern "C" { /** * @brief Initializes a FIFO object. + * @pre The messages size must be a multiple of the alignment + * requirement. * * @param[out] ofp pointer to a @p objects_fifo_t structure * @param[in] objsize size of objects * @param[in] objn number of objects available + * @param[in] objalign required objects alignment * @param[in] objbuf pointer to the buffer of objects, it must be able - * to hold @p objn objects of @p objsize size + * to hold @p objn objects of @p objsize size with + * @p objealign alignment * @param[in] msgbuf pointer to the buffer of messages, it must be able * to hold @p objn messages * * @init */ static inline void chFifoObjectInit(objects_fifo_t *ofp, size_t objsize, - size_t objn, void *objbuf, - msg_t *msgbuf) { + size_t objn, unsigned objalign, + void *objbuf, msg_t *msgbuf) { - chGuardedPoolObjectInit(&ofp->free, objsize); + chGuardedPoolObjectInitAligned(&ofp->free, objsize, objalign); chGuardedPoolLoadArray(&ofp->free, objbuf, objn); chMBObjectInit(&ofp->mbx, msgbuf, objn); } diff --git a/os/common/oslib/include/chmempools.h b/os/common/oslib/include/chmempools.h index 25de236ce..0e9773374 100644 --- a/os/common/oslib/include/chmempools.h +++ b/os/common/oslib/include/chmempools.h @@ -69,6 +69,7 @@ typedef struct { struct pool_header *next; /**< @brief Pointer to the header. */ size_t object_size; /**< @brief Memory pool objects size. */ + unsigned align; /**< @brief Required alignment. */ memgetfunc_t provider; /**< @brief Memory blocks provider for this pool. */ } memory_pool_t; @@ -95,10 +96,11 @@ typedef struct { * * @param[in] name the name of the memory pool variable * @param[in] size size of the memory pool contained objects + * @param[in] align required memory alignment * @param[in] provider memory provider function for the memory pool */ -#define _MEMORYPOOL_DATA(name, size, provider) \ - {NULL, size, provider} +#define _MEMORYPOOL_DATA(name, size, align, provider) \ + {NULL, size, align, provider} /** * @brief Static memory pool initializer. @@ -107,11 +109,12 @@ typedef struct { * * @param[in] name the name of the memory pool variable * @param[in] size size of the memory pool contained objects + * @param[in] align required memory alignment * @param[in] provider memory provider function for the memory pool or @p NULL * if the pool is not allowed to grow automatically */ -#define MEMORYPOOL_DECL(name, size, provider) \ - memory_pool_t name = _MEMORYPOOL_DATA(name, size, provider) +#define MEMORYPOOL_DECL(name, size, align, provider) \ + memory_pool_t name = _MEMORYPOOL_DATA(name, size, align, provider) #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__) /** @@ -121,10 +124,11 @@ typedef struct { * * @param[in] name the name of the memory pool variable * @param[in] size size of the memory pool contained objects + * @param[in] align required memory alignment */ -#define _GUARDEDMEMORYPOOL_DATA(name, size) { \ +#define _GUARDEDMEMORYPOOL_DATA(name, size, align) { \ _SEMAPHORE_DATA(name.sem, (cnt_t)0), \ - _MEMORYPOOL_DATA(NULL, size, NULL) \ + _MEMORYPOOL_DATA(NULL, size, align, NULL) \ } /** @@ -134,9 +138,10 @@ typedef struct { * * @param[in] name the name of the guarded memory pool variable * @param[in] size size of the memory pool contained objects + * @param[in] align required memory alignment */ -#define GUARDEDMEMORYPOOL_DECL(name, size) \ - guarded_memory_pool_t name = _GUARDEDMEMORYPOOL_DATA(name, size) +#define GUARDEDMEMORYPOOL_DECL(name, size, align) \ + guarded_memory_pool_t name = _GUARDEDMEMORYPOOL_DATA(name, size, align) #endif /* CH_CFG_USE_SEMAPHORES == TRUE */ /*===========================================================================*/ @@ -146,14 +151,17 @@ typedef struct { #ifdef __cplusplus extern "C" { #endif - void chPoolObjectInit(memory_pool_t *mp, size_t size, memgetfunc_t provider); + void chPoolObjectInitAligned(memory_pool_t *mp, size_t size, + unsigned align, memgetfunc_t provider); void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n); void *chPoolAllocI(memory_pool_t *mp); void *chPoolAlloc(memory_pool_t *mp); void chPoolFreeI(memory_pool_t *mp, void *objp); void chPoolFree(memory_pool_t *mp, void *objp); #if CH_CFG_USE_SEMAPHORES == TRUE - void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp, size_t size); + void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp, + size_t size, + unsigned align); void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n); void *chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp, systime_t timeout); @@ -170,6 +178,26 @@ extern "C" { /* Module inline functions. */ /*===========================================================================*/ +/** + * @brief Initializes an empty memory pool. + * + * @param[out] mp pointer to a @p memory_pool_t structure + * @param[in] size the size of the objects contained in this memory pool, + * the minimum accepted size is the size of a pointer to + * void. + * @param[in] provider memory provider function for the memory pool or + * @p NULL if the pool is not allowed to grow + * automatically + * + * @init + */ +static inline void chPoolObjectInit(memory_pool_t *mp, + size_t size, + memgetfunc_t provider) { + + chPoolObjectInitAligned(mp, size, PORT_NATURAL_ALIGN, provider); +} + /** * @brief Adds an object to a memory pool. * @pre The memory pool must be already been initialized. @@ -207,12 +235,26 @@ static inline void chPoolAdd(memory_pool_t *mp, void *objp) { */ static inline void chPoolAddI(memory_pool_t *mp, void *objp) { - chDbgCheckClassI(); - chPoolFreeI(mp, objp); } #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__) +/** + * @brief Initializes an empty guarded memory pool. + * + * @param[out] gmp pointer to a @p guarded_memory_pool_t structure + * @param[in] size the size of the objects contained in this guarded + * memory pool, the minimum accepted size is the size + * of a pointer to void. + * + * @init + */ +static inline void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp, + size_t size) { + + chGuardedPoolObjectInitAligned(gmp, size, PORT_NATURAL_ALIGN); +} + /** * @brief Adds an object to a guarded memory pool. * @pre The guarded memory pool must be already been initialized. @@ -248,8 +290,6 @@ static inline void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp) { */ static inline void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp) { - chDbgCheckClassI(); - chGuardedPoolFreeI(gmp, objp); } diff --git a/os/common/oslib/src/chfactory.c b/os/common/oslib/src/chfactory.c index 5e7604917..45a6a0681 100644 --- a/os/common/oslib/src/chfactory.c +++ b/os/common/oslib/src/chfactory.c @@ -581,7 +581,9 @@ void chFactoryReleaseMailbox(dyn_mailbox_t *dmp) { * * @param[in] name name to be assigned to the new dynamic "objects FIFO" * object - * + * @param[in] objsize size of objects + * @param[in] objn number of objects available + * @param[in] objalign required objects alignment * @return The reference to the created dynamic "objects FIFO" * object. * @retval NULL if the dynamic "objects FIFO" object cannot be @@ -592,7 +594,8 @@ void chFactoryReleaseMailbox(dyn_mailbox_t *dmp) { */ dyn_objects_fifo_t *chFactoryCreateObjectsFIFO(const char *name, size_t objsize, - size_t objn) { + size_t objn, + unsigned objalign) { dyn_objects_fifo_t *dofp; chSysLock(); @@ -604,7 +607,7 @@ dyn_objects_fifo_t *chFactoryCreateObjectsFIFO(const char *name, (objn * objsize)); if (dofp != NULL) { /* Initializing mailbox object data.*/ - chFifoObjectInit(&dofp->fifo, objsize, objn, + chFifoObjectInit(&dofp->fifo, objsize, objn, objalign, dofp->msgbuf, (void *)&dofp->msgbuf[objn]); } diff --git a/os/common/oslib/src/chmempools.c b/os/common/oslib/src/chmempools.c index 384e2f7ee..aec47400c 100644 --- a/os/common/oslib/src/chmempools.c +++ b/os/common/oslib/src/chmempools.c @@ -67,18 +67,21 @@ * @param[in] size the size of the objects contained in this memory pool, * the minimum accepted size is the size of a pointer to * void. + * @param[in] align required memory alignment * @param[in] provider memory provider function for the memory pool or * @p NULL if the pool is not allowed to grow * automatically * * @init */ -void chPoolObjectInit(memory_pool_t *mp, size_t size, memgetfunc_t provider) { +void chPoolObjectInitAligned(memory_pool_t *mp, size_t size, + unsigned align, memgetfunc_t provider) { chDbgCheck((mp != NULL) && (size >= sizeof(void *))); mp->next = NULL; mp->object_size = size; + mp->align = align; mp->provider = provider; } @@ -87,6 +90,8 @@ void chPoolObjectInit(memory_pool_t *mp, size_t size, memgetfunc_t provider) { * @pre The memory pool must be already been initialized. * @pre The array elements must be of the right size for the specified * memory pool. + * @pre The array elements size must be a multiple of the alignment + * requirement for the pool. * @post The memory pool contains the elements of the input array. * * @param[in] mp pointer to a @p memory_pool_t structure @@ -130,7 +135,7 @@ void *chPoolAllocI(memory_pool_t *mp) { mp->next = mp->next->next; } else if (mp->provider != NULL) { - objp = mp->provider(mp->object_size, PORT_NATURAL_ALIGN); /* TODO: Alignment is not properly handled */ + objp = mp->provider(mp->object_size, mp->align); } /*lint -restore*/ @@ -175,6 +180,9 @@ void chPoolFreeI(memory_pool_t *mp, void *objp) { chDbgCheckClassI(); chDbgCheck((mp != NULL) && (objp != NULL)); + chDbgAssert(((size_t)objp & MEM_ALIGN_MASK(mp->align)) == 0U, + "unaligned object"); + php->next = mp->next; mp->next = php; } @@ -206,12 +214,15 @@ void chPoolFree(memory_pool_t *mp, void *objp) { * @param[in] size the size of the objects contained in this guarded * memory pool, the minimum accepted size is the size * of a pointer to void. + * @param[in] align required memory alignment * * @init */ -void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp, size_t size) { +void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp, + size_t size, + unsigned align) { - chPoolObjectInit(&gmp->pool, size, NULL); + chPoolObjectInitAligned(&gmp->pool, size, align, NULL); chSemObjectInit(&gmp->sem, (cnt_t)0); } -- cgit v1.2.3