66 class pool_no_allocation :
public pool_exception
70 explicit pool_no_allocation(string_type file_name_, numeric_type line_number_)
71 : pool_exception(ETL_ERROR_TEXT(
"pool:allocation", ETL_POOL_FILE_ID
"A"), file_name_, line_number_)
81 class pool_object_not_in_pool :
public pool_exception
85 pool_object_not_in_pool(string_type file_name_, numeric_type line_number_)
86 : pool_exception(ETL_ERROR_TEXT(
"pool:not in pool", ETL_POOL_FILE_ID
"B"), file_name_, line_number_)
96 class pool_element_size :
public pool_exception
100 pool_element_size(string_type file_name_, numeric_type line_number_)
101 : pool_exception(ETL_ERROR_TEXT(
"pool:element size", ETL_POOL_FILE_ID
"C"), file_name_, line_number_)
113 typedef size_t size_type;
121 const char* buffer_end()
const
123 return p_buffer + Item_Size * items_initialised;
132 bool is_pointing_into_pool_or_end_or_nullptr(
const char* address)
const
134 return address == ETL_NULLPTR || (p_buffer <= address && address <= buffer_end());
140 bool is_in_free_list(
const char* address)
const
142 const char* i = p_next;
143 while (i != ETL_NULLPTR)
149 i = *
reinterpret_cast<const char* const*
>(i);
157 template <
bool is_const>
164 typedef typename etl::conditional<is_const, const char*, char*>::type value_type;
165 typedef typename etl::conditional<is_const, const char*&, char*&>::type reference;
166 typedef typename etl::conditional<is_const, const char**, char**>::type pointer;
167 typedef ptrdiff_t difference_type;
168 typedef ETL_OR_STD::forward_iterator_tag iterator_category;
169 typedef typename etl::conditional<is_const, const void*, void*>::type void_type;
170 typedef typename etl::conditional<is_const, const ipool, ipool>::type pool_type;
171 typedef typename etl::conditional<is_const, const char* const*, char**>::type pointer_type;
174 ipool_iterator(
const ipool_iterator& other)
175 : p_current(other.p_current)
176 , p_pool(other.p_pool)
182 ipool_iterator& operator++()
184 p_current = p_current + p_pool->Item_Size;
190 ipool_iterator operator++(
int)
192 ipool_iterator temp(*
this);
193 p_current = p_current + p_pool->Item_Size;
199 ipool_iterator& operator=(
const ipool_iterator& other)
201 p_current = other.p_current;
202 p_pool = other.p_pool;
207 void_type operator*()
const
213 template <
typename T>
216 return *
reinterpret_cast<T*
>(p_current);
220 friend bool operator==(
const ipool_iterator& lhs,
const ipool_iterator& rhs)
222 return lhs.p_current == rhs.p_current;
226 friend bool operator!=(
const ipool_iterator& lhs,
const ipool_iterator& rhs)
228 return !(lhs == rhs);
237 void find_allocated()
239 while (p_current < p_pool->buffer_end())
241 value_type value = *
reinterpret_cast<pointer_type
>(p_current);
242 if (!p_pool->is_pointing_into_pool_or_end_or_nullptr(value))
246 if (!p_pool->is_in_free_list(p_current))
250 p_current += p_pool->Item_Size;
257 ipool_iterator(value_type p, pool_type* pool_)
264 value_type p_current;
268 template <
bool is_const>
274 class const_iterator :
public ipool_iterator<true>
278 const_iterator(
const ipool_iterator& other)
279 : ipool_iterator(other)
282 const_iterator(
const ipool_iterator<false>& other)
283 : ipool_iterator(other.p_current, other.p_pool)
286 const_iterator(value_type p, pool_type* pool_)
287 : ipool_iterator<true>(p, pool_)
295 return iterator(p_buffer,
this);
301 return iterator(p_buffer + Item_Size * items_initialised,
this);
305 const_iterator begin()
const
307 return const_iterator(p_buffer,
this);
313 return const_iterator(p_buffer + Item_Size * items_initialised,
this);
325 return const_iterator(p_buffer + Item_Size * items_initialised,
this);
333 template <
typename T>
336 if (
sizeof(T) > Item_Size)
341 return reinterpret_cast<T*
>(allocate_item());
344#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
350 template <
typename T>
369 template <
typename T,
typename T1>
382 template <
typename T,
typename T1,
typename T2>
383 T*
create(
const T1& value1,
const T2& value2)
389 ::new (p) T(value1, value2);
395 template <
typename T,
typename T1,
typename T2,
typename T3>
396 T*
create(
const T1& value1,
const T2& value2,
const T3& value3)
402 ::new (p) T(value1, value2, value3);
408 template <
typename T,
typename T1,
typename T2,
typename T3,
typename T4>
409 T*
create(
const T1& value1,
const T2& value2,
const T3& value3,
const T4& value4)
415 ::new (p) T(value1, value2, value3, value4);
424 template <
typename T,
typename... Args>
431 ::new (p) T(etl::forward<Args>(args)...);
443 template <
typename T>
446 if (
sizeof(T) > Item_Size)
463 const uintptr_t p = uintptr_t(p_object);
464 release_item((
char*)p);
473 items_initialised = 0;
484 const uintptr_t p = uintptr_t(p_object);
485 return is_item_in_pool((
const char*)p);
517 return Max_Size - items_allocated;
525 return items_allocated;
534 return items_allocated == 0;
543 return items_allocated == Max_Size;
551 ipool(
char* p_buffer_, uint32_t item_size_, uint32_t max_size_)
552 : p_buffer(p_buffer_)
555 , items_initialised(0)
556 , Item_Size(item_size_)
557 , Max_Size(max_size_)
563 static ETL_CONSTANT uintptr_t invalid_item_ptr = 1;
568 char* allocate_item()
570 char* p_value = ETL_NULLPTR;
573 if (items_allocated < Max_Size)
576 if (items_initialised < Max_Size)
578 char* p = p_buffer + (items_initialised * Item_Size);
579 char* np = p + Item_Size;
580 *
reinterpret_cast<char**
>(p) = np;
588 if (items_allocated < Max_Size)
591 p_next = *
reinterpret_cast<char**
>(p_next);
596 p_next = ETL_NULLPTR;
602 *
reinterpret_cast<uintptr_t*
>(p_value) = invalid_item_ptr;
606 ETL_ASSERT(
false, ETL_ERROR(pool_no_allocation));
615 void release_item(
char* p_value)
618 ETL_ASSERT(is_item_in_pool(p_value), ETL_ERROR(pool_object_not_in_pool));
620 if (items_allocated > 0)
623 *(uintptr_t*)p_value =
reinterpret_cast<uintptr_t
>(p_next);
631 ETL_ASSERT_FAIL(ETL_ERROR(pool_no_allocation));
638 bool is_item_in_pool(
const char* p)
const
641 intptr_t distance = p - p_buffer;
642 bool is_within_range = (distance >= 0) && (distance <= intptr_t((Item_Size * Max_Size) - Item_Size));
646#if ETL_IS_DEBUG_BUILD
648 bool is_valid_address = ((
static_cast<size_t>(distance) % Item_Size) == 0);
650 bool is_valid_address =
true;
653 return is_within_range && is_valid_address;
663 uint32_t items_allocated;
664 uint32_t items_initialised;
666 const uint32_t Item_Size;
667 const uint32_t Max_Size;
672#if defined(ETL_POLYMORPHIC_POOL) || defined(ETL_POLYMORPHIC_CONTAINERS)
ETL_CONSTEXPR14 bool operator==(const etl::to_arithmetic_result< T > &lhs, const etl::to_arithmetic_result< T > &rhs)
Equality test for etl::to_arithmetic_result.
Definition to_arithmetic.h:903
ETL_CONSTEXPR14 bool operator!=(const etl::to_arithmetic_result< T > &lhs, const etl::to_arithmetic_result< T > &rhs)
Inequality test for etl::to_arithmetic_result.
Definition to_arithmetic.h:937