object_pool-inl.h 6.8 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
  if ((&obj < &objects_array_[0]) ||
    (&obj > &objects_array_[value_pool_size_ - 1])) {
88 89 90 91 92 93
    return false;
  } else {
    return true;
  }
}

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

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

    return ret_pointer;
  }
}

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

119 120
template<class Type, typename ValuePool, class ObjectAllocator>
ObjectPool<Type, ValuePool, ObjectAllocator>::ObjectPool(size_t capacity) :
121 122 123 124 125 126
  capacity_(capacity),
  value_pool_size_(
  ValuePool::GetMinimumElementCountForGuaranteedCapacity(capacity)),
  value_pool_(ReturningTrueIterator(0), ReturningTrueIterator(
  value_pool_size_)),
  objects_array_(object_allocator_.allocate(value_pool_size_)) {
127 128
}

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

134
  value_pool_.Free(true, index);
135 136
}

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

  return rawObject;
}

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

  return rawObject;
}

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

  return rawObject;
}

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

  return rawObject;
}

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

  return rawObject;
}

192 193
template<class Type, typename ValuePool, class ObjectAllocator>
ObjectPool<Type, ValuePool, ObjectAllocator>::~ObjectPool() {
194
  // Deallocate the objects
195
  object_allocator_.deallocate(objects_array_, value_pool_size_);
196 197 198 199 200
}
} // namespace containers
} // namespace embb

#endif  // EMBB_CONTAINERS_INTERNAL_OBJECT_POOL_INL_H_