From 326bd9a1e9702c1bbe6d7ca6a9d44b3e1f03d750 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 16 Apr 2023 15:42:57 +0200 Subject: [PATCH] Added HAS_TRIVIAL_CONSTRUCTOR, HAS_TRIVIAL_DESTRUCTOR, and HAS_TRIVIAL_COPY macros to typedefs to fix new clang deprecations. --- core/containers/cowdata.h | 8 ++++---- core/containers/local_vector.h | 10 +++++----- core/containers/paged_allocator.h | 2 +- core/containers/paged_array.h | 10 +++++----- core/containers/tight_local_vector.h | 10 +++++----- core/os/memory.h | 8 ++++---- core/typedefs.h | 30 ++++++++++++++++++++++++++++ 7 files changed, 54 insertions(+), 24 deletions(-) diff --git a/core/containers/cowdata.h b/core/containers/cowdata.h index 8d9d7ba82..d5b4d17cf 100644 --- a/core/containers/cowdata.h +++ b/core/containers/cowdata.h @@ -209,7 +209,7 @@ void CowData::_unref(void *p_data) { } // clean up - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { uint32_t *count = _get_size(); T *data = (T *)(count + 1); @@ -244,7 +244,7 @@ uint32_t CowData::_copy_on_write() { T *_data = (T *)(mem_new); // initialize new elements - if (__has_trivial_copy(T)) { + if (HAS_TRIVIAL_COPY(T)) { memcpy(mem_new, _ptr, current_size * sizeof(T)); } else { @@ -307,7 +307,7 @@ Error CowData::resize(int p_size) { // construct the newly created elements - if (!__has_trivial_constructor(T)) { + if (!HAS_TRIVIAL_CONSTRUCTOR(T)) { for (int i = *_get_size(); i < p_size; i++) { memnew_placement(&_ptr[i], T); } @@ -316,7 +316,7 @@ Error CowData::resize(int p_size) { *_get_size() = p_size; } else if (p_size < current_size) { - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { // deinitialize no longer needed elements for (uint32_t i = p_size; i < *_get_size(); i++) { T *t = &_ptr[i]; diff --git a/core/containers/local_vector.h b/core/containers/local_vector.h index cd9048179..88b6386e8 100644 --- a/core/containers/local_vector.h +++ b/core/containers/local_vector.h @@ -63,7 +63,7 @@ public: CRASH_COND_MSG(!data, "Out of memory"); } - if (!__has_trivial_constructor(T) && !force_trivial) { + if (!HAS_TRIVIAL_CONSTRUCTOR(T) && !force_trivial) { memnew_placement(&data[count++], T(p_elem)); } else { data[count++] = p_elem; @@ -76,7 +76,7 @@ public: for (U i = p_index; i < count; i++) { data[i] = data[i + 1]; } - if (!__has_trivial_destructor(T) && !force_trivial) { + if (!HAS_TRIVIAL_DESTRUCTOR(T) && !force_trivial) { data[count].~T(); } } @@ -89,7 +89,7 @@ public: if (count > p_index) { data[p_index] = data[count]; } - if (!__has_trivial_destructor(T) && !force_trivial) { + if (!HAS_TRIVIAL_DESTRUCTOR(T) && !force_trivial) { data[count].~T(); } } @@ -146,7 +146,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 (!HAS_TRIVIAL_DESTRUCTOR(T) && !force_trivial) { for (U i = p_size; i < count; i++) { data[i].~T(); } @@ -163,7 +163,7 @@ public: data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } - if (!__has_trivial_constructor(T) && !force_trivial) { + if (!HAS_TRIVIAL_CONSTRUCTOR(T) && !force_trivial) { for (U i = count; i < p_size; i++) { memnew_placement(&data[i], T); } diff --git a/core/containers/paged_allocator.h b/core/containers/paged_allocator.h index 11102fdd7..650d88a47 100644 --- a/core/containers/paged_allocator.h +++ b/core/containers/paged_allocator.h @@ -89,7 +89,7 @@ public: } void reset(bool p_allow_unfreed = false) { - if (!p_allow_unfreed || !__has_trivial_destructor(T)) { + if (!p_allow_unfreed || !HAS_TRIVIAL_DESTRUCTOR(T)) { ERR_FAIL_COND(allocs_available < pages_allocated * page_size); } if (pages_allocated) { diff --git a/core/containers/paged_array.h b/core/containers/paged_array.h index 7e589a696..da0b2434d 100644 --- a/core/containers/paged_array.h +++ b/core/containers/paged_array.h @@ -197,7 +197,7 @@ public: uint32_t page = count >> page_size_shift; uint32_t offset = count & page_size_mask; - if (!__has_trivial_constructor(T)) { + if (!HAS_TRIVIAL_CONSTRUCTOR(T)) { memnew_placement(&page_data[page][offset], T(p_value)); } else { page_data[page][offset] = p_value; @@ -209,7 +209,7 @@ public: _FORCE_INLINE_ void pop_back() { ERR_FAIL_COND(count == 0); - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { uint32_t page = (count - 1) >> page_size_shift; uint32_t offset = (count - 1) & page_size_mask; page_data[page][offset].~T(); @@ -226,7 +226,7 @@ public: void clear() { //destruct if needed - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { for (uint64_t i = 0; i < count; i++) { uint32_t page = i >> page_size_shift; uint32_t offset = i & page_size_mask; @@ -309,13 +309,13 @@ public: uint32_t to_copy = MIN(page_size - new_remainder, remainder); for (uint32_t i = 0; i < to_copy; i++) { - if (!__has_trivial_constructor(T)) { + if (!HAS_TRIVIAL_CONSTRUCTOR(T)) { memnew_placement(&dst_page[i + new_remainder], T(remainder_page[i + remainder - to_copy])); } else { dst_page[i + new_remainder] = remainder_page[i + remainder - to_copy]; } - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { remainder_page[i + remainder - to_copy].~T(); } } diff --git a/core/containers/tight_local_vector.h b/core/containers/tight_local_vector.h index ecc205e4c..a3b920188 100644 --- a/core/containers/tight_local_vector.h +++ b/core/containers/tight_local_vector.h @@ -65,7 +65,7 @@ public: CRASH_COND_MSG(!data, "Out of memory"); } - if constexpr (!__has_trivial_constructor(T) && !force_trivial) { + if constexpr (!HAS_TRIVIAL_CONSTRUCTOR(T) && !force_trivial) { memnew_placement(&data[count++], T(p_elem)); } else { data[count++] = p_elem; @@ -78,7 +78,7 @@ public: for (U i = p_index; i < count; i++) { data[i] = data[i + 1]; } - if constexpr (!__has_trivial_destructor(T) && !force_trivial) { + if constexpr (!HAS_TRIVIAL_DESTRUCTOR(T) && !force_trivial) { data[count].~T(); } } @@ -91,7 +91,7 @@ public: if (count > p_index) { data[p_index] = data[count]; } - if constexpr (!__has_trivial_destructor(T) && !force_trivial) { + if constexpr (!HAS_TRIVIAL_DESTRUCTOR(T) && !force_trivial) { data[count].~T(); } } @@ -147,7 +147,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 (!HAS_TRIVIAL_DESTRUCTOR(T) && !force_trivial) { for (U i = p_size; i < count; i++) { data[i].~T(); } @@ -164,7 +164,7 @@ public: data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } - if (!__has_trivial_constructor(T) && !force_trivial) { + if (!HAS_TRIVIAL_CONSTRUCTOR(T) && !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 4df9bb2ac..9462db68f 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -109,7 +109,7 @@ void memdelete(T *p_class) { if (!predelete_handler(p_class)) { return; // doesn't want to be deleted } - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { p_class->~T(); } @@ -121,7 +121,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 (!HAS_TRIVIAL_DESTRUCTOR(T)) { p_class->~T(); } @@ -150,7 +150,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 (!HAS_TRIVIAL_CONSTRUCTOR(T)) { T *elems = (T *)mem; /* call operator new */ @@ -177,7 +177,7 @@ template void memdelete_arr(T *p_class) { uint64_t *ptr = (uint64_t *)p_class; - if (!__has_trivial_destructor(T)) { + if (!HAS_TRIVIAL_DESTRUCTOR(T)) { uint64_t elem_count = *(ptr - 1); for (uint64_t i = 0; i < elem_count; i++) { diff --git a/core/typedefs.h b/core/typedefs.h index 8ea51c998..4b4148811 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -393,4 +393,34 @@ struct _GlobalLock { // Limit the depth of recursive algorithms when dealing with Array/Dictionary #define MAX_RECURSION 100 +//HAS_TRIVIAL_CONSTRUCTOR + +#if defined(__llvm__) && _llvm_has_builtin(__is_trivially_constructible) +#define HAS_TRIVIAL_CONSTRUCTOR(T) __is_trivially_constructible(T) +#endif + +#ifndef HAS_TRIVIAL_CONSTRUCTOR +#define HAS_TRIVIAL_CONSTRUCTOR(T) __has_trivial_constructor(T) +#endif + +//HAS_TRIVIAL_DESTRUCTOR + +#if defined(__llvm__) && _llvm_has_builtin(__is_trivially_destructible) +#define HAS_TRIVIAL_DESTRUCTOR(T) __is_trivially_destructible(T) +#endif + +#ifndef HAS_TRIVIAL_DESTRUCTOR +#define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T) +#endif + +//HAS_TRIVIAL_COPY + +#if defined(__llvm__) && _llvm_has_builtin(__is_trivially_copyable) +#define HAS_TRIVIAL_COPY(T) __is_trivially_copyable(T) +#endif + +#ifndef HAS_TRIVIAL_COPY +#define HAS_TRIVIAL_COPY(T) __has_trivial_copy(T) +#endif + #endif // TYPEDEFS_H