Skip to content

Commit

Permalink
fixes and sanity check
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h committed Jan 7, 2025
1 parent d8d3dc3 commit 98284e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
24 changes: 14 additions & 10 deletions include/dice/template-library/pool_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <boost/pool/pool.hpp>

#include <algorithm>
#include <array>
#include <memory>
#include <cstddef>
#include <type_traits>

namespace dice::template_library {

Expand Down Expand Up @@ -55,22 +57,23 @@ namespace dice::template_library {
pool<bucket_sizes...> *pool_;

public:
explicit constexpr pool_allocator(pool<bucket_sizes...> &p) noexcept
: pool_{&p} {
explicit constexpr pool_allocator(pool<bucket_sizes...> &parent_pool) noexcept
: pool_{&parent_pool} {
}

constexpr pool_allocator(pool_allocator const &other) noexcept = default;
constexpr pool_allocator(pool_allocator &&other) noexcept = default;
constexpr pool_allocator &operator=(pool_allocator const &other) noexcept = default;
constexpr pool_allocator &operator=(pool_allocator &&other) noexcept = default;
constexpr ~pool_allocator() noexcept = default;

template<typename U>
constexpr pool_allocator(pool_allocator<U, bucket_sizes...> const &other) noexcept
: pool_{other.state_} {
}

constexpr pointer allocate(size_t n) {
return pool_->allocate(sizeof(T) * n);
return static_cast<pointer>(pool_->allocate(sizeof(T) * n));
}

constexpr void deallocate(pointer ptr, size_t n) {
Expand All @@ -81,9 +84,9 @@ namespace dice::template_library {
return pool_allocator{*pool_};
}

friend constexpr void swap(pool_allocator &a, pool_allocator &b) noexcept {
friend constexpr void swap(pool_allocator &lhs, pool_allocator &rhs) noexcept {
using std::swap;
swap(a.pool_, b.pool_);
swap(lhs.pool_, rhs.pool_);
}

bool operator==(pool_allocator const &other) const noexcept = default;
Expand All @@ -108,7 +111,7 @@ namespace dice::template_library {

template<size_t ix, size_t bucket_size, size_t ...rest>
void *allocate_impl(size_t n_bytes) {
if (n_bytes < bucket_size) {
if (n_bytes <= bucket_size) {
// fits into bucket

void *ptr = pools_[ix].malloc();
Expand All @@ -129,16 +132,17 @@ namespace dice::template_library {

template<size_t ix, size_t bucket_size, size_t ...rest>
void deallocate_impl(void *data, size_t n_bytes) {
if (n_bytes < bucket_size) {
if (n_bytes <= bucket_size) {
// fits into bucket
pools_[ix].free(data);
return;
}

if constexpr (sizeof...(rest) > 0) {
return deallocate_impl<ix + 1, rest...>(data, n_bytes);
deallocate_impl<ix + 1, rest...>(data, n_bytes);
} else {
// does not fit into any bucket, must have been allocated via new[]
return delete[] static_cast<char *>(data);
delete[] static_cast<char *>(data);
}
}

Expand Down
16 changes: 15 additions & 1 deletion tests/tests_pool_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@

#include <dice/template-library/pool_allocator.hpp>

#include <array>
#include <cstddef>
#include <cstdint>

TEST_SUITE("pool allocator") {
TEST_CASE("sanity check") {
dice::template_library::pool<8, 16> pool;
auto alloc1 = pool.get_allocator<uint64_t>(); // first pool
auto alloc2 = pool.get_allocator<std::array<uint64_t, 2>>(); // second pool
auto alloc3 = pool.get_allocator<std::array<uint64_t, 4>>(); // fallback to new

for (size_t ix = 0; ix < 1'000'000; ++ix) {
auto *ptr1 = alloc1.allocate(1);
auto *ptr2 = alloc2.allocate(1);
auto *ptr3 = alloc3.allocate(1);


alloc2.deallocate(ptr2, 1);
alloc3.deallocate(ptr3, 1);
alloc1.deallocate(ptr1, 1);
}
}
}

0 comments on commit 98284e6

Please sign in to comment.