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>
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 p_object->~U();
174 ipool::release(p_object);
175 }
176
177 private:
178
179 // The pool element.
180 union Element
181 {
182 char* next;
183 char value[VTypeSize];
184 typename etl::type_with_alignment<VAlignment>::type dummy;
185 };
186
188 typename etl::aligned_storage<sizeof(Element), etl::alignment_of<Element>::value>::type buffer[VSize];
189
190 static ETL_CONSTANT uint32_t Element_Size = sizeof(Element);
191
192 // Should not be copied.
193 generic_pool(const generic_pool&) ETL_DELETE;
194 generic_pool& operator =(const generic_pool&) ETL_DELETE;
195 };
196
197 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
198 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::SIZE;
199
200 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
201 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::ALIGNMENT;
202
203 template <size_t VTypeSize, size_t VAlignment, size_t VSize>
204 ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::TYPE_SIZE;
205
206 //*************************************************************************
210 //*************************************************************************
211 template <size_t VTypeSize, size_t VAlignment>
213 {
214 private:
215 // The pool element.
216 union element_internal
217 {
218 char* next;
219 char value[VTypeSize];
220 typename etl::type_with_alignment<VAlignment>::type dummy;
221 };
222
223 static const size_t ELEMENT_INTERNAL_SIZE = sizeof(element_internal);
224
225 public:
226 static ETL_CONSTANT size_t ALIGNMENT = VAlignment;
227 static ETL_CONSTANT size_t TYPE_SIZE = VTypeSize;
228
229 typedef typename etl::aligned_storage<sizeof(element_internal), etl::alignment_of<element_internal>::value>::type element;
230
231 //*************************************************************************
233 //*************************************************************************
234 generic_pool_ext(element* buffer, size_t size)
235 : etl::ipool(reinterpret_cast<char*>(&buffer[0]), ELEMENT_INTERNAL_SIZE, size)
236 {
237 }
238
239 //*************************************************************************
244 //*************************************************************************
245 template <typename U>
247 {
248 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
249 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
250 return ipool::allocate<U>();
251 }
252
253#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
254 //*************************************************************************
258 //*************************************************************************
259 template <typename U>
261 {
262 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
263 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
264 return ipool::create<U>();
265 }
266
267 //*************************************************************************
271 //*************************************************************************
272 template <typename U, typename T1>
273 U* create(const T1& value1)
274 {
275 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
276 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
277 return ipool::create<U>(value1);
278 }
279
280 //*************************************************************************
284 //*************************************************************************
285 template <typename U, typename T1, typename T2>
286 U* create(const T1& value1, const T2& value2)
287 {
288 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
289 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
290 return ipool::create<U>(value1, value2);
291 }
292
293 //*************************************************************************
297 //*************************************************************************
298 template <typename U, typename T1, typename T2, typename T3>
299 U* create(const T1& value1, const T2& value2, const T3& value3)
300 {
301 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
302 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
303 return ipool::create<U>(value1, value2, value3);
304 }
305
306 //*************************************************************************
310 //*************************************************************************
311 template <typename U, typename T1, typename T2, typename T3, typename T4>
312 U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
313 {
314 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
315 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
316 return ipool::create<U>(value1, value2, value3, value4);
317 }
318#else
319 //*************************************************************************
321 //*************************************************************************
322 template <typename U, typename... Args>
323 U* create(Args&&... args)
324 {
325 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
326 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
327 return ipool::create<U>(etl::forward<Args>(args)...);
328 }
329#endif
330
331 //*************************************************************************
335 //*************************************************************************
336 template <typename U>
337 void destroy(const U* const p_object)
338 {
339 ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
340 ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
341 p_object->~U();
342 ipool::release(p_object);
343 }
344
345 private:
346 // Should not be copied.
347 generic_pool_ext(const generic_pool_ext&) ETL_DELETE;
348 generic_pool_ext& operator=(const generic_pool_ext&) ETL_DELETE;
349 };
350
351 template <size_t VTypeSize, size_t VAlignment>
352 ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::ALIGNMENT;
353
354 template <size_t VTypeSize, size_t VAlignment>
355 ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::TYPE_SIZE;
356}
357
358#endif
359
Definition alignment.h:231
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:293
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:273
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:299
generic_pool_ext(element *buffer, size_t size)
Constructor.
Definition generic_pool.h:234
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
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:312
U * create()
Definition generic_pool.h:260
void release(const void *const p_object)
Definition ipool.h:239
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:286
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:246
void destroy(const U *const p_object)
Definition generic_pool.h:337
Definition generic_pool.h:56
Definition generic_pool.h:213
Definition ipool.h:102
add_rvalue_reference
Definition type_traits_generator.h:1327
bitset_ext
Definition absolute.h:38
Definition alignment.h:233
pair holds two objects of arbitrary type
Definition utility.h:164