Skip to content

Commit

Permalink
Use thread-safe cache in thread_local_caching_allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pansysk75 committed Sep 7, 2024
1 parent d9b5f6c commit 5ed299c
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

#include <hpx/config.hpp>
#include <hpx/allocator_support/config/defines.hpp>
#include <hpx/concurrency/stack.hpp>

#include <cstddef>
#include <memory>
#include <new>
#include <stack>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -56,6 +56,7 @@ namespace hpx::util {
explicit allocated_cache(Allocator const& a) noexcept(
noexcept(std::is_nothrow_copy_constructible_v<Allocator>))
: alloc(a)
, data(0)
{
}

Expand All @@ -82,8 +83,9 @@ namespace hpx::util {
}
else
{
p = data.top().first;
data.pop();
std::pair<T*, size_type> pair;
data.pop(pair);
p = pair.first;
}

++allocated;
Expand All @@ -104,16 +106,15 @@ namespace hpx::util {
private:
void clear_cache() noexcept
{
while (!data.empty())
std::pair<T*, size_type> p;
while (data.pop(p))
{
traits::deallocate(
alloc, data.top().first, data.top().second);
data.pop();
traits::deallocate(alloc, p.first, p.second);
}
}

HPX_NO_UNIQUE_ADDRESS Allocator alloc;
std::stack<std::pair<T*, size_type>> data;
hpx::lockfree::stack<std::pair<T*, size_type>> data;
std::size_t allocated = 0;
std::size_t deallocated = 0;
};
Expand Down

0 comments on commit 5ed299c

Please sign in to comment.