Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#25017: validation: make CScriptCheck and prevec…
Browse files Browse the repository at this point in the history
…tor swap members noexcept

e5485e8e4be7f2ee0671f58c3dcce35c68ba0ee0 test, bench: make prevector and checkqueue swap member functions noexcept (Jon Atack)
abc1ee509025d92db5311c3f5df3b61c09cad24f validation: make CScriptCheck and prevector swap member functions noexcept (Jon Atack)

Pull request description:

  along with those seen elsewhere in the codebase (prevector and checkqueue units/fuzz/bench).

  A swap must not fail; when a class has a swap member function, it should be declared noexcept.
  https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c84-a-swap-function-must-not-fail

ACKs for top commit:
  pk-b2:
    ACK bitcoin/bitcoin@e5485e8
  w0xlt:
    ACK bitcoin/bitcoin@e5485e8

Tree-SHA512: c82359d5e13f9262ce45efdae9baf71e41ed26568e0aff620e2bfb0ab37a62b6d56ae9340a28a0332c902cc1fa87da3fb72d6f6d6f53a8b7e695a5011f71f7f1
  • Loading branch information
MacroFake authored and PastaPastaPasta committed Jan 14, 2024
1 parent 89c1e77 commit 1b1badf
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/bench/checkqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
{
return true;
}
void swap(PrevectorJob& x){p.swap(x.p);};
void swap(PrevectorJob& x) noexcept
{
p.swap(x.p);
};
};
CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};

Expand Down
2 changes: 1 addition & 1 deletion src/hdchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CHDChain
);
}

void swap(CHDChain& first, CHDChain& second) // nothrow
void swap(CHDChain& first, CHDChain& second) noexcept
{
// enable ADL (not necessary in our case, but good practice)
using std::swap;
Expand Down
3 changes: 2 additions & 1 deletion src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ class prevector {
return *item_ptr(size() - 1);
}

void swap(prevector<N, T, Size, Diff>& other) {
void swap(prevector<N, T, Size, Diff>& other) noexcept
{
std::swap(_union, other._union);
std::swap(_size, other._size);
}
Expand Down
21 changes: 15 additions & 6 deletions src/test/checkqueue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct FakeCheck {
{
return true;
}
void swap(FakeCheck& x){};
void swap(FakeCheck& x) noexcept {};
};

struct FakeCheckCheckCompletion {
Expand All @@ -37,7 +37,7 @@ struct FakeCheckCheckCompletion {
n_calls.fetch_add(1, std::memory_order_relaxed);
return true;
}
void swap(FakeCheckCheckCompletion& x){};
void swap(FakeCheckCheckCompletion& x) noexcept {};
};

struct FailingCheck {
Expand All @@ -48,7 +48,7 @@ struct FailingCheck {
{
return !fails;
}
void swap(FailingCheck& x)
void swap(FailingCheck& x) noexcept
{
std::swap(fails, x.fails);
};
Expand All @@ -66,7 +66,10 @@ struct UniqueCheck {
results.insert(check_id);
return true;
}
void swap(UniqueCheck& x) { std::swap(x.check_id, check_id); };
void swap(UniqueCheck& x) noexcept
{
std::swap(x.check_id, check_id);
};
};


Expand Down Expand Up @@ -94,7 +97,10 @@ struct MemoryCheck {
{
fake_allocated_memory.fetch_sub(b, std::memory_order_relaxed);
};
void swap(MemoryCheck& x) { std::swap(b, x.b); };
void swap(MemoryCheck& x) noexcept
{
std::swap(b, x.b);
};
};

struct FrozenCleanupCheck {
Expand All @@ -118,7 +124,10 @@ struct FrozenCleanupCheck {
cv.wait(l, []{ return nFrozen.load(std::memory_order_relaxed) == 0;});
}
}
void swap(FrozenCleanupCheck& x){std::swap(should_freeze, x.should_freeze);};
void swap(FrozenCleanupCheck& x) noexcept
{
std::swap(should_freeze, x.should_freeze);
};
};

// Static Allocations
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/checkqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct DumbCheck {
return result;
}

void swap(DumbCheck& x)
void swap(DumbCheck& x) noexcept
{
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/prevector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class prevector_tester
pre_vector.shrink_to_fit();
}

void swap()
void swap() noexcept
{
real_vector.swap(real_vector_alt);
pre_vector.swap(pre_vector_alt);
Expand Down
3 changes: 2 additions & 1 deletion src/test/prevector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ class prevector_tester {
test();
}

void swap() {
void swap() noexcept
{
real_vector.swap(real_vector_alt);
pre_vector.swap(pre_vector_alt);
test();
Expand Down
3 changes: 2 additions & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ class CScriptCheck

bool operator()();

void swap(CScriptCheck &check) {
void swap(CScriptCheck& check) noexcept
{
std::swap(ptxTo, check.ptxTo);
std::swap(m_tx_out, check.m_tx_out);
std::swap(nIn, check.nIn);
Expand Down

0 comments on commit 1b1badf

Please sign in to comment.