17 #ifndef IOX_UTILS_OBJECTPOOL_OBJECTPOOL_HPP
18 #define IOX_UTILS_OBJECTPOOL_OBJECTPOOL_HPP
34 template <
typename T,
int CAPACITY = 1>
39 static constexpr
int NO_INDEX = -1;
42 static constexpr
size_t CHUNKSIZE =
sizeof(T);
43 using Chunk =
char[CHUNKSIZE];
44 using Container = Chunk[CAPACITY];
49 Index_t m_freeIndex{0};
57 bool wasConstructed{
false};
61 alignas(T) Container m_values;
62 CellInfo m_cellInfo[CAPACITY];
82 return *(pool->m_cellInfo[index].data);
90 if (index >= CAPACITY)
94 if ((pool->m_cellInfo[index]).isValid)
96 return pool->m_cellInfo[index].data;
104 for (Index_t i = index + 1; i < CAPACITY; ++i)
106 if (pool->m_cellInfo[i].isValid ==
true)
120 for (Index_t i = index + 1; i < CAPACITY; ++i)
122 if (pool->m_cellInfo[i].isValid ==
true)
132 bool operator!=(
const Iterator& other)
const
134 return (this->index != other.index || this->pool != other.pool);
137 bool operator==(
const Iterator& other)
const
139 return (this->index == other.index && this->pool == other.pool);
145 for (Index_t i = 0; i < CAPACITY; ++i)
147 if (m_cellInfo[i].isValid ==
true)
152 return Iterator(CAPACITY, *
this);
157 return Iterator(CAPACITY, *
this);
161 : m_first(reinterpret_cast<char*>(&(m_values[0])))
162 , m_last(reinterpret_cast<char*>(&(m_values[CAPACITY - 1])))
170 for (Index_t i = 0; i < CAPACITY; ++i)
172 if (m_cellInfo[i].isValid && m_cellInfo[i].wasConstructed)
174 m_cellInfo[i].data->~T();
183 auto index = nextFree();
188 m_cellInfo[m_freeIndex].isValid =
true;
189 m_cellInfo[m_freeIndex].wasConstructed =
false;
198 auto index = nextFree();
203 m_cellInfo[index].data =
new (&m_values[index]) T;
204 m_cellInfo[m_freeIndex].isValid =
true;
205 m_cellInfo[m_freeIndex].wasConstructed =
true;
212 template <
typename... Args>
213 Index_t construct(Args&&... args)
215 auto index = nextFree();
220 m_cellInfo[index].data =
new (&m_values[index]) T(std::forward<Args>(args)...);
221 m_cellInfo[m_freeIndex].isValid =
true;
222 m_cellInfo[m_freeIndex].wasConstructed =
true;
229 Index_t add(
const T& element)
231 auto index = nextFree();
236 auto& cellInfo = m_cellInfo[m_freeIndex];
237 cellInfo.data =
new (m_values[m_freeIndex]) T(element);
238 cellInfo.isValid =
true;
239 cellInfo.wasConstructed =
true;
246 void remove(Index_t index,
bool destruct =
false)
248 if (m_cellInfo[index].isValid)
252 m_cellInfo[index].data->~T();
254 m_cellInfo[index].isValid =
false;
260 T& operator[](Index_t index)
262 return *(m_cellInfo[index].data);
265 Iterator iterator(Index_t index)
267 if (m_cellInfo[index].isValid)
269 return Iterator(index, *
this);
279 size_t capacity()
const
289 auto index = reserve();
290 if (index == NO_INDEX)
300 return reinterpret_cast<T*
>(m_values[index]);
306 auto index = construct();
307 if (index == NO_INDEX)
315 template <
typename... Args>
316 T* create(Args&&... args)
318 auto index = construct(std::forward<Args>(args)...);
319 if (index == NO_INDEX)
327 void free(T* ptr,
bool destruct)
332 remove(index, destruct);
344 remove(index, m_cellInfo[index].wasConstructed);
348 T* insert(
const T& element)
350 auto index = add(element);
364 T* get(Index_t index)
366 return (m_cellInfo[index].isValid) ? m_cellInfo[index].data :
nullptr;
372 if (index != NO_INDEX)
374 return (m_cellInfo[index].isValid) ? m_cellInfo[index].data :
nullptr;
379 Iterator iterator(T* ptr)
384 return iterator(index);
392 char* p =
reinterpret_cast<char*
>(ptr);
393 if (p < m_first || p > m_last)
397 auto delta = p - m_first;
398 if (
static_cast<uint64_t
>(delta) %
sizeof(T) != 0)
404 auto index =
static_cast<Index_t
>(
static_cast<uint64_t
>(delta) /
sizeof(T));
405 if (m_cellInfo[index].isValid && m_cellInfo[index].data)
407 if (m_cellInfo[index].data == ptr)
421 T* indexToPointer(Index_t index)
423 return m_cellInfo[index].data;
430 if (m_size >= CAPACITY)
433 for (; m_cellInfo[m_freeIndex].isValid; m_freeIndex = (m_freeIndex + 1) % CAPACITY)
Definition: objectpool.hpp:68
Definition: objectpool.hpp:36
Index_t pointerToIndex(T *ptr)
Definition: objectpool.hpp:389
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28