31#ifndef ETL_IPOOL_INCLUDED
32#define ETL_IPOOL_INCLUDED
37#include "static_assert.h"
42#define ETL_POOL_CPP03_CODE 0
54 pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
55 :
exception(reason_, file_name_, line_number_)
68 :
pool_exception(ETL_ERROR_TEXT(
"pool:allocation", ETL_POOL_FILE_ID
"A"), file_name_, line_number_)
81 :
pool_exception(ETL_ERROR_TEXT(
"pool:not in pool", ETL_POOL_FILE_ID
"B"), file_name_, line_number_)
94 :
pool_exception(ETL_ERROR_TEXT(
"pool:element size", ETL_POOL_FILE_ID
"C"), file_name_, line_number_)
105 typedef size_t size_type;
112 template <
typename T>
115 if (
sizeof(T) > Item_Size)
120 return reinterpret_cast<T*
>(allocate_item());
123#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
129 template <
typename T>
132 T* p = allocate<T>();
147 template <
typename T,
typename T1>
150 T* p = allocate<T>();
160 template <
typename T,
typename T1,
typename T2>
161 T*
create(
const T1& value1,
const T2& value2)
163 T* p = allocate<T>();
167 ::new (p) T(value1, value2);
173 template <
typename T,
typename T1,
typename T2,
typename T3>
174 T*
create(
const T1& value1,
const T2& value2,
const T3& value3)
176 T* p = allocate<T>();
180 ::new (p) T(value1, value2, value3);
186 template <
typename T,
typename T1,
typename T2,
typename T3,
typename T4>
187 T*
create(
const T1& value1,
const T2& value2,
const T3& value3,
const T4& value4)
189 T* p = allocate<T>();
193 ::new (p) T(value1, value2, value3, value4);
202 template <
typename T,
typename... Args>
205 T* p = allocate<T>();
209 ::new (p) T(etl::forward<Args>(args)...);
221 template <
typename T>
224 if (
sizeof(T) > Item_Size)
241 const uintptr_t p = uintptr_t(p_object);
242 release_item((
char*)p);
251 items_initialised = 0;
262 const uintptr_t p = uintptr_t(p_object);
263 return is_item_in_pool((
const char*)p);
287 return Max_Size - items_allocated;
295 return items_allocated;
304 return items_allocated == 0;
313 return items_allocated == Max_Size;
321 ipool(
char* p_buffer_, uint32_t item_size_, uint32_t max_size_)
322 : p_buffer(p_buffer_),
325 items_initialised(0),
326 Item_Size(item_size_),
336 char* allocate_item()
338 char* p_value = ETL_NULLPTR;
341 if (items_allocated < Max_Size)
344 if (items_initialised < Max_Size)
346 char* p = p_buffer + (items_initialised * Item_Size);
347 char* np = p + Item_Size;
348 *
reinterpret_cast<char**
>(p) = np;
356 if (items_allocated < Max_Size)
359 p_next = *
reinterpret_cast<char**
>(p_next);
364 p_next = ETL_NULLPTR;
369 ETL_ASSERT(
false, ETL_ERROR(pool_no_allocation));
378 void release_item(
char* p_value)
381 ETL_ASSERT(is_item_in_pool(p_value), ETL_ERROR(pool_object_not_in_pool));
383 if (p_next != ETL_NULLPTR)
386 *(uintptr_t*)p_value =
reinterpret_cast<uintptr_t
>(p_next);
391 *((uintptr_t*)p_value) = 0;
402 bool is_item_in_pool(
const char* p)
const
405 intptr_t distance = p - p_buffer;
406 bool is_within_range = (distance >= 0) && (distance <= intptr_t((Item_Size * Max_Size) - Item_Size));
409#if ETL_IS_DEBUG_BUILD
411 bool is_valid_address = ((distance % Item_Size) == 0);
413 bool is_valid_address =
true;
416 return is_within_range && is_valid_address;
426 uint32_t items_allocated;
427 uint32_t items_initialised;
429 const uint32_t Item_Size;
430 const uint32_t Max_Size;
435#if defined(ETL_POLYMORPHIC_POOL) || defined(ETL_POLYMORPHIC_CONTAINERS)
#define ETL_ASSERT(b, e)
Definition: error_handler.h:316
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition: exception.h:69
Definition: exception.h:47
size_t size() const
Returns the number of allocated items in the pool.
Definition: ipool.h:293
~ipool()
Destructor.
Definition: ipool.h:442
bool empty() const
Definition: ipool.h:302
void release_all()
Release all objects in the pool.
Definition: ipool.h:248
bool full() const
Definition: ipool.h:311
size_t max_size() const
Returns the maximum number of items in the pool.
Definition: ipool.h:269
T * allocate()
Definition: ipool.h:113
void release(const void *const p_object)
Definition: ipool.h:239
size_t capacity() const
Returns the maximum number of items in the pool.
Definition: ipool.h:277
ipool(char *p_buffer_, uint32_t item_size_, uint32_t max_size_)
Constructor.
Definition: ipool.h:321
bool is_in_pool(const void *const p_object) const
Definition: ipool.h:260
size_t available() const
Returns the number of free items in the pool.
Definition: ipool.h:285
T * create()
Definition: ipool.h:130
void destroy(const T *const p_object)
Definition: ipool.h:222
T * create(const T1 &value1)
Definition: ipool.h:148
bitset_ext
Definition: absolute.h:38