Skip to content

Commit

Permalink
Memcpy safety check
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Jul 12, 2023
1 parent d29a92a commit f009384
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions librapid/include/librapid/array/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,20 @@ namespace librapid {
auto size = static_cast<SizeType>(std::distance(begin, end));
m_begin = detail::safeAllocate(m_allocator, size);
m_end = m_begin + size;
if (typetraits::TriviallyDefaultConstructible<T>::value) {
// Use a slightly faster memcpy if the type is trivially default constructible
std::uninitialized_copy(begin, end, m_begin);

if (typetraits::TypeInfo<T>::canMemcpy) {
if (typetraits::TriviallyDefaultConstructible<T>::value) {
// Use a slightly faster memcpy if the type is trivially default constructible
std::uninitialized_copy(begin, end, m_begin);
} else {
// Otherwise, use the standard copy algorithm
std::copy(begin, end, m_begin);
}
} else {
// Otherwise, use the standard copy algorithm
std::copy(begin, end, m_begin);
// Since we can't memcpy, we have to copy each element individually
for (auto it = begin; it != end; ++it) {
new (m_begin + std::distance(begin, it)) T(*it);
}
}
}

Expand Down

0 comments on commit f009384

Please sign in to comment.