Skip to content

Commit

Permalink
Replace shared_ptr(*) with make_shared().
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 2, 2024
1 parent 5f216f4 commit 390d877
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
21 changes: 8 additions & 13 deletions include/bitcoin/system/data/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ namespace system {

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

// shared_ptr moves are avoided in vector population by using 'new' and passing
// to shared_pointer construct a raw pointer via std::vector.emplace_back:
// std::vector.emplace_back(new block{...}).
// Otherwise emplace_back copies the shared pointer, just as would push_back:
// std::vector.emplace_back(std::make_shared<block>(block{...})).
// Vector emplace constructs the instance using its allocator, invoking new.
// So in this case it constructs the shared_pointer which accepts the raw
// pointer as its constructor argument, passed by std::vector.emplace_back.

/// shared_ptr
/// ---------------------------------------------------------------------------

Expand Down Expand Up @@ -75,10 +66,7 @@ inline std::shared_ptr<const Type> to_shared(const Type& value) NOEXCEPT
template <typename Type, typename... Args>
inline std::shared_ptr<const Type> to_shared(Args&&... values) NOEXCEPT
{
BC_PUSH_WARNING(NO_NEW_OR_DELETE)
return std::shared_ptr<const Type>(
new const Type(std::forward<Args>(values)...));
BC_POP_WARNING()
return std::make_shared<const Type>(Type{ std::forward<Args>(values)... });
}

/// Create shared pointer to vector of const shared pointers from moved vector.
Expand Down Expand Up @@ -108,6 +96,13 @@ inline std::unique_ptr<const Type> to_unique(const Type& value) NOEXCEPT
return std::make_unique<const Type>(value);
}

/// Construct unique pointer to const from moved constructor parameters.
template <typename Type, typename... Args>
inline std::unique_ptr<const Type> to_unique(Args&&... values) NOEXCEPT
{
return std::make_unique<const Type>(Type{ std::forward<Args>(values)... });
}

BC_POP_WARNING()

} // namespace system
Expand Down
6 changes: 1 addition & 5 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ block block::from_data(reader& source, bool witness) NOEXCEPT
txs->reserve(capacity);

for (size_t tx = 0; tx < capacity; ++tx)
{
BC_PUSH_WARNING(NO_NEW_OR_DELETE)
txs->emplace_back(new transaction{ source, witness });
BC_POP_WARNING()
}
txs->push_back(to_shared<transaction>(source, witness));

// This is a pointer copy (non-const to const).
return txs;
Expand Down
29 changes: 11 additions & 18 deletions src/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,7 @@ read_puts(Source& source) NOEXCEPT

puts->reserve(capacity);
for (auto put = zero; put < capacity; ++put)
{
BC_PUSH_WARNING(NO_NEW_OR_DELETE)
puts->emplace_back(new Put{ source });
BC_POP_WARNING()
}
puts->push_back(to_shared<Put>(source));

// This is a pointer copy (non-const to const).
return puts;
Expand Down Expand Up @@ -860,19 +856,16 @@ hash_digest transaction::unversioned_signature_hash(
// TODO: taproot requires both single and double hash of each.
void transaction::initialize_sighash_cache() const NOEXCEPT
{
// This overconstructs the cache (anyone or !all), however it is simple and
// the same criteria applied by satoshi.
if (segregated_)
{
BC_PUSH_WARNING(NO_NEW_OR_DELETE)
sighash_cache_.reset(new sighash_cache
{
outputs_hash(),
points_hash(),
sequences_hash()
});
BC_POP_WARNING()
}
// This overconstructs the cache (anyone or !all), however it is simple.
if (!segregated_)
return;

sighash_cache_ = to_unique<sighash_cache>
(
outputs_hash(),
points_hash(),
sequences_hash()
);
}

// private
Expand Down
2 changes: 1 addition & 1 deletion test/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ BOOST_AUTO_TEST_CASE(transaction__is_missing_prevouts__default_inputs__true)
BOOST_AUTO_TEST_CASE(transaction__is_missing_prevouts__valid_prevout__false)
{
const input input{ { hash_digest{}, 42 }, {}, 0 };
input.prevout = to_shared<output>(42, script{});
input.prevout = to_shared<output>(42_u64, script{});
const accessor instance
{
0,
Expand Down

0 comments on commit 390d877

Please sign in to comment.