ChibiOS/RT
2.5.1
chmempools.h
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 /**
00022  * @file    chmempools.h
00023  * @brief   Memory Pools macros and structures.
00024  *
00025  * @addtogroup pools
00026  * @{
00027  */
00028 
00029 #ifndef _CHMEMPOOLS_H_
00030 #define _CHMEMPOOLS_H_
00031 
00032 #if CH_USE_MEMPOOLS || defined(__DOXYGEN__)
00033 
00034 /**
00035  * @brief   Memory pool free object header.
00036  */
00037 struct pool_header {
00038   struct pool_header    *ph_next;       /**< @brief Pointer to the next pool
00039                                                     header in the list.     */
00040 };
00041 
00042 /**
00043  * @brief   Memory pool descriptor.
00044  */
00045 typedef struct {
00046   struct pool_header    *mp_next;       /**< @brief Pointer to the header.  */
00047   size_t                mp_object_size; /**< @brief Memory pool objects
00048                                                     size.                   */
00049   memgetfunc_t          mp_provider;    /**< @brief Memory blocks provider for
00050                                                     this pool.              */
00051 } MemoryPool;
00052 
00053 /**
00054  * @brief   Data part of a static memory pool initializer.
00055  * @details This macro should be used when statically initializing a
00056  *          memory pool that is part of a bigger structure.
00057  *
00058  * @param[in] name      the name of the memory pool variable
00059  * @param[in] size      size of the memory pool contained objects
00060  * @param[in] provider  memory provider function for the memory pool
00061  */
00062 #define _MEMORYPOOL_DATA(name, size, provider)                              \
00063   {NULL, size, provider}
00064 
00065 /**
00066  * @brief Static memory pool initializer in hungry mode.
00067  * @details Statically initialized memory pools require no explicit
00068  *          initialization using @p chPoolInit().
00069  *
00070  * @param[in] name the name of the memory pool variable
00071  * @param[in] size size of the memory pool contained objects
00072  * @param[in] provider memory provider function for the memory pool or @p NULL
00073  *                     if the pool is not allowed to grow automatically
00074  */
00075 #define MEMORYPOOL_DECL(name, size, provider)                               \
00076   MemoryPool name = _MEMORYPOOL_DATA(name, size, provider)
00077 
00078 /**
00079  * @name    Macro Functions
00080  * @{
00081  */
00082 /**
00083  * @brief   Adds an object to a memory pool.
00084  * @pre     The memory pool must be already been initialized.
00085  * @pre     The added object must be of the right size for the specified
00086  *          memory pool.
00087  * @pre     The added object must be memory aligned to the size of
00088  *          @p stkalign_t type.
00089  * @note    This function is just an alias for @p chPoolFree() and has been
00090  *          added for clarity.
00091  *
00092  * @param[in] mp        pointer to a @p MemoryPool structure
00093  * @param[in] objp      the pointer to the object to be added
00094  *
00095  * @api
00096  */
00097 #define chPoolAdd(mp, objp) chPoolFree(mp, objp)
00098 
00099 /**
00100  * @brief   Adds an object to a memory pool.
00101  * @pre     The memory pool must be already been initialized.
00102  * @pre     The added object must be of the right size for the specified
00103  *          memory pool.
00104  * @pre     The added object must be memory aligned to the size of
00105  *          @p stkalign_t type.
00106  * @note    This function is just an alias for @p chPoolFree() and has been
00107  *          added for clarity.
00108  *
00109  * @param[in] mp        pointer to a @p MemoryPool structure
00110  * @param[in] objp      the pointer to the object to be added
00111  *
00112  * @iclass
00113  */
00114 #define chPoolAddI(mp, objp) chPoolFreeI(mp, objp)
00115 /** @} */
00116 
00117 #ifdef __cplusplus
00118 extern "C" {
00119 #endif
00120   void chPoolInit(MemoryPool *mp, size_t size, memgetfunc_t provider);
00121   void chPoolLoadArray(MemoryPool *mp, void *p, size_t n);
00122   void *chPoolAllocI(MemoryPool *mp);
00123   void *chPoolAlloc(MemoryPool *mp);
00124   void chPoolFreeI(MemoryPool *mp, void *objp);
00125   void chPoolFree(MemoryPool *mp, void *objp);
00126 #ifdef __cplusplus
00127 }
00128 #endif
00129 
00130 #endif /* CH_USE_MEMPOOLS */
00131 
00132 #endif /* _CHMEMPOOLS_H_ */
00133 
00134 /** @} */