Skip to content

Commit 07cb4de

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#24962: prevector: enforce is_trivially_copyable_v
11e7908 prevector: only allow trivially copyable types (Martin Leitner-Ankerl) Pull request description: prevector uses `memmove` to move around data, that means it can only be used with types that are trivially copyable. That implies that the types are trivially destructible, thus the checks for `is_trivially_destructible` are not needed. ACKs for top commit: w0xlt: ACK bitcoin/bitcoin@11e7908 MarcoFalke: review ACK 11e7908 🏯 ajtowns: ACK 11e7908 -- code review only Tree-SHA512: cbb4d8bfa095100677874b552d92c324c7d6354fcf7adab2ed52f57bd1793762871798b5288064ed1af2d2903a0ec9dbfec48d99955fc428f18cc28d6840dccc
2 parents 6b87fa5 + 11e7908 commit 07cb4de

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/prevector.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
*/
3636
template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
3737
class prevector {
38+
static_assert(std::is_trivially_copyable_v<T>);
39+
3840
public:
3941
typedef Size size_type;
4042
typedef Diff difference_type;
@@ -411,15 +413,7 @@ class prevector {
411413
// representation (with capacity N and size <= N).
412414
iterator p = first;
413415
char* endp = (char*)&(*end());
414-
if (!std::is_trivially_destructible<T>::value) {
415-
while (p != last) {
416-
(*p).~T();
417-
_size--;
418-
++p;
419-
}
420-
} else {
421-
_size -= last - p;
422-
}
416+
_size -= last - p;
423417
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
424418
return first;
425419
}
@@ -465,9 +459,6 @@ class prevector {
465459
}
466460

467461
~prevector() {
468-
if (!std::is_trivially_destructible<T>::value) {
469-
clear();
470-
}
471462
if (!is_direct()) {
472463
free(_union.indirect_contents.indirect);
473464
_union.indirect_contents.indirect = nullptr;

0 commit comments

Comments
 (0)