object_pool-inl.h 7.68 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_CONTAINERS_INTERNAL_OBJECT_POOL_INL_H_
#define EMBB_CONTAINERS_INTERNAL_OBJECT_POOL_INL_H_

namespace embb {
namespace containers {
32 33
template<class Type, typename ValuePool, class ObjectAllocator>
ObjectPool<Type, ValuePool, ObjectAllocator>::
34 35 36 37 38
ReturningTrueIterator::ReturningTrueIterator(size_t count_value) :
count_value(count_value),
  ret_value(true)
{}

39 40
template<class Type, typename ValuePool, class ObjectAllocator>
typename ObjectPool<Type, ValuePool, ObjectAllocator>::
41
ReturningTrueIterator::self_type
42
ObjectPool<Type, ValuePool, ObjectAllocator>::
43 44 45 46 47 48
ReturningTrueIterator::operator++() {
  self_type i = *this;
  count_value++;
  return i;
}

49 50 51
template<class Type, typename ValuePool, class ObjectAllocator>
typename ObjectPool<Type, ValuePool, ObjectAllocator>::
ReturningTrueIterator::self_type ObjectPool<Type, ValuePool, ObjectAllocator>::
52
ReturningTrueIterator::operator++(int) {
53 54 55 56
  count_value++;
  return *this;
}

57 58 59
template<class Type, typename ValuePool, class ObjectAllocator>
typename ObjectPool<Type, ValuePool, ObjectAllocator>::
ReturningTrueIterator::reference ObjectPool<Type, ValuePool, ObjectAllocator>::
60 61 62 63
ReturningTrueIterator::operator*() {
  return ret_value;
}

64 65 66
template<class Type, typename ValuePool, class ObjectAllocator>
typename ObjectPool<Type, ValuePool, ObjectAllocator>::
ReturningTrueIterator::pointer ObjectPool<Type, ValuePool, ObjectAllocator>::
67 68 69 70
ReturningTrueIterator::operator->() {
  return &ret_value;
}

71 72
template<class Type, typename ValuePool, class ObjectAllocator>
bool ObjectPool<Type, ValuePool, ObjectAllocator>::
73 74 75 76
ReturningTrueIterator::operator==(const self_type& rhs) {
  return count_value == rhs.count_value;
}

77 78
template<class Type, typename ValuePool, class ObjectAllocator>
bool ObjectPool<Type, ValuePool, ObjectAllocator>::
79 80 81 82
ReturningTrueIterator::operator!=(const self_type& rhs) {
  return count_value != rhs.count_value;
}

83 84 85
template<class Type, typename ValuePool, class ObjectAllocator>
bool ObjectPool<Type, ValuePool, ObjectAllocator>::
IsContained(const Type &obj) const {
86 87 88 89 90 91 92
  if ((&obj < &objects[0]) || (&obj > &objects[capacity - 1])) {
    return false;
  } else {
    return true;
  }
}

93 94 95
template<class Type, typename ValuePool, class ObjectAllocator>
int ObjectPool<Type, ValuePool, ObjectAllocator>::
GetIndexOfObject(const Type &obj) const {
96 97 98 99
  assert(IsContained(obj));
  return(static_cast<int>(&obj - &objects[0]));
}

100 101
template<class Type, typename ValuePool, class ObjectAllocator>
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::AllocateRaw() {
102
  bool val;
103 104 105 106
  int allocated_index = p.Allocate(val);
  if (allocated_index == -1) {
    return NULL;
  } else {
107
    Type* ret_pointer = &(objects[allocated_index]);
108 109 110 111 112

    return ret_pointer;
  }
}

113 114
template<class Type, typename ValuePool, class ObjectAllocator>
size_t ObjectPool<Type, ValuePool, ObjectAllocator>::GetCapacity() {
115 116 117
  return capacity;
}

118 119
template<class Type, typename ValuePool, class ObjectAllocator>
ObjectPool<Type, ValuePool, ObjectAllocator>::ObjectPool(size_t capacity) :
120 121 122 123 124 125
capacity(capacity),
  p(ReturningTrueIterator(0), ReturningTrueIterator(capacity)) {
  // Allocate the objects (without construction, just get the memory)
  objects = objectAllocator.allocate(capacity);
}

126 127
template<class Type, typename ValuePool, class ObjectAllocator>
void ObjectPool<Type, ValuePool, ObjectAllocator>::Free(Type* obj) {
128
  int index = GetIndexOfObject(*obj);
129
  obj->~Type();
130

131
  p.Free(true, index);
132 133
}

134 135 136
template<class Type, typename ValuePool, class ObjectAllocator>
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate() {
  Type* rawObject = AllocateRaw();
137
  if (rawObject != NULL)
138
    new (rawObject)Type();
139 140 141 142

  return rawObject;
}

143
template<class Type, typename ValuePool, class ObjectAllocator>
144
template<typename Param1>
145
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate(
146
  Param1 const& param1) {
147
  Type* rawObject = AllocateRaw();
148
  if (rawObject != NULL)
149
    new (rawObject)Type(param1);
150 151 152 153

  return rawObject;
}

154
template<class Type, typename ValuePool, class ObjectAllocator>
155
template<typename Param1, typename Param2>
156
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate(
157
  Param1 const& param1, Param2 const& param2) {
158
  Type* rawObject = AllocateRaw();
159
  if (rawObject != NULL)
160
    new (rawObject)Type(param1, param2);
161 162 163 164

  return rawObject;
}

165
template<class Type, typename ValuePool, class ObjectAllocator>
166
template<typename Param1, typename Param2, typename Param3>
167
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate(
168 169
  Param1 const& param1, Param2 const& param2,
  Param3 const& param3) {
170
  Type* rawObject = AllocateRaw();
171
  if (rawObject != NULL)
172
    new (rawObject)Type(param1, param2, param3);
173 174 175 176

  return rawObject;
}

177
template<class Type, typename ValuePool, class ObjectAllocator>
178
template<typename Param1, typename Param2, typename Param3, typename Param4>
179
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate(
180 181
  Param1 const& param1, Param2 const& param2,
  Param3 const& param3, Param4 const& param4) {
182
  Type* rawObject = AllocateRaw();
183
  if (rawObject != NULL)
184
    new (rawObject)Type(param1, param2, param3, param4);
185 186 187 188

  return rawObject;
}

189
template<class Type, typename ValuePool, class ObjectAllocator>
190 191 192 193 194 195 196 197 198 199 200 201 202 203
template<typename Param1, typename Param2, typename Param3, typename Param4,
    typename Param5>
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate(
  Param1 const& param1, Param2 const& param2,
  Param3 const& param3, Param4 const& param4,
  Param5 const& param5) {
  Type* rawObject = AllocateRaw();
  if (rawObject != NULL)
    new (rawObject)Type(param1, param2, param3, param4, param5);

  return rawObject;
}

template<class Type, typename ValuePool, class ObjectAllocator>
204 205 206 207 208 209 210 211 212 213 214 215 216 217
template<typename Param1, typename Param2, typename Param3, typename Param4,
      typename Param5, typename Param6>
Type* ObjectPool<Type, ValuePool, ObjectAllocator>::Allocate(
  Param1 const& param1, Param2 const& param2,
  Param3 const& param3, Param4 const& param4,
  Param5 const& param5, Param6 const& param6) {
  Type* rawObject = AllocateRaw();
  if (rawObject != NULL)
    new (rawObject)Type(param1, param2, param3, param4, param5, param6);

  return rawObject;
}

template<class Type, typename ValuePool, class ObjectAllocator>
218
ObjectPool<Type, ValuePool, ObjectAllocator>::~ObjectPool() {
219 220 221 222 223 224 225
  // Deallocate the objects
  objectAllocator.deallocate(objects, capacity);
}
} // namespace containers
} // namespace embb

#endif  // EMBB_CONTAINERS_INTERNAL_OBJECT_POOL_INL_H_