31 #ifndef ETL_POOL_INCLUDED
32 #define ETL_POOL_INCLUDED
45 #include "static_assert.h"
52 #define ETL_POOL_CPP03_CODE 0
70 pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
71 :
exception(reason_, file_name_, line_number_)
84 :
pool_exception(ETL_ERROR_TEXT(
"pool:allocation", ETL_FILE
"A"), file_name_, line_number_)
97 :
pool_exception(ETL_ERROR_TEXT(
"pool:not in pool", ETL_FILE
"B"), file_name_, line_number_)
110 :
pool_exception(ETL_ERROR_TEXT(
"pool:element size", ETL_FILE
"C"), file_name_, line_number_)
121 typedef size_t size_type;
128 template <
typename T>
131 if (
sizeof(T) > ITEM_SIZE)
136 return reinterpret_cast<T*
>(allocate_item());
139 #if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
145 template <
typename T>
148 T* p = allocate<T>();
163 template <
typename T,
typename T1>
166 T* p = allocate<T>();
176 template <
typename T,
typename T1,
typename T2>
177 T*
create(
const T1& value1,
const T2& value2)
179 T* p = allocate<T>();
183 ::new (p) T(value1, value2);
189 template <
typename T,
typename T1,
typename T2,
typename T3>
190 T*
create(
const T1& value1,
const T2& value2,
const T3& value3)
192 T* p = allocate<T>();
196 ::new (p) T(value1, value2, value3);
202 template <
typename T,
typename T1,
typename T2,
typename T3,
typename T4>
203 T*
create(
const T1& value1,
const T2& value2,
const T3& value3,
const T4& value4)
205 T* p = allocate<T>();
209 ::new (p) T(value1, value2, value3, value4);
218 template <
typename T,
typename... Args>
221 T* p = allocate<T>();
225 ::new (p) T(etl::forward<Args>(args)...);
237 template <
typename T>
240 if (
sizeof(T) > ITEM_SIZE)
257 const uintptr_t p = uintptr_t(p_object);
258 release_item((
char*)p);
267 items_initialised = 0;
278 const uintptr_t p = uintptr_t(p_object);
279 return is_item_in_pool((
const char*)p);
303 return MAX_SIZE - items_allocated;
311 return items_allocated;
320 return items_allocated == 0;
329 return items_allocated == MAX_SIZE;
337 ipool(
char* p_buffer_, uint32_t item_size_, uint32_t max_size_)
338 : p_buffer(p_buffer_),
341 items_initialised(0),
342 ITEM_SIZE(item_size_),
352 char* allocate_item()
354 char* p_value = ETL_NULLPTR;
357 if (items_allocated < MAX_SIZE)
360 if (items_initialised < MAX_SIZE)
362 char* p = p_buffer + (items_initialised * ITEM_SIZE);
363 char* np = p + ITEM_SIZE;
364 *
reinterpret_cast<char**
>(p) = np;
372 if (items_allocated != MAX_SIZE)
375 p_next = *
reinterpret_cast<char**
>(p_next);
380 p_next = ETL_NULLPTR;
385 ETL_ASSERT(
false, ETL_ERROR(pool_no_allocation));
394 void release_item(
char* p_value)
397 ETL_ASSERT(is_item_in_pool(p_value), ETL_ERROR(pool_object_not_in_pool));
399 if (p_next != ETL_NULLPTR)
402 *(uintptr_t*)p_value =
reinterpret_cast<uintptr_t
>(p_next);
407 *((uintptr_t*)p_value) = 0;
418 bool is_item_in_pool(
const char* p)
const
421 intptr_t distance = p - p_buffer;
422 bool is_within_range = (distance >= 0) && (distance <= intptr_t((ITEM_SIZE * MAX_SIZE) - ITEM_SIZE));
425 #if defined(ETL_DEBUG)
427 bool is_valid_address = ((distance % ITEM_SIZE) == 0);
429 bool is_valid_address =
true;
432 return is_within_range && is_valid_address;
442 uint32_t items_allocated;
443 uint32_t items_initialised;
445 const uint32_t ITEM_SIZE;
446 const uint32_t MAX_SIZE;
451 #if defined(ETL_POLYMORPHIC_POOL) || defined(ETL_POLYMORPHIC_CONTAINERS)
468 template <const
size_t TYPE_SIZE_, const
size_t ALIGNMENT_, const
size_t SIZE_>
473 static const size_t SIZE = SIZE_;
474 static const size_t ALIGNMENT = ALIGNMENT_;
475 static const size_t TYPE_SIZE = TYPE_SIZE_;
481 :
etl::
ipool(reinterpret_cast<char*>(&buffer[0]), ELEMENT_SIZE, SIZE)
491 template <
typename U>
495 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
496 return ipool::allocate<U>();
499 #if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
505 template <
typename U>
509 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
510 return ipool::create<U>();
518 template <
typename U,
typename T1>
522 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
523 return ipool::create<U>(value1);
531 template <
typename U,
typename T1,
typename T2>
532 U*
create(
const T1& value1,
const T2& value2)
535 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
536 return ipool::create<U>(value1, value2);
544 template <
typename U,
typename T1,
typename T2,
typename T3>
545 U*
create(
const T1& value1,
const T2& value2,
const T3& value3)
548 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
549 return ipool::create<U>(value1, value2, value3);
557 template <
typename U,
typename T1,
typename T2,
typename T3,
typename T4>
558 U*
create(
const T1& value1,
const T2& value2,
const T3& value3,
const T4& value4)
561 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
562 return ipool::create<U>(value1, value2, value3, value4);
568 template <
typename U,
typename... Args>
572 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
573 return ipool::create<U>(etl::forward<Args>(args)...);
582 template <
typename U>
586 ETL_STATIC_ASSERT(
sizeof(U) <= TYPE_SIZE,
"Type too large for pool");
597 char value[TYPE_SIZE_];
598 typename etl::type_with_alignment<ALIGNMENT_>::type dummy;
604 static const uint32_t ELEMENT_SIZE =
sizeof(Element);
615 template <
typename T, const
size_t SIZE_>
625 using base_t::ALIGNMENT;
626 using base_t::TYPE_SIZE;
644 return base_t::template allocate<T>();
647 #if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
655 return base_t::template create<T>();
663 template <
typename T1>
666 return base_t::template create<T>(value1);
674 template <
typename T1,
typename T2>
675 T*
create(
const T1& value1,
const T2& value2)
677 return base_t::template create<T>(value1, value2);
685 template <
typename T1,
typename T2,
typename T3>
686 T*
create(
const T1& value1,
const T2& value2,
const T3& value3)
688 return base_t::template create<T>(value1, value2, value3);
696 template <
typename T1,
typename T2,
typename T3,
typename T4>
697 T*
create(
const T1& value1,
const T2& value2,
const T3& value3,
const T4& value4)
699 return base_t::template create<T>(value1, value2, value3, value4);
707 template <
typename... Args>
710 return base_t::template create<T>(etl::forward<Args>(args)...);
719 template <
typename U>
Definition: alignment.h:116
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
size_t size() const
Returns the number of allocated items in the pool.
Definition: pool.h:309
void destroy(const U *const p_object)
Definition: pool.h:720
~ipool()
Destructor.
Definition: pool.h:458
bool empty() const
Definition: pool.h:318
T * create(const T1 &value1)
Definition: pool.h:664
bool is_in_pool(const void *p_object) const
Definition: pool.h:276
void release_all()
Release all objects in the pool.
Definition: pool.h:264
T * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition: pool.h:697
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition: pool.h:545
pool()
Constructor.
Definition: pool.h:631
generic_pool()
Constructor.
Definition: pool.h:480
bool full() const
Definition: pool.h:327
size_t max_size() const
Returns the maximum number of items in the pool.
Definition: pool.h:285
T * create(const T1 &value1)
Definition: pool.h:164
U * create(const T1 &value1, const T2 &value2)
Definition: pool.h:532
void release(const void *const p_object)
Definition: pool.h:255
size_t capacity() const
Returns the maximum number of items in the pool.
Definition: pool.h:293
ipool(char *p_buffer_, uint32_t item_size_, uint32_t max_size_)
Constructor.
Definition: pool.h:337
T * allocate()
Definition: pool.h:129
T * create()
Definition: pool.h:146
T * create(const T1 &value1, const T2 &value2)
Definition: pool.h:675
U * allocate()
Definition: pool.h:492
size_t available() const
Returns the number of free items in the pool.
Definition: pool.h:301
U * create()
Definition: pool.h:506
T * create()
Definition: pool.h:653
T * allocate()
Definition: pool.h:642
void destroy(const U *const p_object)
Definition: pool.h:583
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition: pool.h:558
void destroy(const T *const p_object)
Definition: pool.h:238
T * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition: pool.h:686
U * create(const T1 &value1)
Definition: pool.h:519
add_rvalue_reference
Definition: type_traits_generator.h:1348
is_base_of
Definition: type_traits_generator.h:1289
Definition: absolute.h:37