From 4e33610fb0ab762b49bcbb86c0be5ea0c227b73f Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Thu, 4 Aug 2022 13:53:03 +0200 Subject: [PATCH] [Core] Use std type traits to check operations triviality. (cherry picked from commit 6f02183f8c99694ca80bbd93234cf3fc338cd54e) --- core/cowdata.h | 9 +++++---- core/local_vector.h | 12 +++++++----- core/os/memory.h | 9 +++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/core/cowdata.h b/core/cowdata.h index a258cbcc20e..92f0911a718 100644 --- a/core/cowdata.h +++ b/core/cowdata.h @@ -32,6 +32,7 @@ #define COWDATA_H #include +#include #include "core/error_macros.h" #include "core/os/memory.h" @@ -195,7 +196,7 @@ void CowData::_unref(void *p_data) { } // clean up - if (!__has_trivial_destructor(T)) { + if (!std::is_trivially_destructible::value) { uint32_t *count = _get_size(); T *data = (T *)(count + 1); @@ -230,7 +231,7 @@ uint32_t CowData::_copy_on_write() { T *_data = (T *)(mem_new); // initialize new elements - if (__has_trivial_copy(T)) { + if (std::is_trivially_copyable::value) { memcpy(mem_new, _ptr, current_size * sizeof(T)); } else { @@ -293,7 +294,7 @@ Error CowData::resize(int p_size) { // construct the newly created elements - if (!__has_trivial_constructor(T)) { + if (!std::is_trivially_constructible::value) { for (int i = *_get_size(); i < p_size; i++) { memnew_placement(&_ptr[i], T); } @@ -302,7 +303,7 @@ Error CowData::resize(int p_size) { *_get_size() = p_size; } else if (p_size < current_size) { - if (!__has_trivial_destructor(T)) { + if (!std::is_trivially_destructible::value) { // deinitialize no longer needed elements for (uint32_t i = p_size; i < *_get_size(); i++) { T *t = &_ptr[i]; diff --git a/core/local_vector.h b/core/local_vector.h index 6204f0884cc..1991359088f 100644 --- a/core/local_vector.h +++ b/core/local_vector.h @@ -37,6 +37,8 @@ #include "core/sort_array.h" #include "core/vector.h" +#include + template class LocalVector { protected: @@ -64,7 +66,7 @@ public: CRASH_COND_MSG(!data, "Out of memory"); } - if (!__has_trivial_constructor(T) && !force_trivial) { + if (!std::is_trivially_constructible::value && !force_trivial) { memnew_placement(&data[count++], T(p_elem)); } else { data[count++] = p_elem; @@ -77,7 +79,7 @@ public: for (U i = p_index; i < count; i++) { data[i] = data[i + 1]; } - if (!__has_trivial_destructor(T) && !force_trivial) { + if (!std::is_trivially_destructible::value && !force_trivial) { data[count].~T(); } } @@ -90,7 +92,7 @@ public: if (count > p_index) { data[p_index] = data[count]; } - if (!__has_trivial_destructor(T) && !force_trivial) { + if (!std::is_trivially_destructible::value && !force_trivial) { data[count].~T(); } } @@ -146,7 +148,7 @@ public: _FORCE_INLINE_ U size() const { return count; } void resize(U p_size) { if (p_size < count) { - if (!__has_trivial_destructor(T) && !force_trivial) { + if (!std::is_trivially_destructible::value && !force_trivial) { for (U i = p_size; i < count; i++) { data[i].~T(); } @@ -163,7 +165,7 @@ public: data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } - if (!__has_trivial_constructor(T) && !force_trivial) { + if (!std::is_trivially_constructible::value && !force_trivial) { for (U i = count; i < p_size; i++) { memnew_placement(&data[i], T); } diff --git a/core/os/memory.h b/core/os/memory.h index 77f6ea5132a..958a8175bd4 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -35,6 +35,7 @@ #include "core/safe_refcount.h" #include +#include #ifndef PAD_ALIGN #define PAD_ALIGN 16 //must always be greater than this at much @@ -110,7 +111,7 @@ void memdelete(T *p_class) { if (!predelete_handler(p_class)) { return; // doesn't want to be deleted } - if (!__has_trivial_destructor(T)) { + if (!std::is_trivially_destructible::value) { p_class->~T(); } @@ -122,7 +123,7 @@ void memdelete_allocator(T *p_class) { if (!predelete_handler(p_class)) { return; // doesn't want to be deleted } - if (!__has_trivial_destructor(T)) { + if (!std::is_trivially_destructible::value) { p_class->~T(); } @@ -151,7 +152,7 @@ T *memnew_arr_template(size_t p_elements, const char *p_descr = "") { ERR_FAIL_COND_V(!mem, failptr); *(mem - 1) = p_elements; - if (!__has_trivial_constructor(T)) { + if (!std::is_trivially_constructible::value) { T *elems = (T *)mem; /* call operator new */ @@ -178,7 +179,7 @@ template void memdelete_arr(T *p_class) { uint64_t *ptr = (uint64_t *)p_class; - if (!__has_trivial_destructor(T)) { + if (!std::is_trivially_destructible::value) { uint64_t elem_count = *(ptr - 1); for (uint64_t i = 0; i < elem_count; i++) {