Skip to content

Commit

Permalink
Merge pull request #424 from evoskuil/master
Browse files Browse the repository at this point in the history
Manage block memory allocation wrapping in linear allocator.
  • Loading branch information
evoskuil authored Aug 5, 2024
2 parents 6f75278 + b3a8d36 commit 68ef76f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 3 additions & 3 deletions include/bitcoin/network/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BCT_API memory
virtual arena* get_arena() NOEXCEPT = 0;

/// Get memory retainer.
virtual retainer::ptr get_retainer() NOEXCEPT = 0;
virtual retainer::ptr get_retainer(size_t allocation=zero) NOEXCEPT = 0;
};

/// Default tracked memory implementation (untracked).
Expand All @@ -49,8 +49,8 @@ class BCT_API default_memory final
/// Get memory arena (system default).
arena* get_arena() NOEXCEPT override;

/// Get memory retainer (empty pointer).
retainer::ptr get_retainer() NOEXCEPT override;
/// Get memory retainer (empty pointer, allocation unused).
retainer::ptr get_retainer(size_t=zero) NOEXCEPT override;
};

} // namespace network
Expand Down
2 changes: 1 addition & 1 deletion src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ arena* default_memory::get_arena() NOEXCEPT
return default_arena::get();
}

retainer::ptr default_memory::get_retainer() NOEXCEPT
retainer::ptr default_memory::get_retainer(size_t) NOEXCEPT
{
return {};
}
Expand Down
22 changes: 21 additions & 1 deletion src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace network {
namespace messages {

using namespace system;

// Measured through block 550,000 - assumes consistent platform sizing.
constexpr auto maximal_block = 11'904'912_size;

const std::string block::command = "block";
const identifier block::id = identifier::block;
Expand All @@ -47,17 +50,34 @@ typename block::cptr block::deserialize(uint32_t version,
}

// static
// TODO: Move cached hashes to arena.
typename block::cptr block::deserialize(memory& memory, uint32_t version,
const system::data_chunk& data, bool witness) NOEXCEPT
{
const auto arena = memory.get_arena();
if (arena == nullptr)
return nullptr;

// Must have at least maximal_block available to prevent block overflow.
////uint8_t* begin = nullptr;
const auto capacity = arena->get_capacity();
if (capacity < maximal_block)
arena->deallocate(arena->allocate(capacity), capacity);
////else
//// begin = system::pointer_cast<uint8_t>(arena->allocate(zero));

system::istream source{ data };
system::byte_reader reader{ source, memory.get_arena() };
system::byte_reader reader{ source, arena };

// message, block and block_ptr are not allocated by reader's arena.
// chain::block consists only of three pointers and two integral values.
const auto message = to_shared(deserialize(version, reader, witness));
if (!reader)
return nullptr;

////const auto end = system::pointer_cast<uint8_t>(arena->allocate(zero));
////const auto size = system::limit<size_t>(std::distance(begin, end));

// Cache header hash.
constexpr auto header_size = chain::header::serialized_size();
const auto& header = message->block_ptr->header();
Expand Down

0 comments on commit 68ef76f

Please sign in to comment.