Skip to content

Commit

Permalink
Merge pull request #1517 from evoskuil/master
Browse files Browse the repository at this point in the history
Use non-static deleter to simplify calls.
  • Loading branch information
evoskuil authored Aug 12, 2024
2 parents 167e065 + 85892b9 commit d55a8e7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 23 deletions.
12 changes: 12 additions & 0 deletions include/bitcoin/system/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ class allocator
};
}

/// Utility for passing deleted to shared pointer construction.
template <typename Type>
auto deleter() NOEXCEPT
{
return [arena = arena_](Type* ptr) NOEXCEPT
{
deleter<Type>(arena)(ptr);
};
}

/// Other
/// -----------------------------------------------------------------------

Expand Down Expand Up @@ -269,6 +279,8 @@ inline bool operator==(const allocator<Left>& left,
return left == right;
}

using byte_allocator = allocator<uint8_t>;

} // namespace libbitcoin

#endif
2 changes: 1 addition & 1 deletion include/bitcoin/system/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class BC_API block

/// Set/get memory allocation.
void set_allocation(size_t allocation) const NOEXCEPT;
const size_t get_allocation() const NOEXCEPT;
size_t get_allocation() const NOEXCEPT;

/// Identity.
/// -----------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ block::block(reader&& source, bool witness) NOEXCEPT

block::block(reader& source, bool witness) NOEXCEPT
: header_(source.get_allocator().new_object<chain::header>(source),
source.get_allocator().deleter<chain::header>(source.get_arena())),
source.get_allocator().deleter<chain::header>()),
txs_(source.get_allocator().new_object<transaction_cptrs>(),
source.get_allocator().deleter<transaction_cptrs>(source.get_arena()))
source.get_allocator().deleter<transaction_cptrs>())
{
assign_data(source, witness);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ void block::assign_data(reader& source, bool witness) NOEXCEPT

for (size_t tx = 0; tx < count; ++tx)
txs->emplace_back(allocator.new_object<transaction>(source, witness),
allocator.deleter<transaction>(source.get_arena()));
allocator.deleter<transaction>());

size_ = serialized_size(*txs_);
valid_ = source;
Expand All @@ -160,7 +160,7 @@ void block::set_allocation(size_t allocation) const NOEXCEPT
allocation_ = allocation;
}

const size_t block::get_allocation() const NOEXCEPT
size_t block::get_allocation() const NOEXCEPT
{
return allocation_;
}
Expand Down
6 changes: 3 additions & 3 deletions src/chain/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ input::input(reader&& source) NOEXCEPT
// Witness is deserialized and assigned by transaction.
input::input(reader& source) NOEXCEPT
: point_(source.get_allocator().new_object<chain::point>(source),
source.get_allocator().deleter<chain::point>(source.get_arena())),
source.get_allocator().deleter<chain::point>()),
script_(source.get_allocator().new_object<chain::script>(source, true),
source.get_allocator().deleter<chain::script>(source.get_arena())),
source.get_allocator().deleter<chain::script>()),
witness_(nullptr),
sequence_(source.read_4_bytes_little_endian()),
valid_(source),
Expand Down Expand Up @@ -305,7 +305,7 @@ void input::set_witness(reader& source) NOEXCEPT
auto& allocator = source.get_allocator();

witness_.reset(allocator.new_object<chain::witness>(source, true),
allocator.deleter<chain::witness>(source.get_arena()));
allocator.deleter<chain::witness>());

size_.witnessed = ceilinged_add(size_.nominal,
witness_->serialized_size(true));
Expand Down
2 changes: 1 addition & 1 deletion src/chain/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void operation::assign_data(reader& source) NOEXCEPT

// An invalid source.read_bytes_raw returns nullptr.
allocator.construct<chunk_cptr>(&data_, source.read_bytes_raw(size),
allocator.deleter<data_chunk>(source.get_arena()));
allocator.deleter<data_chunk>());

underflow_ = !source;

Expand Down
2 changes: 1 addition & 1 deletion src/chain/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ output::output(reader&& source) NOEXCEPT
output::output(reader& source) NOEXCEPT
: value_(source.read_8_bytes_little_endian()),
script_(source.get_allocator().new_object<chain::script>(source, true),
source.get_allocator().deleter<chain::script>(source.get_arena())),
source.get_allocator().deleter<chain::script>()),
valid_(source),
size_(serialized_size(*script_, value_))
{
Expand Down
4 changes: 3 additions & 1 deletion src/chain/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ void script::assign_data(reader& source, bool prefix) NOEXCEPT
}

valid_ = source;

// TODO: possible leak with linear arena allocation.
offset = ops_.begin();
}

Expand All @@ -285,7 +287,7 @@ script script::from_string(const std::string& mnemonic) NOEXCEPT
if (tokens.front().empty())
tokens.clear();

operations ops;
operations ops{};
ops.reserve(tokens.size());

// Create an op list from the split tokens.
Expand Down
12 changes: 6 additions & 6 deletions src/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ transaction::transaction(reader&& source, bool witness) NOEXCEPT
transaction::transaction(reader& source, bool witness) NOEXCEPT
: version_(source.read_4_bytes_little_endian()),
inputs_(source.get_allocator().new_object<input_cptrs>(),
source.get_allocator().deleter<input_cptrs>(source.get_arena())),
source.get_allocator().deleter<input_cptrs>()),
outputs_(source.get_allocator().new_object<output_cptrs>(),
source.get_allocator().deleter<output_cptrs>(source.get_arena()))
source.get_allocator().deleter<output_cptrs>())
{
assign_data(source, witness);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ void transaction::assign_data(reader& source, bool witness) NOEXCEPT
for (size_t in = 0; in < count; ++in)
ins->emplace_back(
allocator.new_object<input>(source),
allocator.deleter<input>(source.get_arena()));
allocator.deleter<input>());

// Expensive repeated recomputation, so cache segregated state.
// Detect witness as no inputs (marker) and expected flag (bip144).
Expand All @@ -261,14 +261,14 @@ void transaction::assign_data(reader& source, bool witness) NOEXCEPT
ins->reserve(count);
for (size_t in = 0; in < count; ++in)
ins->emplace_back(allocator.new_object<input>(source),
allocator.deleter<input>(source.get_arena()));
allocator.deleter<input>());

auto outs = to_non_const_raw_ptr(outputs_);
count = source.read_size(max_block_size);
outs->reserve(count);
for (size_t out = 0; out < count; ++out)
outs->emplace_back(allocator.new_object<output>(source),
allocator.deleter<output>(source.get_arena()));
allocator.deleter<output>());

// Read or skip witnesses as specified.
if (witness)
Expand All @@ -290,7 +290,7 @@ void transaction::assign_data(reader& source, bool witness) NOEXCEPT
outs->reserve(count);
for (size_t out = 0; out < count; ++out)
outs->emplace_back(allocator.new_object<output>(source),
allocator.deleter<output>(source.get_arena()));
allocator.deleter<output>());
}

locktime_ = source.read_4_bytes_little_endian();
Expand Down
6 changes: 3 additions & 3 deletions src/chain/witness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void witness::skip(reader& source, bool prefix) NOEXCEPT
void witness::assign_data(reader& source, bool prefix) NOEXCEPT
{
size_ = zero;
const auto& allocator = source.get_allocator();
auto& allocator = source.get_allocator();

if (prefix)
{
Expand All @@ -178,7 +178,7 @@ void witness::assign_data(reader& source, bool prefix) NOEXCEPT
{
const auto size = source.read_size(max_block_weight);
stack_.emplace_back(source.read_bytes_raw(size),
allocator.deleter<data_chunk>(source.get_arena()));
allocator.deleter<data_chunk>());
size_ = element_size(size_, stack_.back());
}
}
Expand All @@ -188,7 +188,7 @@ void witness::assign_data(reader& source, bool prefix) NOEXCEPT
{
const auto size = source.read_size(max_block_weight);
stack_.emplace_back(source.read_bytes_raw(size),
allocator.deleter<data_chunk>(source.get_arena()));
allocator.deleter<data_chunk>());
size_ = element_size(size_, stack_.back());
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/stream/streamers/byte_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ BOOST_AUTO_TEST_CASE(byte_reader__read_bytes_raw1__end_zero__not_null_valid)
const auto ptr = reader.read_bytes_raw(0);
BOOST_REQUIRE(ptr != nullptr);
BOOST_REQUIRE(reader);
allocator<>::deleter<data_chunk>(reader.get_arena())(ptr);
reader.get_allocator().deleter<data_chunk>(reader.get_arena())(ptr);
}

BOOST_AUTO_TEST_CASE(byte_reader__read_bytes_raw1__to_end__expected)
Expand All @@ -1451,7 +1451,7 @@ BOOST_AUTO_TEST_CASE(byte_reader__read_bytes_raw1__to_end__expected)
BOOST_REQUIRE(ptr != nullptr);
BOOST_REQUIRE_EQUAL(*ptr, to_chunk("abc"));
BOOST_REQUIRE(reader);
allocator<>::deleter<data_chunk>(reader.get_arena())(ptr);
reader.get_allocator().deleter<data_chunk>()(ptr);
}

BOOST_AUTO_TEST_CASE(byte_reader__read_bytes_raw1__middle__expected)
Expand All @@ -1464,7 +1464,7 @@ BOOST_AUTO_TEST_CASE(byte_reader__read_bytes_raw1__middle__expected)
BOOST_REQUIRE_EQUAL(*ptr, to_chunk("abc"));
BOOST_REQUIRE_EQUAL(stream.get(), '*');
BOOST_REQUIRE(reader);
allocator<>::deleter<data_chunk>(reader.get_arena())(ptr);
reader.get_allocator().deleter<data_chunk>()(ptr);
}

BOOST_AUTO_TEST_CASE(byte_reader__read_bytes_raw1__past_end__nullptr_invalid)
Expand Down

0 comments on commit d55a8e7

Please sign in to comment.