Skip to content

Commit

Permalink
Style, comments, throw on over-requested block arenas.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Aug 4, 2024
1 parent 3aebc3b commit f13518a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 4 additions & 5 deletions include/bitcoin/node/block_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class BCN_API block_memory final
public:
DELETE_COPY_MOVE_DESTRUCT(block_memory);

/// Store the size for each thread's arena initilization.
/// Default allocate each arena to preclude allcation and locking.
block_memory(size_t size, size_t threads) NOEXCEPT;
block_memory(size_t bytes, size_t threads) NOEXCEPT;

/// Each thread obtains an arena of the same size.
arena* get_arena() NOEXCEPT override;
Expand All @@ -47,13 +46,13 @@ class BCN_API block_memory final
retainer::ptr get_retainer() NOEXCEPT override;

protected:
block_arena* get_block_arena() NOEXCEPT;
block_arena* get_block_arena() THROWS;

private:
// These are thread safe.
// This is thread safe.
std::atomic_size_t count_;

// This is protected by thread_local member.
// This is protected by constructor init and thread_local indexation.
std::vector<block_arena> arenas_;
};

Expand Down
11 changes: 7 additions & 4 deletions src/block_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
namespace libbitcoin {
namespace node {

block_memory::block_memory(size_t size, size_t threads) NOEXCEPT
block_memory::block_memory(size_t bytes, size_t threads) NOEXCEPT
: count_{}, arenas_{}
{
arenas_.reserve(threads);
for (auto index = zero; index < threads; ++index)
arenas_.emplace_back(size);
arenas_.emplace_back(bytes);
}

arena* block_memory::get_arena() NOEXCEPT
{
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
return get_block_arena();
BC_POP_WARNING()
}

retainer::ptr block_memory::get_retainer() NOEXCEPT
Expand All @@ -47,12 +49,13 @@ retainer::ptr block_memory::get_retainer() NOEXCEPT

// protected

block_arena* block_memory::get_block_arena() NOEXCEPT
block_arena* block_memory::get_block_arena() THROWS
{
static thread_local auto index = count_.fetch_add(one);

// More threads are requesting an arena than specified at construct.
if (index >= arenas_.size())
return nullptr;
throw allocation_exception();

return &arenas_.at(index);
}
Expand Down

0 comments on commit f13518a

Please sign in to comment.