ChibiOS 21.11.5
chmempools.c
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/src/chmempools.c
21 * @brief Memory Pools code.
22 *
23 * @addtogroup oslib_mempools
24 * @details Memory Pools related APIs and services.
25 * <h2>Operation mode</h2>
26 * The Memory Pools APIs allow to allocate/free fixed size objects in
27 * <b>constant time</b> and reliably without memory fragmentation
28 * problems.<br>
29 * Memory Pools do not enforce any alignment constraint on the
30 * contained object however the objects must be properly aligned
31 * to contain a pointer to void.
32 * @pre In order to use the memory pools APIs the @p CH_CFG_USE_MEMPOOLS option
33 * must be enabled in @p chconf.h.
34 * @note Compatible with RT and NIL.
35 * @{
36 */
37
38#include "ch.h"
39
40#if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
41
42/*===========================================================================*/
43/* Module exported variables. */
44/*===========================================================================*/
45
46/*===========================================================================*/
47/* Module local types. */
48/*===========================================================================*/
49
50/*===========================================================================*/
51/* Module local variables. */
52/*===========================================================================*/
53
54/*===========================================================================*/
55/* Module local functions. */
56/*===========================================================================*/
57
58/*===========================================================================*/
59/* Module exported functions. */
60/*===========================================================================*/
61
62/**
63 * @brief Initializes an empty memory pool.
64 *
65 * @param[out] mp pointer to a @p memory_pool_t structure
66 * @param[in] size the size of the objects contained in this memory pool,
67 * the minimum accepted size is the size of a pointer to
68 * void.
69 * @param[in] align required memory alignment
70 * @param[in] provider memory provider function for the memory pool or
71 * @p NULL if the pool is not allowed to grow
72 * automatically
73 *
74 * @init
75 */
77 unsigned align, memgetfunc_t provider) {
78
79 chDbgCheck((mp != NULL) &&
80 (size >= sizeof(void *)) &&
81 (align >= PORT_NATURAL_ALIGN) &&
83
84 mp->next = NULL;
85 mp->object_size = size;
86 mp->align = align;
87 mp->provider = provider;
88}
89
90/**
91 * @brief Loads a memory pool with an array of static objects.
92 * @pre The memory pool must already be initialized.
93 * @pre The array elements must be of the right size for the specified
94 * memory pool.
95 * @pre The array elements size must be a multiple of the alignment
96 * requirement for the pool.
97 * @post The memory pool contains the elements of the input array.
98 *
99 * @param[in] mp pointer to a @p memory_pool_t structure
100 * @param[in] p pointer to the array first element
101 * @param[in] n number of elements in the array
102 *
103 * @api
104 */
105void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n) {
106
107 chDbgCheck((mp != NULL) && (n != 0U));
108
109 while (n != 0U) {
110 chPoolAdd(mp, p);
111 /*lint -save -e9087 [11.3] Safe cast.*/
112 p = (void *)(((uint8_t *)p) + mp->object_size);
113 /*lint -restore*/
114 n--;
115 }
116}
117
118/**
119 * @brief Allocates an object from a memory pool.
120 * @pre The memory pool must already be initialized.
121 *
122 * @param[in] mp pointer to a @p memory_pool_t structure
123 * @return The pointer to the allocated object.
124 * @retval NULL if pool is empty.
125 *
126 * @iclass
127 */
129 void *objp;
130
132 chDbgCheck(mp != NULL);
133
134 objp = mp->next;
135 /*lint -save -e9013 [15.7] There is no else because it is not needed.*/
136 if (objp != NULL) {
137 mp->next = mp->next->next;
138 }
139 else if (mp->provider != NULL) {
140 objp = mp->provider(mp->object_size, mp->align);
141
143 "returned object not aligned");
144 }
145 /*lint -restore*/
146
147 return objp;
148}
149
150/**
151 * @brief Allocates an object from a memory pool.
152 * @pre The memory pool must already be initialized.
153 *
154 * @param[in] mp pointer to a @p memory_pool_t structure
155 * @return The pointer to the allocated object.
156 * @retval NULL if pool is empty.
157 *
158 * @api
159 */
161 void *objp;
162
163 chSysLock();
164 objp = chPoolAllocI(mp);
165 chSysUnlock();
166
167 return objp;
168}
169
170/**
171 * @brief Releases an object into a memory pool.
172 * @pre The memory pool must already be initialized.
173 * @pre The freed object must be of the right size for the specified
174 * memory pool.
175 * @pre The added object must be properly aligned.
176 *
177 * @param[in] mp pointer to a @p memory_pool_t structure
178 * @param[in] objp the pointer to the object to be released
179 *
180 * @iclass
181 */
182void chPoolFreeI(memory_pool_t *mp, void *objp) {
183 struct pool_header *php = objp;
184
186 chDbgCheck((mp != NULL) &&
187 (objp != NULL) &&
188 MEM_IS_ALIGNED(objp, mp->align));
189
190 php->next = mp->next;
191 mp->next = php;
192}
193
194/**
195 * @brief Releases an object into a memory pool.
196 * @pre The memory pool must already be initialized.
197 * @pre The freed object must be of the right size for the specified
198 * memory pool.
199 * @pre The added object must be properly aligned.
200 *
201 * @param[in] mp pointer to a @p memory_pool_t structure
202 * @param[in] objp the pointer to the object to be released
203 *
204 * @api
205 */
206void chPoolFree(memory_pool_t *mp, void *objp) {
207
208 chSysLock();
209 chPoolFreeI(mp, objp);
210 chSysUnlock();
211}
212
213#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
214/**
215 * @brief Initializes an empty guarded memory pool.
216 *
217 * @param[out] gmp pointer to a @p guarded_memory_pool_t structure
218 * @param[in] size the size of the objects contained in this guarded
219 * memory pool, the minimum accepted size is the size
220 * of a pointer to void.
221 * @param[in] align required memory alignment
222 *
223 * @init
224 */
226 size_t size,
227 unsigned align) {
228
229 chPoolObjectInitAligned(&gmp->pool, size, align, NULL);
230 chSemObjectInit(&gmp->sem, (cnt_t)0);
231}
232
233/**
234 * @brief Loads a guarded memory pool with an array of static objects.
235 * @pre The guarded memory pool must already be initialized.
236 * @pre The array elements must be of the right size for the specified
237 * guarded memory pool.
238 * @post The guarded memory pool contains the elements of the input array.
239 *
240 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
241 * @param[in] p pointer to the array first element
242 * @param[in] n number of elements in the array
243 *
244 * @api
245 */
246void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n) {
247
248 chDbgCheck((gmp != NULL) && (n != 0U));
249
250 while (n != 0U) {
251 chGuardedPoolAdd(gmp, p);
252 /*lint -save -e9087 [11.3] Safe cast.*/
253 p = (void *)(((uint8_t *)p) + gmp->pool.object_size);
254 /*lint -restore*/
255 n--;
256 }
257}
258
259/**
260 * @brief Allocates an object from a guarded memory pool.
261 * @pre The guarded memory pool must already be initialized.
262 *
263 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
264 * @param[in] timeout the number of ticks before the operation timeouts,
265 * the following special values are allowed:
266 * - @a TIME_IMMEDIATE immediate timeout.
267 * - @a TIME_INFINITE no timeout.
268 * .
269 * @return The pointer to the allocated object.
270 * @retval NULL if the operation timed out.
271 *
272 * @sclass
273 */
275 sysinterval_t timeout) {
276 msg_t msg;
277
278 msg = chSemWaitTimeoutS(&gmp->sem, timeout);
279 if (msg != MSG_OK) {
280 return NULL;
281 }
282
283 return chPoolAllocI(&gmp->pool);
284}
285
286/**
287 * @brief Allocates an object from a guarded memory pool.
288 * @pre The guarded memory pool must already be initialized.
289 *
290 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
291 * @param[in] timeout the number of ticks before the operation timeouts,
292 * the following special values are allowed:
293 * - @a TIME_IMMEDIATE immediate timeout.
294 * - @a TIME_INFINITE no timeout.
295 * .
296 * @return The pointer to the allocated object.
297 * @retval NULL if the operation timed out.
298 *
299 * @api
300 */
302 sysinterval_t timeout) {
303 void *p;
304
305 chSysLock();
306 p = chGuardedPoolAllocTimeoutS(gmp, timeout);
307 chSysUnlock();
308
309 return p;
310}
311
312/**
313 * @brief Releases an object into a guarded memory pool.
314 * @pre The guarded memory pool must already be initialized.
315 * @pre The freed object must be of the right size for the specified
316 * guarded memory pool.
317 * @pre The added object must be properly aligned.
318 *
319 * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
320 * @param[in] objp the pointer to the object to be released
321 *
322 * @api
323 */
325
326 chSysLock();
327 chGuardedPoolFreeI(gmp, objp);
329 chSysUnlock();
330}
331#endif
332
333#endif /* CH_CFG_USE_MEMPOOLS == TRUE */
334
335/** @} */
#define chSysUnlock()
Leaves the kernel lock state.
#define chSysLock()
Enters the kernel lock state.
#define chSemObjectInit(sp, n)
Initializes a semaphore with the specified counter value.
#define chDbgAssert(c, r)
Condition assertion.
Definition chdebug.h:143
#define chDbgCheck(c)
Function parameters check.
Definition chdebug.h:117
#define chDbgCheckClassI()
Definition chdebug.h:98
#define MEM_IS_VALID_ALIGNMENT(a)
Returns whatever a constant is a valid alignment.
Definition chalign.h:98
#define MEM_IS_ALIGNED(p, a)
Returns whatever a pointer or memory size is aligned.
Definition chalign.h:90
int32_t cnt_t
Definition chearly.h:91
int32_t msg_t
Definition chearly.h:87
void *(* memgetfunc_t)(size_t size, unsigned align)
Memory get function.
Definition chmemcore.h:70
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
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
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
void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp)
Releases an object into a guarded memory pool.
Definition chmempools.c:324
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
#define MSG_OK
Normal wakeup message.
Definition chschd.h:38
msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout)
Performs a wait operation on a semaphore with timeout specification.
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