Embedded Multicore Building Blocks V1.0.0
object_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_OBJECT_POOL_H_
28 #define EMBB_CONTAINERS_OBJECT_POOL_H_
29 
30 #include <embb/base/atomic.h>
31 #include <embb/containers/wait_free_array_value_pool.h>
32 
33 #include <limits>
34 #include <stdexcept>
35 
36 namespace embb {
37 namespace containers {
55 template<class Type,
56  typename ValuePool =
58  class ObjectAllocator = embb::base::Allocator<Type> >
59 class ObjectPool {
60  private:
64  ObjectAllocator object_allocator_;
65 
69  size_t capacity_;
70 
76  size_t value_pool_size_;
77 
81  ValuePool value_pool_;
82 
86  Type* objects_array_;
87 
93  class ReturningTrueIterator {
94  public:
95  typedef ReturningTrueIterator self_type;
96  typedef bool value_type;
97  typedef bool& reference;
98  typedef bool* pointer;
99  typedef std::forward_iterator_tag iterator_category;
100  typedef int difference_type;
101  explicit ReturningTrueIterator(size_t count_value);
102  self_type operator++();
103  self_type operator++(int);
104  reference operator*();
105  pointer operator->();
106  bool operator==(const self_type& rhs);
107  bool operator!=(const self_type& rhs);
108 
109  private:
110  size_t count_value;
111  bool ret_value;
112  };
113 
114  bool IsContained(const Type &obj) const;
115  int GetIndexOfObject(const Type &obj) const;
116  Type* AllocateRaw();
117 
118  public:
126  ObjectPool(
127  size_t capacity
129  );
130 
136  ~ObjectPool();
137 
145  size_t GetCapacity();
146 
155  void Free(
156  Type* obj
158  );
159 
160 #ifdef DOXYGEN
161 
171  Type* Allocate(...);
172 #else
173  Type* Allocate();
174 
175  template<typename Param1>
176  Type* Allocate(Param1 const& param1);
177 
178  template<typename Param1, typename Param2>
179  Type* Allocate(Param1 const& param1, Param2 const& param2);
180 
181  template<typename Param1, typename Param2, typename Param3>
182  Type* Allocate(Param1 const& param1, Param2 const& param2,
183  Param3 const& param3);
184 
185  template<typename Param1, typename Param2, typename Param3, typename Param4>
186  Type* Allocate(Param1 const& param1, Param2 const& param2,
187  Param3 const& param3, Param4 const& param4);
188 
189 #endif
190 };
191 } // namespace containers
192 } // namespace embb
193 
194 #include <embb/containers/internal/object_pool-inl.h>
195 
196 #endif // EMBB_CONTAINERS_OBJECT_POOL_H_
Definition: lock_free_mpmc_queue.h:40
Type * Allocate(...)
Allocates an element from the pool.
void Free(Type *obj)
Returns an element to the pool.
ObjectPool(size_t capacity)
Constructs an object pool with capacity capacity.
Allocator according to the C++ standard.
Definition: memory_allocation.h:525
Pool for thread-safe management of arbitrary objects.
Definition: object_pool.h:59
Wait-free value pool using array construction.
Definition: wait_free_array_value_pool.h:148
size_t GetCapacity()
Returns the capacity of the pool.
bool operator!=(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (inequality).
Definition: duration.h:183
~ObjectPool()
Destructs the pool.
bool operator==(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (equality).
Definition: duration.h:166