        -:    0:Source:../../../os/common/oslib/include/chmempools.h
        -:    0:Programs:16
        -:    1:/*
        -:    2:    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio.
        -:    3:
        -:    4:    This file is part of ChibiOS.
        -:    5:
        -:    6:    ChibiOS is free software; you can redistribute it and/or modify
        -:    7:    it under the terms of the GNU General Public License as published by
        -:    8:    the Free Software Foundation; either version 3 of the License, or
        -:    9:    (at your option) any later version.
        -:   10:
        -:   11:    ChibiOS is distributed in the hope that it will be useful,
        -:   12:    but WITHOUT ANY WARRANTY; without even the implied warranty of
        -:   13:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        -:   14:    GNU General Public License for more details.
        -:   15:
        -:   16:    You should have received a copy of the GNU General Public License
        -:   17:    along with this program.  If not, see <http://www.gnu.org/licenses/>.
        -:   18:*/
        -:   19:
        -:   20:/**
        -:   21: * @file    chmempools.h
        -:   22: * @brief   Memory Pools macros and structures.
        -:   23: *
        -:   24: * @addtogroup pools
        -:   25: * @{
        -:   26: */
        -:   27:
        -:   28:#ifndef CHMEMPOOLS_H
        -:   29:#define CHMEMPOOLS_H
        -:   30:
        -:   31:#if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
        -:   32:
        -:   33:/*===========================================================================*/
        -:   34:/* Module constants.                                                         */
        -:   35:/*===========================================================================*/
        -:   36:
        -:   37:/*===========================================================================*/
        -:   38:/* Module pre-compile time settings.                                         */
        -:   39:/*===========================================================================*/
        -:   40:
        -:   41:/*===========================================================================*/
        -:   42:/* Derived constants and error checks.                                       */
        -:   43:/*===========================================================================*/
        -:   44:
        -:   45:#if CH_CFG_USE_MEMCORE == FALSE
        -:   46:#error "CH_CFG_USE_MEMPOOLS requires CH_CFG_USE_MEMCORE"
        -:   47:#endif
        -:   48:
        -:   49:/*===========================================================================*/
        -:   50:/* Module data structures and types.                                         */
        -:   51:/*===========================================================================*/
        -:   52:
        -:   53:/**
        -:   54: * @brief   Memory pool free object header.
        -:   55: */
        -:   56:struct pool_header {
        -:   57:  struct pool_header    *next;          /**< @brief Pointer to the next pool
        -:   58:                                                    header in the list.     */
        -:   59:};
        -:   60:
        -:   61:/**
        -:   62: * @brief   Memory pool descriptor.
        -:   63: */
        -:   64:typedef struct {
        -:   65:  struct pool_header    *next;          /**< @brief Pointer to the header.  */
        -:   66:  size_t                object_size;    /**< @brief Memory pool objects
        -:   67:                                                    size.                   */
        -:   68:  memgetfunc_t          provider;       /**< @brief Memory blocks provider
        -:   69:                                                    for this pool.          */
        -:   70:} memory_pool_t;
        -:   71:
        -:   72:#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
        -:   73:/**
        -:   74: * @brief   Guarded memory pool descriptor.
        -:   75: */
        -:   76:typedef struct {
        -:   77:  semaphore_t           sem;            /**< @brief Counter semaphore quarding
        -:   78:                                                    the memory pool.        */
        -:   79:  memory_pool_t         pool;           /**< @brief The memory pool itself. */
        -:   80:} guarded_memory_pool_t;
        -:   81:#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
        -:   82:
        -:   83:/*===========================================================================*/
        -:   84:/* Module macros.                                                            */
        -:   85:/*===========================================================================*/
        -:   86:
        -:   87:/**
        -:   88: * @brief   Data part of a static memory pool initializer.
        -:   89: * @details This macro should be used when statically initializing a
        -:   90: *          memory pool that is part of a bigger structure.
        -:   91: *
        -:   92: * @param[in] name      the name of the memory pool variable
        -:   93: * @param[in] size      size of the memory pool contained objects
        -:   94: * @param[in] provider  memory provider function for the memory pool
        -:   95: */
        -:   96:#define _MEMORYPOOL_DATA(name, size, provider)                              \
        -:   97:  {NULL, size, provider}
        -:   98:
        -:   99:/**
        -:  100: * @brief   Static memory pool initializer.
        -:  101: * @details Statically initialized memory pools require no explicit
        -:  102: *          initialization using @p chPoolInit().
        -:  103: *
        -:  104: * @param[in] name      the name of the memory pool variable
        -:  105: * @param[in] size      size of the memory pool contained objects
        -:  106: * @param[in] provider  memory provider function for the memory pool or @p NULL
        -:  107: *                      if the pool is not allowed to grow automatically
        -:  108: */
        -:  109:#define MEMORYPOOL_DECL(name, size, provider)                               \
        -:  110:  memory_pool_t name = _MEMORYPOOL_DATA(name, size, provider)
        -:  111:
        -:  112:#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
        -:  113:/**
        -:  114: * @brief   Data part of a static guarded memory pool initializer.
        -:  115: * @details This macro should be used when statically initializing a
        -:  116: *          memory pool that is part of a bigger structure.
        -:  117: *
        -:  118: * @param[in] name      the name of the memory pool variable
        -:  119: * @param[in] size      size of the memory pool contained objects
        -:  120: */
        -:  121:#define _GUARDEDMEMORYPOOL_DATA(name, size) {                               \
        -:  122:  _SEMAPHORE_DATA(name.sem, (cnt_t)0),                                      \
        -:  123:  _MEMORYPOOL_DATA(NULL, size, NULL)                                        \
        -:  124:}
        -:  125:
        -:  126:/**
        -:  127: * @brief   Static guarded memory pool initializer.
        -:  128: * @details Statically initialized guarded memory pools require no explicit
        -:  129: *          initialization using @p chGuardedPoolInit().
        -:  130: *
        -:  131: * @param[in] name      the name of the guarded memory pool variable
        -:  132: * @param[in] size      size of the memory pool contained objects
        -:  133: */
        -:  134:#define GUARDEDMEMORYPOOL_DECL(name, size)                                  \
        -:  135:  guarded_memory_pool_t name = _GUARDEDMEMORYPOOL_DATA(name, size)
        -:  136:#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
        -:  137:
        -:  138:/*===========================================================================*/
        -:  139:/* External declarations.                                                    */
        -:  140:/*===========================================================================*/
        -:  141:
        -:  142:#ifdef __cplusplus
        -:  143:extern "C" {
        -:  144:#endif
        -:  145:  void chPoolObjectInit(memory_pool_t *mp, size_t size, memgetfunc_t provider);
        -:  146:  void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n);
        -:  147:  void *chPoolAllocI(memory_pool_t *mp);
        -:  148:  void *chPoolAlloc(memory_pool_t *mp);
        -:  149:  void chPoolFreeI(memory_pool_t *mp, void *objp);
        -:  150:  void chPoolFree(memory_pool_t *mp, void *objp);
        -:  151:#if CH_CFG_USE_SEMAPHORES == TRUE
        -:  152:  void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp, size_t size);
        -:  153:  void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n);
        -:  154:  void *chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp,
        -:  155:                                   systime_t timeout);
        -:  156:  void *chGuardedPoolAllocTimeout(guarded_memory_pool_t *gmp,
        -:  157:                                  systime_t timeout);
        -:  158:  void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp);
        -:  159:  void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp);
        -:  160:#endif
        -:  161:#ifdef __cplusplus
        -:  162:}
        -:  163:#endif
        -:  164:
        -:  165:/*===========================================================================*/
        -:  166:/* Module inline functions.                                                  */
        -:  167:/*===========================================================================*/
        -:  168:
        -:  169:/**
        -:  170: * @brief   Adds an object to a memory pool.
        -:  171: * @pre     The memory pool must be already been initialized.
        -:  172: * @pre     The added object must be of the right size for the specified
        -:  173: *          memory pool.
        -:  174: * @pre     The added object must be memory aligned to the size of
        -:  175: *          @p stkalign_t type.
        -:  176: * @note    This function is just an alias for @p chPoolFree() and has been
        -:  177: *          added for clarity.
        -:  178: *
        -:  179: * @param[in] mp        pointer to a @p memory_pool_t structure
        -:  180: * @param[in] objp      the pointer to the object to be added
        -:  181: *
        -:  182: * @api
        -:  183: */
function chPoolAdd called 4 returned 100% blocks executed 100%
        4:  184:static inline void chPoolAdd(memory_pool_t *mp, void *objp) {
        -:  185:
        4:  186:  chPoolFree(mp, objp);
call    0 returned 100%
        4:  187:}
        -:  188:
        -:  189:/**
        -:  190: * @brief   Adds an object to a memory pool.
        -:  191: * @pre     The memory pool must be already been initialized.
        -:  192: * @pre     The added object must be of the right size for the specified
        -:  193: *          memory pool.
        -:  194: * @pre     The added object must be memory aligned to the size of
        -:  195: *          @p stkalign_t type.
        -:  196: * @note    This function is just an alias for @p chPoolFreeI() and has been
        -:  197: *          added for clarity.
        -:  198: *
        -:  199: * @param[in] mp        pointer to a @p memory_pool_t structure
        -:  200: * @param[in] objp      the pointer to the object to be added
        -:  201: *
        -:  202: * @iclass
        -:  203: */
        -:  204:static inline void chPoolAddI(memory_pool_t *mp, void *objp) {
        -:  205:
        -:  206:  chDbgCheckClassI();
        -:  207:
        -:  208:  chPoolFreeI(mp, objp);
        -:  209:}
        -:  210:
        -:  211:#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
        -:  212:/**
        -:  213: * @brief   Adds an object to a guarded memory pool.
        -:  214: * @pre     The guarded memory pool must be already been initialized.
        -:  215: * @pre     The added object must be of the right size for the specified
        -:  216: *          guarded memory pool.
        -:  217: * @pre     The added object must be properly aligned.
        -:  218: * @note    This function is just an alias for @p chGuardedPoolFree() and
        -:  219: *          has been added for clarity.
        -:  220: *
        -:  221: * @param[in] gmp       pointer to a @p guarded_memory_pool_t structure
        -:  222: * @param[in] objp      the pointer to the object to be added
        -:  223: *
        -:  224: * @api
        -:  225: */
function chGuardedPoolAdd called 4 returned 100% blocks executed 100%
        4:  226:static inline void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp) {
        -:  227:
        4:  228:  chGuardedPoolFree(gmp, objp);
call    0 returned 100%
        4:  229:}
        -:  230:
        -:  231:/**
        -:  232: * @brief   Adds an object to a guarded memory pool.
        -:  233: * @pre     The guarded memory pool must be already been initialized.
        -:  234: * @pre     The added object must be of the right size for the specified
        -:  235: *          guarded memory pool.
        -:  236: * @pre     The added object must be properly aligned.
        -:  237: * @note    This function is just an alias for @p chGuardedPoolFreeI() and
        -:  238: *          has been added for clarity.
        -:  239: *
        -:  240: * @param[in] gmp       pointer to a @p guarded_memory_pool_t structure
        -:  241: * @param[in] objp      the pointer to the object to be added
        -:  242: *
        -:  243: * @iclass
        -:  244: */
        -:  245:static inline void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp) {
        -:  246:
        -:  247:  chDbgCheckClassI();
        -:  248:
        -:  249:  chGuardedPoolFreeI(gmp, objp);
        -:  250:}
        -:  251:#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
        -:  252:
        -:  253:#endif /* CH_CFG_USE_MEMPOOLS == TRUE */
        -:  254:
        -:  255:#endif /* CHMEMPOOLS_H */
        -:  256:
        -:  257:/** @} */
