Embedded Multicore Building Blocks V1.0.0
wait_free_array_value_pool.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_CONTAINERS_WAIT_FREE_ARRAY_VALUE_POOL_H_
28 #define EMBB_CONTAINERS_WAIT_FREE_ARRAY_VALUE_POOL_H_
29 
30 #include <embb/base/atomic.h>
31 #include <embb/base/memory_allocation.h>
32 #include <iterator>
33 #include <utility>
34 
35 namespace embb {
36 namespace containers {
145 template<typename Type,
146  Type Undefined,
149  private:
150  int size_;
151  embb::base::Atomic<Type>* pool_array_;
153  Allocator allocator_;
154 
155  // Prevent copy-construction
157 
158  // Prevent assignment
160 
161  public:
167  class Iterator : public std::iterator<
168  std::forward_iterator_tag, std::pair<int, Type> > {
169  private:
170  explicit Iterator(WaitFreeArrayValuePool * pool);
171  Iterator(WaitFreeArrayValuePool * pool, int index);
172  void Advance();
173 
174  WaitFreeArrayValuePool * pool_;
175  int index_;
176 
177  friend class WaitFreeArrayValuePool;
178 
179  public:
184  Iterator();
185 
190  Iterator(
191  Iterator const & other
192  );
193 
201  Iterator const & other
202  );
203 
210  Iterator & operator ++();
211 
218  Iterator operator ++(int);
219 
227  bool operator ==(
228  Iterator const & rhs
229  );
230 
238  bool operator !=(
239  Iterator const & rhs
240  );
241 
248  std::pair<int, Type> operator *();
249  };
250 
257  Iterator Begin();
258 
266  Iterator End();
267 
279  template<typename ForwardIterator>
281  ForwardIterator first,
284  ForwardIterator last
287  );
288 
298  size_t capacity);
300 
307 
317  int Allocate(
318  Type & element
321  );
322 
332  void Free(
333  Type element,
335  int index
337  );
338 };
339 } // namespace containers
340 } // namespace embb
341 
342 #include <embb/containers/internal/wait_free_array_value_pool-inl.h>
343 
344 #endif // EMBB_CONTAINERS_WAIT_FREE_ARRAY_VALUE_POOL_H_
Definition: lock_free_mpmc_queue.h:40
bool operator!=(Iterator const &rhs)
Compares two iterators for inequality.
static size_t GetMinimumElementCountForGuaranteedCapacity(size_t capacity)
Due to concurrency effects, a pool might provide less elements than managed by it.
Forward iterator to iterate over the allocated elements of the pool.
Definition: wait_free_array_value_pool.h:167
Allocator according to the C++ standard.
Definition: memory_allocation.h:525
Iterator()
Constructs an invalid iterator.
~WaitFreeArrayValuePool()
Destructs the pool.
Iterator & operator=(Iterator const &other)
Copies an iterator.
Wait-free value pool using array construction.
Definition: wait_free_array_value_pool.h:148
int Allocate(Type &element)
Allocates an element from the pool.
bool operator==(Iterator const &rhs)
Compares two iterators for equality.
Iterator & operator++()
Pre-increments an iterator.
void Free(Type element, int index)
Returns an element to the pool.
std::pair< int, Type > operator*()
Dereferences the iterator.
Iterator Begin()
Gets a forward iterator to the first allocated element in the pool.
Iterator End()
Gets a forward iterator pointing after the last allocated element in the pool.