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 "ipool.h"
36#include "type_traits.h"
37#include "static_assert.h"
38#include "alignment.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 //*************************************************************************
103 //*************************************************************************
104 template <typename U, typename T1>
105 U* create(const T1& value1)
106 {
107 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
108 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
109 return ipool::create<U>(value1);
110 }
111
112 //*************************************************************************
116 //*************************************************************************
117 template <typename U, typename T1, typename T2>
118 U* create(const T1& value1, const T2& value2)
119 {
120 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
121 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
122 return ipool::create<U>(value1, value2);
123 }
124
125 //*************************************************************************
129 //*************************************************************************
130 template <typename U, typename T1, typename T2, typename T3>
131 U* create(const T1& value1, const T2& value2, const T3& value3)
132 {
133 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
134 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
135 return ipool::create<U>(value1, value2, value3);
136 }
137
138 //*************************************************************************
142 //*************************************************************************
143 template <typename U, typename T1, typename T2, typename T3, typename T4>
144 U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
145 {
146 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
147 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
148 return ipool::create<U>(value1, value2, value3, value4);
149 }
150#else
151 //*************************************************************************
153 //*************************************************************************
154 template <typename U, typename... Args>
155 U* create(Args&&... args)
156 {
157 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
158 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
159 return ipool::create<U>(etl::forward<Args>(args)...);
160 }
161#endif
162
163 //*************************************************************************
167 //*************************************************************************
168 template <typename U>
169 void destroy(const U* const p_object)
170 {
171 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
172 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
173 ipool::destroy(p_object);
174 }
175
176 private:
177
178 // The pool element.
179 union Element
180 {
181 char* next;
182 char value[VTypeSize];
183 typename etl::type_with_alignment<VAlignment>::type dummy;
184 };
185
187 typename etl::aligned_storage<sizeof(Element), etl::alignment_of<Element>::value>::type buffer[VSize];
188
189 static ETL_CONSTANT uint32_t Element_Size = sizeof(Element);
190
191 // Should not be copied.
192 generic_pool(const generic_pool&) ETL_DELETE;
193 generic_pool& operator =(const generic_pool&) ETL_DELETE;
194 };
195
196 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
197 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::SIZE;
198
199 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
200 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::ALIGNMENT;
201
202 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
203 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::TYPE_SIZE;
204
205 //*************************************************************************
209 //*************************************************************************
210 template <size_t VTypeSize, size_t VAlignment>
212 {
213 private:
214 // The pool element.
215 union element_internal
216 {
217 char* next;
218 char value[VTypeSize];
219 typename etl::type_with_alignment<VAlignment>::type dummy;
220 };
221
222 static const size_t ELEMENT_INTERNAL_SIZE = sizeof(element_internal);
223
224 public:
225 static ETL_CONSTANT size_t ALIGNMENT = VAlignment;
226 static ETL_CONSTANT size_t TYPE_SIZE = VTypeSize;
227
228 typedef typename etl::aligned_storage<sizeof(element_internal), etl::alignment_of<element_internal>::value>::type element;
229
230 //*************************************************************************
232 //*************************************************************************
233 generic_pool_ext(element* buffer, size_t size)
234 : etl::ipool(reinterpret_cast<char*>(&buffer[0]), ELEMENT_INTERNAL_SIZE, size)
235 {
236 }
237
238 //*************************************************************************
243 //*************************************************************************
244 template <typename U>
246 {
247 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
248 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
249 return ipool::allocate<U>();
250 }
251
252#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
253 //*************************************************************************
257 //*************************************************************************
258 template <typename U>
260 {
261 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
262 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
263 return ipool::create<U>();
264 }
265
266 //*************************************************************************
270 //*************************************************************************
271 template <typename U, typename T1>
272 U* create(const T1& value1)
273 {
274 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
275 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
276 return ipool::create<U>(value1);
277 }
278
279 //*************************************************************************
283 //*************************************************************************
284 template <typename U, typename T1, typename T2>
285 U* create(const T1& value1, const T2& value2)
286 {
287 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
288 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
289 return ipool::create<U>(value1, value2);
290 }
291
292 //*************************************************************************
296 //*************************************************************************
297 template <typename U, typename T1, typename T2, typename T3>
298 U* create(const T1& value1, const T2& value2, const T3& value3)
299 {
300 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
301 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
302 return ipool::create<U>(value1, value2, value3);
303 }
304
305 //*************************************************************************
309 //*************************************************************************
310 template <typename U, typename T1, typename T2, typename T3, typename T4>
311 U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
312 {
313 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
314 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
315 return ipool::create<U>(value1, value2, value3, value4);
316 }
317#else
318 //*************************************************************************
320 //*************************************************************************
321 template <typename U, typename... Args>
322 U* create(Args&&... args)
323 {
324 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
325 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
326 return ipool::create<U>(etl::forward<Args>(args)...);
327 }
328#endif
329
330 //*************************************************************************
334 //*************************************************************************
335 template <typename U>
336 void destroy(const U* const p_object)
337 {
338 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
339 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
340 ipool::destroy(p_object);
341 }
342
343 private:
344 // Should not be copied.
345 generic_pool_ext(const generic_pool_ext&) ETL_DELETE;
346 generic_pool_ext& operator=(const generic_pool_ext&) ETL_DELETE;
347 };
348
349 template <size_t VTypeSize, size_t VAlignment>
350 ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::ALIGNMENT;
351
352 template <size_t VTypeSize, size_t VAlignment>
353 ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::TYPE_SIZE;
354}
355
356#endif
357
Definition alignment.h:246
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:504
U * create(const T1 &value1)
Definition generic_pool.h:105
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:144
U * create(const T1 &value1)
Definition generic_pool.h:272
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:298
generic_pool_ext(element *buffer, size_t size)
Constructor.
Definition generic_pool.h:233
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:131
U * create(const T1 &value1, const T2 &value2)
Definition generic_pool.h:118
T * allocate()
Definition ipool.h:316
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:311
U * create()
Definition generic_pool.h:259
ipool(char *p_buffer_, uint32_t item_size_, uint32_t max_size_)
Constructor.
Definition ipool.h:532
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:285
void destroy(const U *const p_object)
Definition generic_pool.h:169
U * create()
Definition generic_pool.h:92
U * allocate()
Definition generic_pool.h:245
T * create()
Definition ipool.h:333
void destroy(const T *const p_object)
Definition ipool.h:425
void destroy(const U *const p_object)
Definition generic_pool.h:336
Definition generic_pool.h:212
Definition ipool.h:103
add_rvalue_reference
Definition type_traits_generator.h:1413
bitset_ext
Definition absolute.h:39