From b77ceabca4a949a7829fd0318a4f8ea1563d2f06 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 18 Aug 2024 21:05:57 -0400 Subject: [PATCH] Use detachable arena. --- src/messages/block.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/messages/block.cpp b/src/messages/block.cpp index 2270bcb46..1ece821b9 100644 --- a/src/messages/block.cpp +++ b/src/messages/block.cpp @@ -102,9 +102,8 @@ typename block::cptr block::deserialize(arena& arena, uint32_t version, if (version < version_minimum || version > version_maximum) return nullptr; - const auto begin = pointer_cast(arena.initialize()); - if (is_null(begin)) - return nullptr; + // Get starting address of block allocation (nullptr if not detachable). + const auto begin = pointer_cast(arena.start()); istream source{ data }; byte_reader reader{ source, &arena }; @@ -114,13 +113,21 @@ typename block::cptr block::deserialize(arena& arena, uint32_t version, return nullptr; set_hashes(*raw, data); - const auto end = pointer_cast(arena.allocate(zero)); - const auto size = std::distance(begin, end); - raw->set_allocation(possible_narrow_sign_cast(size)); + + // Get size of block allocation owned by begin (zero if non-detachable). + raw->set_allocation(arena.detach()); // All block and contained object destructors should be optimized out. return to_shared(std::shared_ptr(raw, - allocator.deleter())); + [&arena, begin](chain::block* ptr) NOEXCEPT + { + // Detach from non-null begin (raw may be aligned|non-detachable). + arena.release(begin, ptr->get_allocation()); + + // Detachable arena is nop deallocation, which is necesssary since + // the allocation has been detached from arena (and freed above). + byte_allocator::deleter(&arena); + })); } // static