Merge bitcoin/bitcoin#24962: prevector: enforce is_trivially_copyable_v

11e7908484 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 11e7908484
  MarcoFalke:
    review ACK 11e7908484 🏯
  ajtowns:
    ACK 11e7908484 -- code review only

Tree-SHA512: cbb4d8bfa095100677874b552d92c324c7d6354fcf7adab2ed52f57bd1793762871798b5288064ed1af2d2903a0ec9dbfec48d99955fc428f18cc28d6840dccc
pull/25148/head
MacroFake 3 years ago
commit 07cb4dee5d
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

@ -35,6 +35,8 @@
*/
template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
class prevector {
static_assert(std::is_trivially_copyable_v<T>);
public:
typedef Size size_type;
typedef Diff difference_type;
@ -411,15 +413,7 @@ public:
// representation (with capacity N and size <= N).
iterator p = first;
char* endp = (char*)&(*end());
if (!std::is_trivially_destructible<T>::value) {
while (p != last) {
(*p).~T();
_size--;
++p;
}
} else {
_size -= last - p;
}
_size -= last - p;
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
return first;
}
@ -465,9 +459,6 @@ public:
}
~prevector() {
if (!std::is_trivially_destructible<T>::value) {
clear();
}
if (!is_direct()) {
free(_union.indirect_contents.indirect);
_union.indirect_contents.indirect = nullptr;

Loading…
Cancel
Save