QP/C  7.3.3
Real-Time Embedded Framework
Loading...
Searching...
No Matches
qf_mem.c
Go to the documentation of this file.
1//$file${src::qf::qf_mem.c} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2//
3// Model: qpc.qm
4// File: ${src::qf::qf_mem.c}
5//
6// This code has been generated by QM 6.1.1 <www.state-machine.com/qm>.
7// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
8//
9// This code is covered by the following QP license:
10// License # : LicenseRef-QL-dual
11// Issued to : Any user of the QP/C real-time embedded framework
12// Framework(s) : qpc
13// Support ends : 2024-12-31
14// License scope:
15//
16// Copyright (C) 2005 Quantum Leaps, LLC <state-machine.com>.
17//
18// Q u a n t u m L e a P s
19// ------------------------
20// Modern Embedded Software
21//
22// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
23//
24// This software is dual-licensed under the terms of the open source GNU
25// General Public License version 3 (or any later version), or alternatively,
26// under the terms of one of the closed source Quantum Leaps commercial
27// licenses.
28//
29// The terms of the open source GNU General Public License version 3
30// can be found at: <www.gnu.org/licenses/gpl-3.0>
31//
32// The terms of the closed source Quantum Leaps commercial licenses
33// can be found at: <www.state-machine.com/licensing>
34//
35// Redistributions in source code must retain this top-level comment block.
36// Plagiarizing this software to sidestep the license obligations is illegal.
37//
38// Contact information:
39// <www.state-machine.com/licensing>
40// <info@state-machine.com>
41//
42//$endhead${src::qf::qf_mem.c} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43#define QP_IMPL // this is QP implementation
44#include "qp_port.h" // QP port
45#include "qp_pkg.h" // QP package-scope interface
46#include "qsafe.h" // QP Functional Safety (FuSa) Subsystem
47#ifdef Q_SPY // QS software tracing enabled?
48 #include "qs_port.h" // QS port
49 #include "qs_pkg.h" // QS facilities for pre-defined trace records
50#else
51 #include "qs_dummy.h" // disable the QS software tracing
52#endif // Q_SPY
53
54Q_DEFINE_THIS_MODULE("qf_mem")
55
56//$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
57// Check for the minimum required QP version
58#if (QP_VERSION < 730U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
59#error qpc version 7.3.0 or higher required
60#endif
61//$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62
63//$define${QF::QMPool} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
64
65//${QF::QMPool} ..............................................................
66
67//${QF::QMPool::init} ........................................................
68//! @public @memberof QMPool
69void QMPool_init(QMPool * const me,
70 void * const poolSto,
71 uint_fast32_t const poolSize,
72 uint_fast16_t const blockSize)
73{
76 QF_MEM_SYS();
77
78 Q_REQUIRE_INCRIT(100, (poolSto != (void *)0)
79 && (poolSize >= (uint_fast32_t)sizeof(QFreeBlock))
80 && ((uint_fast16_t)(blockSize + sizeof(QFreeBlock)) > blockSize));
81
82 me->free_head = (QFreeBlock *)poolSto;
83
84 // find # free blocks in a memory block, NO DIVISION
85 me->blockSize = (QMPoolSize)sizeof(QFreeBlock);
86 uint_fast16_t nblocks = 1U;
87 while (me->blockSize < (QMPoolSize)blockSize) {
88 me->blockSize += (QMPoolSize)sizeof(QFreeBlock);
89 ++nblocks;
90 }
91
92 // the pool buffer must fit at least one rounded-up block
93 Q_ASSERT_INCRIT(110, poolSize >= me->blockSize);
94
95 // start at the head of the free list
96 QFreeBlock *fb = me->free_head;
97 me->nTot = 1U; // the last block already in the list
98
99 // chain all blocks together in a free-list...
100 for (uint_fast32_t size = poolSize - me->blockSize;
101 size >= (uint_fast32_t)me->blockSize;
102 size -= (uint_fast32_t)me->blockSize)
103 {
104 fb->next = &fb[nblocks]; // point next link to next block
105 #ifndef Q_UNSAFE
106 fb->next_dis = (uintptr_t)(~Q_UINTPTR_CAST_(fb->next));
107 #endif
108 fb = fb->next; // advance to the next block
109 ++me->nTot; // one more free block in the pool
110 }
111
112 fb->next = (QFreeBlock *)0; // the last link points to NULL
113 #ifndef Q_UNSAFE
114 fb->next_dis = (uintptr_t)(~Q_UINTPTR_CAST_(fb->next));
115 #endif
116
117 me->nFree = me->nTot; // all blocks are free
118 me->nMin = me->nTot; // the minimum # free blocks
119 me->start = poolSto; // the original start this pool buffer
120 me->end = fb; // the last block in this pool
121
122 QF_MEM_APP();
123 QF_CRIT_EXIT();
124}
125
126//${QF::QMPool::get} .........................................................
127//! @public @memberof QMPool
128void * QMPool_get(QMPool * const me,
129 uint_fast16_t const margin,
130 uint_fast8_t const qsId)
131{
132 #ifndef Q_SPY
133 Q_UNUSED_PAR(qsId);
134 #endif
135
138 QF_MEM_SYS();
139
140 // have more free blocks than the requested margin?
141 QFreeBlock *fb;
142 if (me->nFree > (QMPoolCtr)margin) {
143 fb = me->free_head; // get a free block
144
145 // a free block must be valid
146 Q_ASSERT_INCRIT(300, fb != (QFreeBlock *)0);
147
148 QFreeBlock * const fb_next = fb->next; // fast temporary
149
150 // the free block must have integrity (duplicate inverse storage)
151 Q_ASSERT_INCRIT(302, Q_UINTPTR_CAST_(fb_next)
152 == (uintptr_t)~fb->next_dis);
153
154 --me->nFree; // one less free block
155 if (me->nFree == 0U) { // is the pool becoming empty?
156 // pool is becoming empty, so the next free block must be NULL
157 Q_ASSERT_INCRIT(320, fb_next == (QFreeBlock *)0);
158
159 me->nMin = 0U; // remember that the pool got empty
160 }
161 else {
162 // invariant:
163 // The pool is not empty, so the next free-block pointer,
164 // so the next free block must be in range.
165
166 // NOTE: The next free block pointer can fall out of range
167 // when the client code writes past the memory block, thus
168 // corrupting the next block.
169 Q_ASSERT_INCRIT(330,
170 (me->start <= fb_next) && (fb_next <= me->end));
171
172 // is the # free blocks the new minimum so far?
173 if (me->nMin > me->nFree) {
174 me->nMin = me->nFree; // remember the new minimum
175 }
176 }
177
178 me->free_head = fb_next; // set the head to the next free block
179
180 QS_BEGIN_PRE_(QS_QF_MPOOL_GET, qsId)
181 QS_TIME_PRE_(); // timestamp
182 QS_OBJ_PRE_(me); // this memory pool
183 QS_MPC_PRE_(me->nFree); // # of free blocks in the pool
184 QS_MPC_PRE_(me->nMin); // min # free blocks ever in the pool
185 QS_END_PRE_()
186 }
187 else { // don't have enough free blocks at this point
188 fb = (QFreeBlock *)0;
189
190 QS_BEGIN_PRE_(QS_QF_MPOOL_GET_ATTEMPT, qsId)
191 QS_TIME_PRE_(); // timestamp
192 QS_OBJ_PRE_(me); // this memory pool
193 QS_MPC_PRE_(me->nFree); // # of free blocks in the pool
194 QS_MPC_PRE_(margin); // the requested margin
195 QS_END_PRE_()
196 }
197
198 QF_MEM_APP();
199 QF_CRIT_EXIT();
200
201 return fb; // return the block or NULL pointer to the caller
202}
203
204//${QF::QMPool::put} .........................................................
205//! @public @memberof QMPool
206void QMPool_put(QMPool * const me,
207 void * const block,
208 uint_fast8_t const qsId)
209{
210 #ifndef Q_SPY
211 Q_UNUSED_PAR(qsId);
212 #endif
213
214 QFreeBlock * const fb = (QFreeBlock *)block;
215
218 QF_MEM_SYS();
219
220 Q_REQUIRE_INCRIT(200, (me->nFree < me->nTot)
221 && (me->start <= fb) && (fb <= me->end));
222
223 fb->next = me->free_head; // link into list
224 #ifndef Q_UNSAFE
225 fb->next_dis = (uintptr_t)(~Q_UINTPTR_CAST_(fb->next));
226 #endif
227
228 // set as new head of the free list
229 me->free_head = block;
230
231 ++me->nFree; // one more free block in this pool
232
233 QS_BEGIN_PRE_(QS_QF_MPOOL_PUT, qsId)
234 QS_TIME_PRE_(); // timestamp
235 QS_OBJ_PRE_(me); // this memory pool
236 QS_MPC_PRE_(me->nFree); // the # free blocks in the pool
237 QS_END_PRE_()
238
239 QF_MEM_APP();
240 QF_CRIT_EXIT();
241}
242//$enddef${QF::QMPool} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
uint16_t QMPoolCtr
Definition qmpool.h:66
uint16_t QMPoolSize
Definition qmpool.h:56
#define QF_MEM_APP()
Definition qp.h:1284
#define Q_UNUSED_PAR(par_)
Definition qp.h:532
#define QF_MEM_SYS()
Definition qp.h:1279
Internal (package scope) QP/C interface.
#define Q_UINTPTR_CAST_(ptr_)
Definition qp_pkg.h:97
Sample QP/C port.
@ QS_QF_MPOOL_GET_ATTEMPT
attempt to get a memory block failed
Definition qs.h:148
#define QS_TIME_PRE_()
Definition qs.h:450
@ QS_QF_MPOOL_PUT
a memory block was returned to memory pool
Definition qs.h:114
@ QS_QF_MPOOL_GET
a memory block was removed from memory pool
Definition qs.h:113
QS/C package-scope interface.
Sample QS/C port.
QP Functional Safety (FuSa) Subsystem.
#define QF_CRIT_ENTRY()
Definition qsafe.h:58
#define Q_ASSERT_INCRIT(id_, expr_)
Definition qsafe.h:72
#define QF_CRIT_EXIT()
Definition qsafe.h:62
#define Q_REQUIRE_INCRIT(id_, expr_)
Definition qsafe.h:136
#define QF_CRIT_STAT
Definition qsafe.h:54
Structure representing a free block in QMPool.
Definition qmpool.h:81
uintptr_t next_dis
Definition qmpool.h:89
struct QFreeBlock * next
Definition qmpool.h:85
Native QF Memory Pool.
Definition qmpool.h:98
QFreeBlock * start
Definition qmpool.h:102
QMPoolCtr volatile nFree
Definition qmpool.h:117
QFreeBlock * end
Definition qmpool.h:105
QMPoolCtr nTot
Definition qmpool.h:114
QMPoolSize blockSize
Definition qmpool.h:111
QMPoolCtr nMin
Definition qmpool.h:120
QFreeBlock *volatile free_head
Definition qmpool.h:108