Commit e94b884a by Christian Kern

fix for jira ticket #525

parent 56b4e070
...@@ -213,11 +213,28 @@ LockFreeTreeValuePool(ForwardIterator first, ForwardIterator last) { ...@@ -213,11 +213,28 @@ LockFreeTreeValuePool(ForwardIterator first, ForwardIterator last) {
// Size of binary tree without the leaves // Size of binary tree without the leaves
tree_size = size - 1; tree_size = size - 1;
// make sure, signed values are not negative
assert(tree_size >= 0);
assert(real_size >= 0);
size_t tree_size_unsigned = static_cast<size_t>(tree_size);
size_t real_size_unsigned = static_cast<size_t>(real_size);
// Pool stores elements of type T // Pool stores elements of type T
pool = poolAllocator.allocate(static_cast<size_t>(real_size)); pool = poolAllocator.allocate(real_size_unsigned);
// invoke inplace new for each pool element
for (size_t i = 0; i != real_size_unsigned; ++i) {
new (&pool[i]) embb::base::Atomic<Type>();
}
// Tree holds the counter of not allocated elements // Tree holds the counter of not allocated elements
tree = treeAllocator.allocate(static_cast<size_t>(tree_size)); tree = treeAllocator.allocate(tree_size_unsigned);
// invoke inplace new for each tree element
for (size_t i = 0; i != tree_size_unsigned; ++i) {
new (&tree[i]) embb::base::Atomic<int>();
}
int i = 0; int i = 0;
...@@ -234,8 +251,22 @@ template<typename Type, Type Undefined, class PoolAllocator, ...@@ -234,8 +251,22 @@ template<typename Type, Type Undefined, class PoolAllocator,
class TreeAllocator > class TreeAllocator >
LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>:: LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
~LockFreeTreeValuePool() { ~LockFreeTreeValuePool() {
poolAllocator.deallocate(pool, static_cast<size_t>(real_size)); size_t tree_size_unsigned = static_cast<size_t>(tree_size);
treeAllocator.deallocate(tree, static_cast<size_t>(tree_size)); size_t real_size_unsigned = static_cast<size_t>(real_size);
poolAllocator.deallocate(pool, real_size_unsigned);
// invoke destructor for each pool element
for (size_t i = 0; i != real_size_unsigned; ++i) {
pool[i].~Atomic();
}
treeAllocator.deallocate(tree, tree_size_unsigned);
// invoke destructor for each tree element
for (size_t i = 0; i != tree_size_unsigned; ++i) {
tree[i].~Atomic();
}
} }
} // namespace containers } // namespace containers
......
...@@ -66,9 +66,17 @@ WaitFreeArrayValuePool(ForwardIterator first, ForwardIterator last) { ...@@ -66,9 +66,17 @@ WaitFreeArrayValuePool(ForwardIterator first, ForwardIterator last) {
size = static_cast<int>(dist); size = static_cast<int>(dist);
// conversion may result in negative number. check!
assert(size >= 0);
// Use the allocator to allocate an array of size dist // Use the allocator to allocate an array of size dist
pool = allocator.allocate(dist); pool = allocator.allocate(dist);
// invoke inplace new for each pool element
for ( size_t i = 0; i != dist; ++i ) {
new (&pool[i]) embb::base::Atomic<Type>();
}
int i = 0; int i = 0;
// Store the elements of the range // Store the elements of the range
...@@ -79,6 +87,12 @@ WaitFreeArrayValuePool(ForwardIterator first, ForwardIterator last) { ...@@ -79,6 +87,12 @@ WaitFreeArrayValuePool(ForwardIterator first, ForwardIterator last) {
template<typename Type, Type Undefined, class Allocator > template<typename Type, Type Undefined, class Allocator >
WaitFreeArrayValuePool<Type, Undefined, Allocator>::~WaitFreeArrayValuePool() { WaitFreeArrayValuePool<Type, Undefined, Allocator>::~WaitFreeArrayValuePool() {
// invoke destructor for each pool element
for (int i = 0; i != size; ++i) {
pool[i].~Atomic();
}
// free memory
allocator.deallocate(pool, (size_t)size); allocator.deallocate(pool, (size_t)size);
} }
} // namespace containers } // namespace containers
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment