Skip to content

Commit

Permalink
Add arena::get_capacity().
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Aug 4, 2024
1 parent cde27c7 commit d522d05
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/bitcoin/system/arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ class arena
return do_is_equal(other);
}

/// Get the remaining area memory capacity (additional to std::pmr).
NODISCARD size_t get_capacity() const NOEXCEPT
{
return do_get_capacity();
}

private:
virtual void* do_allocate(size_t bytes, size_t align) THROWS = 0;
virtual void do_deallocate(void* ptr, size_t bytes, size_t align) NOEXCEPT = 0;
virtual bool do_is_equal(const arena& other) const NOEXCEPT = 0;
virtual size_t do_get_capacity() const NOEXCEPT = 0;
};

/// Left can deallocate memory allocated by right and vice versa.
Expand Down Expand Up @@ -84,6 +91,7 @@ class BC_API default_arena final
void* do_allocate(size_t bytes, size_t align) THROWS override;
void do_deallocate(void* ptr, size_t bytes, size_t align) NOEXCEPT override;
bool do_is_equal(const arena& other) const NOEXCEPT override;
size_t do_get_capacity() const NOEXCEPT override;
};

} // namespace libbitcoin
Expand Down
16 changes: 12 additions & 4 deletions src/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
#include <bitcoin/system/arena.hpp>

#include <bitcoin/system/constants.hpp>

namespace libbitcoin {

BC_PUSH_WARNING(NO_NEW_OR_DELETE)
Expand All @@ -27,6 +29,14 @@ bool operator==(const arena& left, const arena& right) NOEXCEPT
return &left == &right || left.is_equal(right);
}

// static
// use bc::default_arena::get() vs. std::pmr::get_default_resource()
arena* default_arena::get() NOEXCEPT
{
static default_arena resource{};
return &resource;
}

void* default_arena::do_allocate(size_t bytes, size_t) THROWS
{
////if (align > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
Expand All @@ -49,11 +59,9 @@ bool default_arena::do_is_equal(const arena& other) const NOEXCEPT
return &other == this;
}

// use bc::default_arena::get() vs. std::pmr::get_default_resource()
arena* default_arena::get() NOEXCEPT
size_t default_arena::do_get_capacity() const NOEXCEPT
{
static default_arena resource{};
return &resource;
return max_size_t;
}

BC_POP_WARNING()
Expand Down
10 changes: 10 additions & 0 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ class reporting_arena
return &other == this;
}

size_t do_get_capacity() const NOEXCEPT override
{
return {};
}

void report(void* ptr, size_t bytes, bool allocate) const NOEXCEPT
{
if constexpr (Report)
Expand Down Expand Up @@ -204,6 +209,11 @@ class mock_arena
do_is_equal_address = &other;
return false;
}

size_t do_get_capacity() const NOEXCEPT override
{
return {};
}
};

template <bool Report = false>
Expand Down

0 comments on commit d522d05

Please sign in to comment.