Skip to content

Commit 49d4c56

Browse files
MacroFakePastaPastaPasta
authored andcommitted
Merge 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@11e7908 MarcoFalke: review ACK 11e7908 🏯 ajtowns: ACK 11e7908 -- code review only Tree-SHA512: cbb4d8bfa095100677874b552d92c324c7d6354fcf7adab2ed52f57bd1793762871798b5288064ed1af2d2903a0ec9dbfec48d99955fc428f18cc28d6840dccc
1 parent 431c1e3 commit 49d4c56

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;
@@ -428,15 +430,7 @@ class prevector {
428430
// representation (with capacity N and size <= N).
429431
iterator p = first;
430432
char* endp = (char*)&(*end());
431-
if (!std::is_trivially_destructible<T>::value) {
432-
while (p != last) {
433-
(*p).~T();
434-
_size--;
435-
++p;
436-
}
437-
} else {
438-
_size -= last - p;
439-
}
433+
_size -= last - p;
440434
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
441435
return first;
442436
}
@@ -482,9 +476,6 @@ class prevector {
482476
}
483477

484478
~prevector() {
485-
if (!std::is_trivially_destructible<T>::value) {
486-
clear();
487-
}
488479
if (!is_direct()) {
489480
free(_union.indirect_contents.indirect);
490481
_union.indirect_contents.indirect = nullptr;

0 commit comments

Comments
 (0)