forked from QuantumLeaps/qpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmpool.h
171 lines (152 loc) · 5.52 KB
/
qmpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file
* @brief QP native, platform-independent memory pool ::QMPool interface.
* @ingroup qf
* @cond
******************************************************************************
* Last updated for version 6.9.1
* Last updated on 2020-09-08
*
* Q u a n t u m L e a P s
* ------------------------
* Modern Embedded Software
*
* Copyright (C) 2005-2020 Quantum Leaps, LLC. All rights reserved.
*
* This program is open source software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <www.gnu.org/licenses>.
*
* Contact information:
* <www.state-machine.com/licensing>
******************************************************************************
* @endcond
*/
#ifndef QMPOOL_H
#define QMPOOL_H
/****************************************************************************/
#ifndef QF_MPOOL_SIZ_SIZE
/*! macro to override the default ::QMPoolSize size [bytes].
* Valid values 1U, 2U, or 4U; default 2U
*/
#define QF_MPOOL_SIZ_SIZE 2U
#endif
#if (QF_MPOOL_SIZ_SIZE == 1U)
/*! The data type to store the block-size based on the macro
* #QF_MPOOL_SIZ_SIZE.
*/
/**
* @description
* The dynamic range of this data type determines the maximum size
* of blocks that can be managed by the native QF event pool.
*/
typedef uint8_t QMPoolSize;
#elif (QF_MPOOL_SIZ_SIZE == 2U)
typedef uint16_t QMPoolSize;
#elif (QF_MPOOL_SIZ_SIZE == 4U)
typedef uint32_t QMPoolSize;
#else
#error "QF_MPOOL_SIZ_SIZE defined incorrectly, expected 1U, 2U, or 4U"
#endif
/****************************************************************************/
#ifndef QF_MPOOL_CTR_SIZE
/*! macro to override the default ::QMPoolCtr size [bytes].
* Valid values 1U, 2U, or 4U; default 2U
*/
#define QF_MPOOL_CTR_SIZE 2U
#endif
#if (QF_MPOOL_CTR_SIZE == 1U)
/*! The data type to store the block-counter based on the macro
* #QF_MPOOL_CTR_SIZE.
*/
/**
* @description
* The dynamic range of this data type determines the maximum number
* of blocks that can be stored in the pool.
*/
typedef uint8_t QMPoolCtr;
#elif (QF_MPOOL_CTR_SIZE == 2U)
typedef uint16_t QMPoolCtr;
#elif (QF_MPOOL_CTR_SIZE == 4U)
typedef uint32_t QMPoolCtr;
#else
#error "QF_MPOOL_CTR_SIZE defined incorrectly, expected 1U, 2U, or 4U"
#endif
/****************************************************************************/
/*! Native QF Memory Pool */
/**
* @description
* A fixed block-size memory pool is a very fast and efficient data
* structure for dynamic allocation of fixed block-size chunks of memory.
* A memory pool offers fast and deterministic allocation and recycling of
* memory blocks and is not subject to fragmenation.@n
* @n
* The ::QMPool class describes the native QF memory pool, which can be used as
* the event pool for dynamic event allocation, or as a fast, deterministic
* fixed block-size heap for any other objects in your application.
*
* @note
* ::QMPool contains only data members for managing a memory pool, but
* does not contain the pool storage, which must be provided externally
* during the pool initialization.
*
* @note
* The native QF event pool is configured by defining the macro
* #QF_EPOOL_TYPE_ as ::QMPool in the specific QF port header file.
*/
typedef struct {
/*! The head of the linked list of free blocks */
void * volatile free_head;
/*! the original start this pool */
void *start;
/*! the last memory block managed by this memory pool */
void *end;
/*! maximum block size (in bytes) */
QMPoolSize blockSize;
/*! total number of blocks */
QMPoolCtr nTot;
/*! number of free blocks remaining */
QMPoolCtr volatile nFree;
/*! minimum number of free blocks ever present in this pool */
/**
* @description
* this attribute remembers the low watermark of the pool, which
* provides a valuable information for sizing event pools.
* @sa QF_getPoolMin().
*/
QMPoolCtr nMin;
} QMPool;
/* public functions: */
/*! Initializes the native QF memory pool */
void QMPool_init(QMPool * const me, void * const poolSto,
uint_fast32_t poolSize, uint_fast16_t blockSize);
/*! Obtains a memory block from a memory pool. */
void *QMPool_get(QMPool * const me, uint_fast16_t const margin,
uint_fast8_t const qs_id);
/*! Recycles a memory block back to a memory pool. */
void QMPool_put(QMPool * const me, void *b,
uint_fast8_t const qs_id);
/*! Memory pool element to allocate correctly aligned storage
* for QMPool class.
*/
/**
* @param[in] evType_ event type (name of the subclass of QEvt)
*/
#define QF_MPOOL_EL(evType_) \
struct { void *sto_[((sizeof(evType_) - 1U)/sizeof(void*)) + 1U]; }
#endif /* QMPOOL_H */