1
0
Fork 0

[Core] Use std type traits to check operations triviality.

(cherry picked from commit 6f02183f8c)
This commit is contained in:
Fabio Alessandrelli 2022-08-04 13:53:03 +02:00 committed by Rémi Verschelde
parent d70e80ee78
commit 9a87271aa8
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 12 additions and 9 deletions

View File

@ -32,6 +32,7 @@
#define COWDATA_H_
#include <string.h>
#include <type_traits>
#include "core/error_macros.h"
#include "core/os/memory.h"
@ -199,7 +200,7 @@ void CowData<T>::_unref(void *p_data) {
return; // still in use
// clean up
if (!__has_trivial_destructor(T)) {
if (!std::is_trivially_destructible<T>::value) {
uint32_t *count = _get_size();
T *data = (T *)(count + 1);
@ -233,7 +234,7 @@ void CowData<T>::_copy_on_write() {
T *_data = (T *)(mem_new);
// initialize new elements
if (__has_trivial_copy(T)) {
if (std::is_trivially_copyable<T>::value) {
memcpy(mem_new, _ptr, current_size * sizeof(T));
} else {
@ -292,7 +293,7 @@ Error CowData<T>::resize(int p_size) {
// construct the newly created elements
if (!__has_trivial_constructor(T)) {
if (!std::is_trivially_constructible<T>::value) {
T *elems = _get_data();
for (int i = *_get_size(); i < p_size; i++) {
@ -303,8 +304,7 @@ Error CowData<T>::resize(int p_size) {
*_get_size() = p_size;
} else if (p_size < current_size) {
if (!__has_trivial_destructor(T)) {
if (!std::is_trivially_destructible<T>::value) {
// deinitialize no longer needed elements
for (uint32_t i = p_size; i < *_get_size(); i++) {
T *t = &_get_data()[i];

View File

@ -35,6 +35,7 @@
#include "core/safe_refcount.h"
#include <stddef.h>
#include <type_traits>
#ifndef PAD_ALIGN
#define PAD_ALIGN 16 //must always be greater than this at much
@ -113,8 +114,9 @@ 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<T>::value) {
p_class->~T();
}
Memory::free_static(p_class, false);
}
@ -124,8 +126,9 @@ 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<T>::value) {
p_class->~T();
}
A::free(p_class);
}
@ -151,7 +154,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<T>::value) {
T *elems = (T *)mem;
/* call operator new */
@ -180,7 +183,7 @@ void memdelete_arr(T *p_class) {
uint64_t *ptr = (uint64_t *)p_class;
if (!__has_trivial_destructor(T)) {
if (!std::is_trivially_destructible<T>::value) {
uint64_t elem_count = *(ptr - 1);
for (uint64_t i = 0; i < elem_count; i++) {