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/chmempools.h | 68 ++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 14 deletions(-) (limited to 'os/common/oslib/include/chmempools.h') 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); } -- cgit v1.2.3