Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace arena::do_get_capacity() with require(). #1515

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions include/bitcoin/system/arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ class arena
return do_is_equal(other);
}

/// Get the remaining area memory capacity (additional to std::pmr).
NODISCARD size_t get_capacity() const NOEXCEPT
{
return do_get_capacity();
}
/// Require memory capacity, return current or nullptr (custom interface).
virtual void* require(size_t bytes) NOEXCEPT = 0;

private:
virtual void* do_allocate(size_t bytes, size_t align) THROWS = 0;
virtual void do_deallocate(void* ptr, size_t bytes, size_t align) NOEXCEPT = 0;
virtual bool do_is_equal(const arena& other) const NOEXCEPT = 0;
virtual size_t do_get_capacity() const NOEXCEPT = 0;
};

/// Left can deallocate memory allocated by right and vice versa.
Expand All @@ -86,12 +82,12 @@ class BC_API default_arena final
{
public:
static arena* get() NOEXCEPT;
void* require(size_t bytes) NOEXCEPT override;

private:
void* do_allocate(size_t bytes, size_t align) THROWS override;
void do_deallocate(void* ptr, size_t bytes, size_t align) NOEXCEPT override;
bool do_is_equal(const arena& other) const NOEXCEPT override;
size_t do_get_capacity() const NOEXCEPT override;
};

} // namespace libbitcoin
Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/system/retainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ class BC_API retainer final
DELETE_COPY_MOVE_DESTRUCT(retainer);

inline retainer() NOEXCEPT
: /*allocation_{},*/ shared_lock_{}
: allocation_{}, shared_lock_{}
{
}

inline retainer(std::shared_mutex& mutex, size_t=0) NOEXCEPT
: /*allocation_{ allocation },*/ shared_lock_{ mutex }
inline retainer(std::shared_mutex& mutex, size_t allocation=0) NOEXCEPT
: allocation_{ allocation }, shared_lock_{ mutex }
{
}

inline size_t allocation() const NOEXCEPT
{
return {};//// allocation_;
return allocation_;
}

private:
// These are thread safe.
////size_t allocation_;
size_t allocation_;
std::shared_lock<std::shared_mutex> shared_lock_;
};

Expand Down
6 changes: 2 additions & 4 deletions src/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ void* default_arena::do_allocate(size_t bytes, size_t) THROWS
{
////if (align > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
//// return ::operator new(bytes, std::align_val_t{ align });

return ::operator new(bytes);
}

void default_arena::do_deallocate(void* ptr, size_t, size_t) NOEXCEPT
{
////if (align > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
//// ::operator delete(ptr, std::align_val_t{ align });

::operator delete(ptr);
}

Expand All @@ -59,9 +57,9 @@ bool default_arena::do_is_equal(const arena& other) const NOEXCEPT
return &other == this;
}

size_t default_arena::do_get_capacity() const NOEXCEPT
void* default_arena::require(size_t) NOEXCEPT
{
return max_size_t;
return nullptr;
}

BC_POP_WARNING()
Expand Down
21 changes: 6 additions & 15 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ block::block(reader&& source, bool witness) NOEXCEPT
}

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

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

size_ = serialized_size(*txs_);
Expand Down Expand Up @@ -284,17 +281,11 @@ block::sizes block::serialized_size(
size.witnessed = ceilinged_add(size.witnessed, tx->serialized_size(true));
});

const auto common_size = ceilinged_add(
header::serialized_size(),
const auto common_size = ceilinged_add(header::serialized_size(),
variable_size(txs.size()));

const auto nominal_size = ceilinged_add(
common_size,
size.nominal);

const auto witnessed_size = ceilinged_add(
common_size,
size.witnessed);
const auto nominal_size = ceilinged_add(common_size, size.nominal);
const auto witnessed_size = ceilinged_add(common_size, size.witnessed);

return { nominal_size, witnessed_size };
}
Expand Down
24 changes: 8 additions & 16 deletions src/chain/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,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),
: point_(source.get_allocator().new_object<chain::point>(source),
source.get_allocator().deleter<chain::point>(source.get_arena())),
script_(
source.get_allocator().new_object<chain::script>(source, true),
script_(source.get_allocator().new_object<chain::script>(source, true),
source.get_allocator().deleter<chain::script>(source.get_arena())),
witness_(nullptr),
sequence_(source.read_4_bytes_little_endian()),
Expand Down Expand Up @@ -248,12 +246,10 @@ void input::to_data(writer& sink) const NOEXCEPT
// static/private
input::sizes input::serialized_size(const chain::script& script) NOEXCEPT
{
constexpr auto const_size = ceilinged_add(
point::serialized_size(),
constexpr auto const_size = ceilinged_add(point::serialized_size(),
sizeof(sequence_));

const auto nominal_size = ceilinged_add(
const_size,
const auto nominal_size = ceilinged_add(const_size,
script.serialized_size(true));

return { nominal_size, zero };
Expand All @@ -263,16 +259,13 @@ input::sizes input::serialized_size(const chain::script& script) NOEXCEPT
input::sizes input::serialized_size(const chain::script& script,
const chain::witness& witness) NOEXCEPT
{
constexpr auto const_size = ceilinged_add(
point::serialized_size(),
constexpr auto const_size = ceilinged_add(point::serialized_size(),
sizeof(sequence_));

const auto nominal_size = ceilinged_add(
const_size,
const auto nominal_size = ceilinged_add(const_size,
script.serialized_size(true));

const auto witnessed_size = ceilinged_add(
nominal_size,
const auto witnessed_size = ceilinged_add(nominal_size,
witness.serialized_size(true));

return { nominal_size, witnessed_size };
Expand Down Expand Up @@ -311,8 +304,7 @@ void input::set_witness(reader& source) NOEXCEPT
{
auto& allocator = source.get_allocator();

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

size_.witnessed = ceilinged_add(size_.nominal,
Expand Down
3 changes: 1 addition & 2 deletions src/chain/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ void operation::assign_data(reader& source) NOEXCEPT
source.invalidate();

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

underflow_ = !source;
Expand Down
3 changes: 1 addition & 2 deletions src/chain/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,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),
script_(source.get_allocator().new_object<chain::script>(source, true),
source.get_allocator().deleter<chain::script>(source.get_arena())),
valid_(source),
size_(serialized_size(*script_, value_))
Expand Down
32 changes: 10 additions & 22 deletions src/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,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>(),
inputs_(source.get_allocator().new_object<input_cptrs>(),
source.get_allocator().deleter<input_cptrs>(source.get_arena())),
outputs_(
source.get_allocator().new_object<output_cptrs>(),
outputs_(source.get_allocator().new_object<output_cptrs>(),
source.get_allocator().deleter<output_cptrs>(source.get_arena()))
{
assign_data(source, witness);
Expand Down Expand Up @@ -262,16 +260,14 @@ void transaction::assign_data(reader& source, bool witness) NOEXCEPT
count = source.read_size(max_block_size);
ins->reserve(count);
for (size_t in = 0; in < count; ++in)
ins->emplace_back(
allocator.new_object<input>(source),
ins->emplace_back(allocator.new_object<input>(source),
allocator.deleter<input>(source.get_arena()));

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),
outs->emplace_back(allocator.new_object<output>(source),
allocator.deleter<output>(source.get_arena()));

// Read or skip witnesses as specified.
Expand All @@ -293,8 +289,7 @@ void transaction::assign_data(reader& source, bool witness) NOEXCEPT
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),
outs->emplace_back(allocator.new_object<output>(source),
allocator.deleter<output>(source.get_arena()));
}

Expand Down Expand Up @@ -386,26 +381,19 @@ transaction::sizes transaction::serialized_size(
return ceilinged_add(total, output->serialized_size());
};

constexpr auto base_const_size = ceilinged_add(
sizeof(version_),
constexpr auto base_const_size = ceilinged_add(sizeof(version_),
sizeof(locktime_));

constexpr auto witness_const_size = ceilinged_add(
sizeof(witness_marker),
constexpr auto witness_const_size = ceilinged_add(sizeof(witness_marker),
sizeof(witness_enabled));

const auto base_size = ceilinged_add(ceilinged_add(ceilinged_add(
base_const_size,
variable_size(inputs.size())),
base_const_size, variable_size(inputs.size())),
variable_size(outputs.size())),
std::accumulate(outputs.begin(), outputs.end(), zero, outs));

const auto nominal_size = ceilinged_add(
base_size,
size.nominal);

const auto witnessed_size = ceilinged_add(ceilinged_add(
base_size,
const auto nominal_size = ceilinged_add(base_size, size.nominal);
const auto witnessed_size = ceilinged_add(ceilinged_add(base_size,
witness_const_size),
size.witnessed);

Expand Down
6 changes: 2 additions & 4 deletions src/chain/witness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ void witness::assign_data(reader& source, bool prefix) NOEXCEPT
for (size_t element = 0; element < count; ++element)
{
const auto size = source.read_size(max_block_weight);
stack_.emplace_back(
source.read_bytes_raw(size),
stack_.emplace_back(source.read_bytes_raw(size),
allocator.deleter<data_chunk>(source.get_arena()));
size_ = element_size(size_, stack_.back());
}
Expand All @@ -188,8 +187,7 @@ void witness::assign_data(reader& source, bool prefix) NOEXCEPT
while (!source.is_exhausted())
{
const auto size = source.read_size(max_block_weight);
stack_.emplace_back(
source.read_bytes_raw(size),
stack_.emplace_back(source.read_bytes_raw(size),
allocator.deleter<data_chunk>(source.get_arena()));
size_ = element_size(size_, stack_.back());
}
Expand Down
20 changes: 10 additions & 10 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class reporting_arena
size_t dec_count{};
size_t dec_bytes{};

void* require(size_t) NOEXCEPT override
{
return nullptr;
}

private:
void* do_allocate(size_t bytes, size_t align) override
{
Expand Down Expand Up @@ -157,11 +162,6 @@ class reporting_arena
return &other == this;
}

size_t do_get_capacity() const NOEXCEPT override
{
return {};
}

void report(void* ptr, size_t bytes, bool allocate) const NOEXCEPT
{
if constexpr (Report)
Expand Down Expand Up @@ -189,6 +189,11 @@ class mock_arena
size_t do_deallocate_align{};
mutable const arena* do_is_equal_address{};

void* require(size_t) NOEXCEPT override
{
return nullptr;
}

private:
void* do_allocate(size_t bytes, size_t align) THROWS override
{
Expand All @@ -209,11 +214,6 @@ class mock_arena
do_is_equal_address = &other;
return false;
}

size_t do_get_capacity() const NOEXCEPT override
{
return {};
}
};

template <bool Report = false>
Expand Down
Loading