ChibiOS 21.11.5
chmempools.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 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 version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file oslib/include/chmempools.h
21 * @brief Memory Pools macros and structures.
22 *
23 * @addtogroup oslib_mempools
24 * @{
25 */
26
27#ifndef CHMEMPOOLS_H
28#define CHMEMPOOLS_H
29
30#if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Module pre-compile time settings. */
38/*===========================================================================*/
39
40/*===========================================================================*/
41/* Derived constants and error checks. */
42/*===========================================================================*/
43
44#if CH_CFG_USE_MEMCORE == FALSE
45#error "CH_CFG_USE_MEMPOOLS requires CH_CFG_USE_MEMCORE"
46#endif
47
48/*===========================================================================*/
49/* Module data structures and types. */
50/*===========================================================================*/
51
52/**
53 * @brief Memory pool free object header.
54 */
56 struct pool_header *next; /**< @brief Pointer to the next pool
57 header in the list. */
58};
59
60/**
61 * @brief Memory pool descriptor.
62 */
63typedef struct {
64 struct pool_header *next; /**< @brief Pointer to the header. */
65 size_t object_size; /**< @brief Memory pool objects
66 size. */
67 unsigned align; /**< @brief Required alignment. */
68 memgetfunc_t provider; /**< @brief Memory blocks provider
69 for this pool. */
71
72#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
73/**
74 * @brief Guarded memory pool descriptor.
75 */
76typedef struct {
77 semaphore_t sem; /**< @brief Counter semaphore guarding
78 the memory pool. */
79 memory_pool_t pool; /**< @brief The memory pool itself. */
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] align required memory alignment
95 * @param[in] provider memory provider function for the memory pool
96 */
97#define __MEMORYPOOL_DATA(name, size, align, provider) \
98 {NULL, size, align, provider}
99
100/**
101 * @brief Static memory pool initializer.
102 * @details Statically initialized memory pools require no explicit
103 * initialization using @p chPoolInit().
104 *
105 * @param[in] name the name of the memory pool variable
106 * @param[in] size size of the memory pool contained objects
107 * @param[in] align required memory alignment
108 * @param[in] provider memory provider function for the memory pool or @p NULL
109 * if the pool is not allowed to grow automatically
110 */
111#define MEMORYPOOL_DECL(name, size, align, provider) \
112 memory_pool_t name = __MEMORYPOOL_DATA(name, size, align, provider)
113
114#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
115/**
116 * @brief Data part of a static guarded memory pool initializer.
117 * @details This macro should be used when statically initializing a
118 * memory pool that is part of a bigger structure.
119 *
120 * @param[in] name the name of the memory pool variable
121 * @param[in] size size of the memory pool contained objects
122 * @param[in] align required memory alignment
123 */
124#define __GUARDEDMEMORYPOOL_DATA(name, size, align) { \
125 __SEMAPHORE_DATA(name.sem, (cnt_t)0), \
126 __MEMORYPOOL_DATA(NULL, size, align, NULL) \
127}
128
129/**
130 * @brief Static guarded memory pool initializer.
131 * @details Statically initialized guarded memory pools require no explicit
132 * initialization using @p chGuardedPoolInit().
133 *
134 * @param[in] name the name of the guarded memory pool variable
135 * @param[in] size size of the memory pool contained objects
136 * @param[in] align required memory alignment
137 */
138#define GUARDEDMEMORYPOOL_DECL(name, size, align) \
139 guarded_memory_pool_t name = __GUARDEDMEMORYPOOL_DATA(name, size, align)
140#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
141
142/*===========================================================================*/
143/* External declarations. */
144/*===========================================================================*/
145
146#ifdef __cplusplus
147extern "C" {
148#endif
149 void chPoolObjectInitAligned(memory_pool_t *mp, size_t size,
150 unsigned align, memgetfunc_t provider);
151 void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n);
152 void *chPoolAllocI(memory_pool_t *mp);
153 void *chPoolAlloc(memory_pool_t *mp);
154 void chPoolFreeI(memory_pool_t *mp, void *objp);
155 void chPoolFree(memory_pool_t *mp, void *objp);
156#if CH_CFG_USE_SEMAPHORES == TRUE
158 size_t size,
159 unsigned align);
160 void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n);
162 sysinterval_t timeout);
164 sysinterval_t timeout);
165 void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp);
166#endif
167#ifdef __cplusplus
168}
169#endif
170
171/*===========================================================================*/
172/* Module inline functions. */
173/*===========================================================================*/
174
175/**
176 * @brief Initializes an empty memory pool.
177 *
178 * @param[out] mp pointer to a @p memory_pool_t structure
179 * @param[in] size the size of the objects contained in this memory pool,
180 * the minimum accepted size is the size of a pointer to
181 * void.
182 * @param[in] provider memory provider function for the memory pool or
183 * @p NULL if the pool is not allowed to grow
184 * automatically
185 *
186 * @init
187 */
188static inline void chPoolObjectInit(memory_pool_t *mp,
189 size_t size,
190 memgetfunc_t provider) {
191
192 chPoolObjectInitAligned(mp, size, PORT_NATURAL_ALIGN, provider);
193}
194
195/**
196 * @brief Adds an object to a memory pool.
197 * @pre The memory pool must be already been initialized.
198 * @pre The added object must be of the right size for the specified
199 * memory pool.
200 * @pre The added object must be properly aligned.
201 * @note This function is just an alias for @p chPoolFree() and has been
202 * added for clarity.
203 *
204 * @param[in] mp pointer to a @p memory_pool_t structure
205 * @param[in] objp the pointer to the object to be added
206 *
207 * @api
208 */
209static inline void chPoolAdd(memory_pool_t *mp, void *objp) {
210
211 chPoolFree(mp, objp);
212}
213
214/**
215 * @brief Adds an object to a memory pool.
216 * @pre The memory pool must be already been initialized.
217 * @pre The added object must be of the right size for the specified
218 * memory pool.
219 * @pre The added object must be properly aligned.
220 * @note This function is just an alias for @p chPoolFreeI() and has been
221 * added for clarity.
222 *
223 * @param[in] mp pointer to a @p memory_pool_t structure
224 * @param[in] objp the pointer to the object to be added
225 *
226 * @iclass
227 */
228static inline void chPoolAddI(memory_pool_t *mp, void *objp) {
229
230 chPoolFreeI(mp, objp);
231}
232
233#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
234/**
235 * @brief Initializes an empty guarded memory pool.
236 *
237 * @param[out] gmp pointer to a @p guarded_memory_pool_t structure
238 * @param[in] size the size of the objects contained in this guarded
239 * memory pool, the minimum accepted size is the size
240 * of a pointer to void.
241 *
242 * @init
243 */
245 size_t size) {
246
248}
249
250/**
251 * @brief Gets the count of objects in a guarded memory pool.
252 * @pre The guarded memory pool must be already been initialized.
253 *
254 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
255 * @return The counter of the guard semaphore.
256 *
257 * @iclass
258 */
260
261 return chSemGetCounterI(&gmp->sem);
262}
263
264/**
265 * @brief Allocates an object from a guarded memory pool.
266 * @pre The guarded memory pool must be already been initialized.
267 *
268 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
269 * @return The pointer to the allocated object.
270 * @retval NULL if the pool is empty.
271 *
272 * @iclass
273 */
275 void *p;
276
277 if (chSemGetCounterI(&gmp->sem) > (cnt_t)0) {
278
279 chSemFastWaitI(&gmp->sem);
280 p = chPoolAllocI(&gmp->pool);
281 }
282 else {
283 p = NULL;
284 }
285
286 return p;
287}
288
289/**
290 * @brief Releases an object into a guarded memory pool.
291 * @pre The guarded memory pool must already be initialized.
292 * @pre The freed object must be of the right size for the specified
293 * guarded memory pool.
294 * @pre The added object must be properly aligned.
295 *
296 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
297 * @param[in] objp the pointer to the object to be released
298 *
299 * @iclass
300 */
301static inline void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp) {
302
303 chPoolFreeI(&gmp->pool, objp);
304 chSemSignalI(&gmp->sem);
305}
306
307/**
308 * @brief Releases an object into a guarded memory pool.
309 * @pre The guarded memory pool must already be initialized.
310 * @pre The freed object must be of the right size for the specified
311 * guarded memory pool.
312 * @pre The added object must be properly aligned.
313 *
314 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
315 * @param[in] objp the pointer to the object to be released
316 *
317 * @sclass
318 */
319static inline void chGuardedPoolFreeS(guarded_memory_pool_t *gmp, void *objp) {
320
321 chGuardedPoolFreeI(gmp, objp);
323}
324
325/**
326 * @brief Adds an object to a guarded memory pool.
327 * @pre The guarded memory pool must be already been initialized.
328 * @pre The added object must be of the right size for the specified
329 * guarded memory pool.
330 * @pre The added object must be properly aligned.
331 * @note This function is just an alias for @p chGuardedPoolFree() and
332 * has been added for clarity.
333 *
334 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
335 * @param[in] objp the pointer to the object to be added
336 *
337 * @api
338 */
339static inline void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp) {
340
341 chGuardedPoolFree(gmp, objp);
342}
343
344/**
345 * @brief Adds an object to a guarded memory pool.
346 * @pre The guarded memory pool must be already been initialized.
347 * @pre The added object must be of the right size for the specified
348 * guarded memory pool.
349 * @pre The added object must be properly aligned.
350 * @note This function is just an alias for @p chGuardedPoolFreeI() and
351 * has been added for clarity.
352 *
353 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
354 * @param[in] objp the pointer to the object to be added
355 *
356 * @iclass
357 */
358static inline void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp) {
359
360 chGuardedPoolFreeI(gmp, objp);
361}
362
363/**
364 * @brief Adds an object to a guarded memory pool.
365 * @pre The guarded memory pool must be already been initialized.
366 * @pre The added object must be of the right size for the specified
367 * guarded memory pool.
368 * @pre The added object must be properly aligned.
369 * @note This function is just an alias for @p chGuardedPoolFreeI() and
370 * has been added for clarity.
371 *
372 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
373 * @param[in] objp the pointer to the object to be added
374 *
375 * @sclass
376 */
377static inline void chGuardedPoolAddS(guarded_memory_pool_t *gmp, void *objp) {
378
379 chGuardedPoolFreeS(gmp, objp);
380}
381#endif /* CH_CFG_USE_SEMAPHORES == TRUE */
382
383#endif /* CH_CFG_USE_MEMPOOLS == TRUE */
384
385#endif /* CHMEMPOOLS_H */
386
387/** @} */
#define chSemFastWaitI(sp)
Decreases the semaphore counter.
#define chSemGetCounterI(sp)
Returns the semaphore counter current value.
int32_t cnt_t
Definition chearly.h:91
void *(* memgetfunc_t)(size_t size, unsigned align)
Memory get function.
Definition chmemcore.h:70
static void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp)
Adds an object to a guarded memory pool.
Definition chmempools.h:358
void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp, size_t size, unsigned align)
Initializes an empty guarded memory pool.
Definition chmempools.c:225
void * chPoolAllocI(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition chmempools.c:128
static void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.h:301
void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n)
Loads a memory pool with an array of static objects.
Definition chmempools.c:105
static void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp)
Adds an object to a guarded memory pool.
Definition chmempools.h:339
static void chPoolAdd(memory_pool_t *mp, void *objp)
Adds an object to a memory pool.
Definition chmempools.h:209
static void chGuardedPoolFreeS(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.h:319
void * chPoolAlloc(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition chmempools.c:160
void * chGuardedPoolAllocTimeout(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition chmempools.c:301
void * chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp, sysinterval_t timeout)
Allocates an object from a guarded memory pool.
Definition chmempools.c:274
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:246
static void chPoolObjectInit(memory_pool_t *mp, size_t size, memgetfunc_t provider)
Initializes an empty memory pool.
Definition chmempools.h:188
void chPoolObjectInitAligned(memory_pool_t *mp, size_t size, unsigned align, memgetfunc_t provider)
Initializes an empty memory pool.
Definition chmempools.c:76
void chPoolFreeI(memory_pool_t *mp, void *objp)
Releases an object into a memory pool.
Definition chmempools.c:182
static cnt_t chGuardedPoolGetCounterI(guarded_memory_pool_t *gmp)
Gets the count of objects in a guarded memory pool.
Definition chmempools.h:259
static void chGuardedPoolAddS(guarded_memory_pool_t *gmp, void *objp)
Adds an object to a guarded memory pool.
Definition chmempools.h:377
static void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp, size_t size)
Initializes an empty guarded memory pool.
Definition chmempools.h:244
void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.c:324
static void * chGuardedPoolAllocI(guarded_memory_pool_t *gmp)
Allocates an object from a guarded memory pool.
Definition chmempools.h:274
static void chPoolAddI(memory_pool_t *mp, void *objp)
Adds an object to a memory pool.
Definition chmempools.h:228
void chPoolFree(memory_pool_t *mp, void *objp)
Releases an object into a memory pool.
Definition chmempools.c:206
#define PORT_NATURAL_ALIGN
Natural alignment constant.
Definition chcore.h:49
void chSchRescheduleS(void)
Performs a reschedule if a higher priority thread is runnable.
Definition chschd.c:457
struct ch_semaphore semaphore_t
Semaphore structure.
void chSemSignalI(semaphore_t *sp)
Performs a signal operation on a semaphore.
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
Guarded memory pool descriptor.
Definition chmempools.h:76
semaphore_t sem
Counter semaphore guarding the memory pool.
Definition chmempools.h:77
memory_pool_t pool
The memory pool itself.
Definition chmempools.h:79
Memory pool descriptor.
Definition chmempools.h:63
unsigned align
Required alignment.
Definition chmempools.h:67
struct pool_header * next
Pointer to the header.
Definition chmempools.h:64
memgetfunc_t provider
Memory blocks provider for this pool.
Definition chmempools.h:68
size_t object_size
Memory pool objects size.
Definition chmempools.h:65
Memory pool free object header.
Definition chmempools.h:55
struct pool_header * next
Pointer to the next pool header in the list.
Definition chmempools.h:56