QP/C++  6.9.3
Real-Time Embedded Framework
qmpool.hpp
Go to the documentation of this file.
1 
39 #ifndef QMPOOL_HPP
40 #define QMPOOL_HPP
41 
42 #ifndef QF_MPOOL_SIZ_SIZE
45  #define QF_MPOOL_SIZ_SIZE 2U
46 #endif
47 
48 #ifndef QF_MPOOL_CTR_SIZE
51  #define QF_MPOOL_CTR_SIZE 2
52 #endif
53 
54 namespace QP {
55 #if (QF_MPOOL_SIZ_SIZE == 1U)
57 #elif (QF_MPOOL_SIZ_SIZE == 2U)
63  using QMPoolSize = std::uint16_t;
64 #elif (QF_MPOOL_SIZ_SIZE == 4U)
65  using QMPoolSize = std::uint32_t;
66 #else
67  #error "QF_MPOOL_SIZ_SIZE defined incorrectly, expected 1U, 2U, or 4U"
68 #endif
69 
70 #if (QF_MPOOL_CTR_SIZE == 1U)
72 #elif (QF_MPOOL_CTR_SIZE == 2U)
78  using QMPoolCtr = std::uint16_t;
79 #elif (QF_MPOOL_CTR_SIZE == 4U)
80  using QMPoolCtr = std::uint32_t;
81 #else
82  #error "QF_MPOOL_CTR_SIZE defined incorrectly, expected 1U, 2U, or 4U"
83 #endif
84 
85 //****************************************************************************
106 class QMPool {
107 private:
108 
110  void *m_start;
111 
113  void *m_end;
114 
116  void * volatile m_free_head;
117 
120 
123 
125  QMPoolCtr volatile m_nFree;
126 
134 
135 public:
136  QMPool(void);
137 
139  void init(void * const poolSto, std::uint_fast32_t poolSize,
140  std::uint_fast16_t blockSize) noexcept;
141 
143  void *get(std::uint_fast16_t const margin,
144  std::uint_fast8_t const qs_id) noexcept;
145 
147  void put(void * const b, std::uint_fast8_t const qs_id) noexcept;
148 
150  QMPoolSize getBlockSize(void) const noexcept {
151  return m_blockSize;
152  }
153 
154 // duplicated API to be used exclusively inside ISRs (useful in some QP ports)
155 #ifdef QF_ISR_API
156  void *getFromISR(std::uint_fast16_t const margin,
157  std::uint_fast8_t const qs_id) noexcept;
158  void putFromISR(void * const b,
159  std::uint_fast8_t const qs_id) noexcept;
160 #endif // QF_ISR_API
161 
162 private:
164  QMPool(QMPool const &) = delete;
165 
167  QMPool &operator=(QMPool const &) = delete;
168 
169  friend class QF;
170  friend class QS;
171 };
172 
173 } // namespace QP
174 
176 #define QF_MPOOL_EL(type_) \
177  struct { void *sto_[((sizeof(type_) - 1U)/sizeof(void*)) + 1U]; }
178 
179 #endif // QMPOOL_HPP
180 
unsigned int uint16_t
Definition: 16bit/stdint.h:30
unsigned int uint_fast16_t
Definition: 16bit/stdint.h:38
unsigned long int uint32_t
Definition: 16bit/stdint.h:31
unsigned char uint8_t
Definition: 16bit/stdint.h:29
unsigned int uint_fast8_t
Definition: 16bit/stdint.h:36
unsigned long uint_fast32_t
Definition: 16bit/stdint.h:40
Definition: qf.hpp:496
void put(void *const b, std::uint_fast8_t const qs_id) noexcept
Returns a memory block back to a memory pool.
Definition: qf_mem.cpp:174
void * m_end
end of the memory managed by this memory pool
Definition: qmpool.hpp:113
void init(void *const poolSto, std::uint_fast32_t poolSize, std::uint_fast16_t blockSize) noexcept
Initializes the native QF event pool.
Definition: qf_mem.cpp:103
QMPoolSize m_blockSize
maximum block size (in bytes)
Definition: qmpool.hpp:119
void *volatile m_free_head
head of linked list of free blocks
Definition: qmpool.hpp:116
void * m_start
start of the memory managed by this memory pool
Definition: qmpool.hpp:110
void * get(std::uint_fast16_t const margin, std::uint_fast8_t const qs_id) noexcept
Obtains a memory block from a memory pool.
Definition: qf_mem.cpp:224
QMPoolCtr m_nMin
Definition: qmpool.hpp:133
QMPoolCtr volatile m_nFree
number of free blocks remaining
Definition: qmpool.hpp:125
QMPool(void)
public default constructor
Definition: qf_mem.cpp:63
QMPool & operator=(QMPool const &)=delete
QMPoolCtr m_nTot
total number of blocks
Definition: qmpool.hpp:122
QMPoolSize getBlockSize(void) const noexcept
return the fixed block-size of the blocks managed by this pool
Definition: qmpool.hpp:150
QMPool(QMPool const &)=delete
disallow copying of QMPools
Definition: qs.hpp:302
Definition: struct.dox:1
std::uint8_t QMPoolCtr
Definition: qmpool.hpp:71
std::uint8_t QMPoolSize
Definition: qmpool.hpp:56