ChibiOS  0.0.0
chmempools.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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 oslib_mempools
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  unsigned align; /**< @brief Required alignment. */
69  memgetfunc_t provider; /**< @brief Memory blocks provider
70  for this pool. */
72 
73 #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
74 /**
75  * @brief Guarded memory pool descriptor.
76  */
77 typedef struct {
78  semaphore_t sem; /**< @brief Counter semaphore guarding
79  the memory pool. */
80  memory_pool_t pool; /**< @brief The memory pool itself. */
82 #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
83 
84 /*===========================================================================*/
85 /* Module macros. */
86 /*===========================================================================*/
87 
88 /**
89  * @brief Data part of a static memory pool initializer.
90  * @details This macro should be used when statically initializing a
91  * memory pool that is part of a bigger structure.
92  *
93  * @param[in] name the name of the memory pool variable
94  * @param[in] size size of the memory pool contained objects
95  * @param[in] align required memory alignment
96  * @param[in] provider memory provider function for the memory pool
97  */
98 #define _MEMORYPOOL_DATA(name, size, align, provider) \
99  {NULL, size, align, provider}
100 
101 /**
102  * @brief Static memory pool initializer.
103  * @details Statically initialized memory pools require no explicit
104  * initialization using @p chPoolInit().
105  *
106  * @param[in] name the name of the memory pool variable
107  * @param[in] size size of the memory pool contained objects
108  * @param[in] align required memory alignment
109  * @param[in] provider memory provider function for the memory pool or @p NULL
110  * if the pool is not allowed to grow automatically
111  */
112 #define MEMORYPOOL_DECL(name, size, align, provider) \
113  memory_pool_t name = _MEMORYPOOL_DATA(name, size, align, provider)
114 
115 #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
116 /**
117  * @brief Data part of a static guarded memory pool initializer.
118  * @details This macro should be used when statically initializing a
119  * memory pool that is part of a bigger structure.
120  *
121  * @param[in] name the name of the memory pool variable
122  * @param[in] size size of the memory pool contained objects
123  * @param[in] align required memory alignment
124  */
125 #define _GUARDEDMEMORYPOOL_DATA(name, size, align) { \
126  _SEMAPHORE_DATA(name.sem, (cnt_t)0), \
127  _MEMORYPOOL_DATA(NULL, size, align, NULL) \
128 }
129 
130 /**
131  * @brief Static guarded memory pool initializer.
132  * @details Statically initialized guarded memory pools require no explicit
133  * initialization using @p chGuardedPoolInit().
134  *
135  * @param[in] name the name of the guarded memory pool variable
136  * @param[in] size size of the memory pool contained objects
137  * @param[in] align required memory alignment
138  */
139 #define GUARDEDMEMORYPOOL_DECL(name, size, align) \
140  guarded_memory_pool_t name = _GUARDEDMEMORYPOOL_DATA(name, size, align)
141 #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
142 
143 /*===========================================================================*/
144 /* External declarations. */
145 /*===========================================================================*/
146 
147 #ifdef __cplusplus
148 extern "C" {
149 #endif
150  void chPoolObjectInitAligned(memory_pool_t *mp, size_t size,
151  unsigned align, memgetfunc_t provider);
152  void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n);
153  void *chPoolAllocI(memory_pool_t *mp);
154  void *chPoolAlloc(memory_pool_t *mp);
155  void chPoolFreeI(memory_pool_t *mp, void *objp);
156  void chPoolFree(memory_pool_t *mp, void *objp);
157 #if CH_CFG_USE_SEMAPHORES == TRUE
159  size_t size,
160  unsigned align);
161  void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n);
163  sysinterval_t timeout);
165  sysinterval_t timeout);
166  void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp);
167  void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp);
168 #endif
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 /*===========================================================================*/
174 /* Module inline functions. */
175 /*===========================================================================*/
176 
177 /**
178  * @brief Initializes an empty memory pool.
179  *
180  * @param[out] mp pointer to a @p memory_pool_t structure
181  * @param[in] size the size of the objects contained in this memory pool,
182  * the minimum accepted size is the size of a pointer to
183  * void.
184  * @param[in] provider memory provider function for the memory pool or
185  * @p NULL if the pool is not allowed to grow
186  * automatically
187  *
188  * @init
189  */
190 static inline void chPoolObjectInit(memory_pool_t *mp,
191  size_t size,
192  memgetfunc_t provider) {
193 
194  chPoolObjectInitAligned(mp, size, PORT_NATURAL_ALIGN, provider);
195 }
196 
197 /**
198  * @brief Adds an object to a memory pool.
199  * @pre The memory pool must be already been initialized.
200  * @pre The added object must be of the right size for the specified
201  * memory pool.
202  * @pre The added object must be memory aligned to the size of
203  * @p stkalign_t type.
204  * @note This function is just an alias for @p chPoolFree() and has been
205  * added for clarity.
206  *
207  * @param[in] mp pointer to a @p memory_pool_t structure
208  * @param[in] objp the pointer to the object to be added
209  *
210  * @api
211  */
212 static inline void chPoolAdd(memory_pool_t *mp, void *objp) {
213 
214  chPoolFree(mp, objp);
215 }
216 
217 /**
218  * @brief Adds an object to a memory pool.
219  * @pre The memory pool must be already been initialized.
220  * @pre The added object must be of the right size for the specified
221  * memory pool.
222  * @pre The added object must be memory aligned to the size of
223  * @p stkalign_t type.
224  * @note This function is just an alias for @p chPoolFreeI() and has been
225  * added for clarity.
226  *
227  * @param[in] mp pointer to a @p memory_pool_t structure
228  * @param[in] objp the pointer to the object to be added
229  *
230  * @iclass
231  */
232 static inline void chPoolAddI(memory_pool_t *mp, void *objp) {
233 
234  chPoolFreeI(mp, objp);
235 }
236 
237 #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
238 /**
239  * @brief Initializes an empty guarded memory pool.
240  *
241  * @param[out] gmp pointer to a @p guarded_memory_pool_t structure
242  * @param[in] size the size of the objects contained in this guarded
243  * memory pool, the minimum accepted size is the size
244  * of a pointer to void.
245  *
246  * @init
247  */
249  size_t size) {
250 
252 }
253 
254 /**
255  * @brief Adds an object to a guarded memory pool.
256  * @pre The guarded memory pool must be already been initialized.
257  * @pre The added object must be of the right size for the specified
258  * guarded memory pool.
259  * @pre The added object must be properly aligned.
260  * @note This function is just an alias for @p chGuardedPoolFree() and
261  * has been added for clarity.
262  *
263  * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
264  * @param[in] objp the pointer to the object to be added
265  *
266  * @api
267  */
268 static inline void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp) {
269 
270  chGuardedPoolFree(gmp, objp);
271 }
272 
273 /**
274  * @brief Adds an object to a guarded memory pool.
275  * @pre The guarded memory pool must be already been initialized.
276  * @pre The added object must be of the right size for the specified
277  * guarded memory pool.
278  * @pre The added object must be properly aligned.
279  * @note This function is just an alias for @p chGuardedPoolFreeI() and
280  * has been added for clarity.
281  *
282  * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
283  * @param[in] objp the pointer to the object to be added
284  *
285  * @iclass
286  */
287 static inline void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp) {
288 
289  chGuardedPoolFreeI(gmp, objp);
290 }
291 
292 /**
293  * @brief Allocates an object from a guarded memory pool.
294  * @pre The guarded memory pool must be already been initialized.
295  *
296  * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
297  * @return The pointer to the allocated object.
298  * @retval NULL if the pool is empty.
299  *
300  * @iclass
301  */
302 static inline void *chGuardedPoolAllocI(guarded_memory_pool_t *gmp) {
303  void *p;
304 
305  p = chPoolAllocI(&gmp->pool);
306  if (p != NULL) {
307  chSemFastWaitI(&gmp->sem);
308  chDbgAssert(chSemGetCounterI(&gmp->sem) >= (cnt_t)0,
309  "semaphore out of sync");
310  }
311  return p;
312 }
313 #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
314 
315 #endif /* CH_CFG_USE_MEMPOOLS == TRUE */
316 
317 #endif /* CHMEMPOOLS_H */
318 
319 /** @} */
Memory pool descriptor.
Definition: chmempools.h:64
void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition: chmempools.c:338
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
void chPoolFreeI(memory_pool_t *mp, void *objp)
Releases an object into a memory pool.
Definition: chmempools.c:177
static void chPoolObjectInit(memory_pool_t *mp, size_t size, memgetfunc_t provider)
Initializes an empty memory pool.
Definition: chmempools.h:190
void * chGuardedPoolAllocTimeout(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition: chmempools.c:297
memgetfunc_t provider
Memory blocks provider for this pool.
Definition: chmempools.h:69
int32_t cnt_t
Definition: chtypes.h:55
void chPoolFree(memory_pool_t *mp, void *objp)
Releases an object into a memory pool.
Definition: chmempools.c:202
unsigned align
Required alignment.
Definition: chmempools.h:68
Memory pool free object header.
Definition: chmempools.h:56
memory_pool_t pool
The memory pool itself.
Definition: chmempools.h:80
static void * chGuardedPoolAllocI(guarded_memory_pool_t *gmp)
Allocates an object from a guarded memory pool.
Definition: chmempools.h:302
void *(* memgetfunc_t)(size_t size, unsigned align)
Memory get function.
Definition: chmemcore.h:71
static void chPoolAddI(memory_pool_t *mp, void *objp)
Adds an object to a memory pool.
Definition: chmempools.h:232
void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition: chmempools.c:320
size_t object_size
Memory pool objects size.
Definition: chmempools.h:66
static void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp)
Adds an object to a guarded memory pool.
Definition: chmempools.h:268
static void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp, size_t size)
Initializes an empty guarded memory pool.
Definition: chmempools.h:248
struct pool_header * next
Pointer to the header.
Definition: chmempools.h:65
struct pool_header * next
Pointer to the next pool header in the list.
Definition: chmempools.h:57
void * chPoolAlloc(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition: chmempools.c:155
#define chSemFastWaitI(sp)
Decreases the semaphore counter.
Semaphore structure.
Definition: chsem.h:52
#define chSemGetCounterI(sp)
Returns the semaphore counter current value.
void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n)
Loads a memory pool with an array of static objects.
Definition: chmempools.c:103
semaphore_t sem
Counter semaphore guarding the memory pool.
Definition: chmempools.h:78
void * chPoolAllocI(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition: chmempools.c:126
static void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp)
Adds an object to a guarded memory pool.
Definition: chmempools.h:287
void * chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition: chmempools.c:270
void chPoolObjectInitAligned(memory_pool_t *mp, size_t size, unsigned align, memgetfunc_t provider)
Initializes an empty memory pool.
Definition: chmempools.c:77
#define chDbgAssert(c, r)
Condition assertion.
Definition: chdebug.h:127
void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp, size_t size, unsigned align)
Initializes an empty guarded memory pool.
Definition: chmempools.c:221
static void chPoolAdd(memory_pool_t *mp, void *objp)
Adds an object to a memory pool.
Definition: chmempools.h:212
Guarded memory pool descriptor.
Definition: chmempools.h:77
void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n)
Loads a guarded memory pool with an array of static objects.
Definition: chmempools.c:242
#define PORT_NATURAL_ALIGN
Natural alignment constant.
Definition: chcore.h:50