Skip to content

Commit

Permalink
Merge pull request #428 from evoskuil/master
Browse files Browse the repository at this point in the history
Custom allocate block object.
  • Loading branch information
evoskuil authored Aug 18, 2024
2 parents 703a6e2 + e0474f4 commit a13df3f
Showing 1 changed file with 14 additions and 54 deletions.
68 changes: 14 additions & 54 deletions src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,73 +94,33 @@ typename block::cptr block::deserialize(uint32_t version,

// static
// WARNING: linear arena must allocate a maximal block and all contents.
// WARNING: chain::block::cptr destruct frees all contained shared_ptr objects.
// WARNING: the block_allocator block becomes invalidated once this method is
// WARNING: called subsequently on the same thread.
typename block::cptr block::deserialize(arena& arena, uint32_t version,
const data_chunk& data, bool witness) NOEXCEPT
{
////if (version < version_minimum || version > version_maximum)
//// nullptr;
if (version < version_minimum || version > version_maximum)
return nullptr;

// TODO: arena::initialize().
// Returns current arena address (next allocation point).
// This implies a linear allocator, otherwise nullptr returned.
const auto begin = pointer_cast<uint8_t>(arena.initialize());
if (is_null(begin))
return nullptr;

istream source{ data };
byte_reader reader{ source, &arena };

// block and all of its parts are allocated at the start of arena.
////auto allocator = reader.get_allocator();
////const auto raw = allocator.new_object<chain::block>(reader, witness);
////if (is_null(raw) || !reader)
//// return nullptr;
const auto message = to_shared(deserialize(version, reader, witness));
if (!reader)
auto& allocator = reader.get_allocator();
const auto raw = allocator.new_object<chain::block>(reader, witness);
if (is_null(raw) || !reader)
return nullptr;

// Copy header and tx hashes into preallocated optionals.
set_hashes(*message->block_ptr, data);

// Compute allocated size of raw and its block.
set_hashes(*raw, data);
const auto end = pointer_cast<uint8_t>(arena.allocate(zero));
const auto allocation = std::distance(begin, end);
const auto size = possible_narrow_sign_cast<size_t>(allocation);
message->block_ptr->set_allocation(size);

// Invokes destruct and deallocate (which may not need to free memory).
////return std::make_shared<block>(std::shared_ptr<chain::block>(raw,
//// allocator.deleter<chain::block>()));
return message;

// Allocate memory for copy of block_ptr and its block, and define deleter.
////const auto copy = pointer_cast<chain::block>(std::malloc(size));
////if (is_null(copy))
//// return nullptr;
////
// Copy the contiguous byte buffer that represents the block.
////std::memcpy(copy, block_ptr, size);
////
// Do not invoke block destructor, no free required for linear allocator.
////allocator.deleter<chain::block>()(block_ptr);
////std::free(copy);
////
////const auto thread = std::this_thread::get_id();
////const auto wiper = [thread](void* memory) NOEXCEPT
////{
//// if (std::this_thread::get_id() != thread)
//// {
//// std::free(memory);
//// }
////};
////
// This leaks copy if destruct on same thread.
////return std::make_shared<block>(std::shared_ptr<chain::block>(copy, wiper));
////
// Create an owning block pointer with deallocator and assign to message.
////const auto wiper = [](void* address) NOEXCEPT { std::free(address); };
////return std::make_shared<block>(std::shared_ptr<chain::block>(copy, wiper));
const auto size = std::distance(begin, end);
raw->set_allocation(possible_narrow_sign_cast<size_t>(size));

// All block and contained object destructors should be optimized out.
return to_shared<block>(std::shared_ptr<chain::block>(raw,
allocator.deleter<chain::block>()));
}

// static
Expand Down

0 comments on commit a13df3f

Please sign in to comment.