Embedded Template Library 1.0
Loading...
Searching...
No Matches
generic_pool.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_GENERIC_POOL_INCLUDED
32#define ETL_GENERIC_POOL_INCLUDED
33
34#include "platform.h"
35#include "alignment.h"
36#include "ipool.h"
37#include "static_assert.h"
38#include "type_traits.h"
39
40#define ETL_POOL_CPP03_CODE 0
41
42//*****************************************************************************
46//*****************************************************************************
47
48namespace etl
49{
50 //*************************************************************************
53 //*************************************************************************
54 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
55 class generic_pool : public etl::ipool
56 {
57 public:
58
59 static ETL_CONSTANT size_t SIZE = VSize;
60 static ETL_CONSTANT size_t ALIGNMENT = VAlignment;
61 static ETL_CONSTANT size_t TYPE_SIZE = VTypeSize;
62
63 //*************************************************************************
65 //*************************************************************************
67 : etl::ipool(reinterpret_cast<char*>(&buffer[0]), Element_Size, VSize)
68 {
69 }
70
71 //*************************************************************************
76 //*************************************************************************
77 template <typename U>
79 {
80 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
81 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
82 return ipool::allocate<U>();
83 }
84
85#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
86 //*************************************************************************
90 //*************************************************************************
91 template <typename U>
92 U* create()
93 {
94 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
95 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
96 return ipool::create<U>();
97 }
98
99 //*************************************************************************
104 //*************************************************************************
105 template <typename U, typename T1>
106 U* create(const T1& value1)
107 {
108 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
109 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
110 return ipool::create<U>(value1);
111 }
112
113 //*************************************************************************
118 //*************************************************************************
119 template <typename U, typename T1, typename T2>
120 U* create(const T1& value1, const T2& value2)
121 {
122 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
123 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
124 return ipool::create<U>(value1, value2);
125 }
126
127 //*************************************************************************
132 //*************************************************************************
133 template <typename U, typename T1, typename T2, typename T3>
134 U* create(const T1& value1, const T2& value2, const T3& value3)
135 {
136 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
137 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
138 return ipool::create<U>(value1, value2, value3);
139 }
140
141 //*************************************************************************
146 //*************************************************************************
147 template <typename U, typename T1, typename T2, typename T3, typename T4>
148 U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
149 {
150 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
151 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
152 return ipool::create<U>(value1, value2, value3, value4);
153 }
154#else
155 //*************************************************************************
157 //*************************************************************************
158 template <typename U, typename... Args>
159 U* create(Args&&... args)
160 {
161 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
162 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
163 return ipool::create<U>(etl::forward<Args>(args)...);
164 }
165#endif
166
167 //*************************************************************************
171 //*************************************************************************
172 template <typename U>
173 void destroy(const U* const p_object)
174 {
175 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
176 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
177 ipool::destroy(p_object);
178 }
179
180 private:
181
182 // The pool element.
183 union Element
184 {
185 char* next;
186 char value[VTypeSize];
187 typename etl::type_with_alignment<VAlignment>::type dummy;
188 };
189
191 typename etl::aligned_storage< sizeof(Element), etl::alignment_of<Element>::value>::type buffer[VSize];
192
193 static ETL_CONSTANT uint32_t Element_Size = sizeof(Element);
194
195 // Should not be copied.
196 generic_pool(const generic_pool&) ETL_DELETE;
197 generic_pool& operator=(const generic_pool&) ETL_DELETE;
198 };
199
200 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
201 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::SIZE;
202
203 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
204 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::ALIGNMENT;
205
206 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
207 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::TYPE_SIZE;
208
209 //*************************************************************************
213 //*************************************************************************
214 template <size_t VTypeSize, size_t VAlignment>
216 {
217 private:
218
219 // The pool element.
220 union element_internal
221 {
222 char* next;
223 char value[VTypeSize];
224 typename etl::type_with_alignment<VAlignment>::type dummy;
225 };
226
227 static const size_t ELEMENT_INTERNAL_SIZE = sizeof(element_internal);
228
229 public:
230
231 static ETL_CONSTANT size_t ALIGNMENT = VAlignment;
232 static ETL_CONSTANT size_t TYPE_SIZE = VTypeSize;
233
234 typedef typename etl::aligned_storage< sizeof(element_internal), etl::alignment_of<element_internal>::value>::type element;
235
236 //*************************************************************************
238 //*************************************************************************
239 generic_pool_ext(element* buffer, size_t size)
240 : etl::ipool(reinterpret_cast<char*>(&buffer[0]), ELEMENT_INTERNAL_SIZE, size)
241 {
242 }
243
244 //*************************************************************************
249 //*************************************************************************
250 template <typename U>
252 {
253 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
254 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
255 return ipool::allocate<U>();
256 }
257
258#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
259 //*************************************************************************
263 //*************************************************************************
264 template <typename U>
266 {
267 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
268 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
269 return ipool::create<U>();
270 }
271
272 //*************************************************************************
277 //*************************************************************************
278 template <typename U, typename T1>
279 U* create(const T1& value1)
280 {
281 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
282 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
283 return ipool::create<U>(value1);
284 }
285
286 //*************************************************************************
291 //*************************************************************************
292 template <typename U, typename T1, typename T2>
293 U* create(const T1& value1, const T2& value2)
294 {
295 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
296 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
297 return ipool::create<U>(value1, value2);
298 }
299
300 //*************************************************************************
305 //*************************************************************************
306 template <typename U, typename T1, typename T2, typename T3>
307 U* create(const T1& value1, const T2& value2, const T3& value3)
308 {
309 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
310 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
311 return ipool::create<U>(value1, value2, value3);
312 }
313
314 //*************************************************************************
319 //*************************************************************************
320 template <typename U, typename T1, typename T2, typename T3, typename T4>
321 U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
322 {
323 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
324 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
325 return ipool::create<U>(value1, value2, value3, value4);
326 }
327#else
328 //*************************************************************************
330 //*************************************************************************
331 template <typename U, typename... Args>
332 U* create(Args&&... args)
333 {
334 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
335 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
336 return ipool::create<U>(etl::forward<Args>(args)...);
337 }
338#endif
339
340 //*************************************************************************
344 //*************************************************************************
345 template <typename U>
346 void destroy(const U* const p_object)
347 {
348 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
349 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
350 ipool::destroy(p_object);
351 }
352
353 private:
354
355 // Should not be copied.
356 generic_pool_ext(const generic_pool_ext&) ETL_DELETE;
357 generic_pool_ext& operator=(const generic_pool_ext&) ETL_DELETE;
358 };
359
360 template <size_t VTypeSize, size_t VAlignment>
361 ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::ALIGNMENT;
362
363 template <size_t VTypeSize, size_t VAlignment>
364 ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::TYPE_SIZE;
365} // namespace etl
366
367#endif
Definition alignment.h:251
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:522
U * create(const T1 &value1)
Definition generic_pool.h:106
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:148
U * create(const T1 &value1)
Definition generic_pool.h:279
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:307
generic_pool_ext(element *buffer, size_t size)
Constructor.
Definition generic_pool.h:239
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:134
U * create(const T1 &value1, const T2 &value2)
Definition generic_pool.h:120
T * allocate()
Definition ipool.h:333
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:321
U * create()
Definition generic_pool.h:265
ipool(char *p_buffer_, uint32_t item_size_, uint32_t max_size_)
Constructor.
Definition ipool.h:550
U * allocate()
Definition generic_pool.h:78
generic_pool()
Constructor.
Definition generic_pool.h:66
U * create(const T1 &value1, const T2 &value2)
Definition generic_pool.h:293
void destroy(const U *const p_object)
Definition generic_pool.h:173
U * create()
Definition generic_pool.h:92
U * allocate()
Definition generic_pool.h:251
T * create()
Definition ipool.h:350
void destroy(const T *const p_object)
Definition ipool.h:443
void destroy(const U *const p_object)
Definition generic_pool.h:346
Definition generic_pool.h:216
Definition ipool.h:109
bitset_ext
Definition absolute.h:40