diff --git a/include/bitcoin/system/data/memory.hpp b/include/bitcoin/system/data/memory.hpp index 9d58027b2e..b719f4bedf 100644 --- a/include/bitcoin/system/data/memory.hpp +++ b/include/bitcoin/system/data/memory.hpp @@ -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{...})). -// 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 /// --------------------------------------------------------------------------- @@ -75,10 +66,7 @@ inline std::shared_ptr to_shared(const Type& value) NOEXCEPT template inline std::shared_ptr to_shared(Args&&... values) NOEXCEPT { - BC_PUSH_WARNING(NO_NEW_OR_DELETE) - return std::shared_ptr( - new const Type(std::forward(values)...)); - BC_POP_WARNING() + return std::make_shared(Type{ std::forward(values)... }); } /// Create shared pointer to vector of const shared pointers from moved vector. @@ -108,6 +96,13 @@ inline std::unique_ptr to_unique(const Type& value) NOEXCEPT return std::make_unique(value); } +/// Construct unique pointer to const from moved constructor parameters. +template +inline std::unique_ptr to_unique(Args&&... values) NOEXCEPT +{ + return std::make_unique(Type{ std::forward(values)... }); +} + BC_POP_WARNING() } // namespace system diff --git a/src/chain/block.cpp b/src/chain/block.cpp index da13744caa..6092cba185 100644 --- a/src/chain/block.cpp +++ b/src/chain/block.cpp @@ -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(source, witness)); // This is a pointer copy (non-const to const). return txs; diff --git a/src/chain/transaction.cpp b/src/chain/transaction.cpp index b6a3816ad1..1a1d43f2db 100644 --- a/src/chain/transaction.cpp +++ b/src/chain/transaction.cpp @@ -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(source)); // This is a pointer copy (non-const to const). return puts; @@ -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 + ( + outputs_hash(), + points_hash(), + sequences_hash() + ); } // private diff --git a/test/chain/transaction.cpp b/test/chain/transaction.cpp index f81fc91ba4..773a63dd06 100644 --- a/test/chain/transaction.cpp +++ b/test/chain/transaction.cpp @@ -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(42, script{}); + input.prevout = to_shared(42_u64, script{}); const accessor instance { 0,