Try #1 at setting up a fully automatic memnew() macro.

This commit is contained in:
Relintai 2023-10-23 20:40:06 +02:00
parent e468d94bbe
commit ee393fc5b5
3 changed files with 32 additions and 6 deletions

View File

@ -641,7 +641,7 @@ private:
_resize_and_rehash(capacity_index + 1); _resize_and_rehash(capacity_index + 1);
} }
Element *elem = memnew(Element(p_key, p_value)); Element *elem = memnew_core(Element(p_key, p_value));
if (tail_element == nullptr) { if (tail_element == nullptr) {
head_element = elem; head_element = elem;

View File

@ -32,11 +32,13 @@
#include "core/defs.h" #include "core/defs.h"
#include "core/os/safe_refcount.h" #include "core/os/safe_refcount.h"
#include "core/wrapped.h"
#include <stddef.h> #include <stddef.h>
#include <type_traits>
#ifndef PAD_ALIGN #ifndef PAD_ALIGN
#define PAD_ALIGN 16 //must always be greater than this at much #define PAD_ALIGN 16 // must always be greater than this at much
#endif #endif
class Memory { class Memory {
@ -88,11 +90,21 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
return p_obj; return p_obj;
} }
#define memnew(m_class) _post_initialize(new ("") m_class) template <typename T>
_ALWAYS_INLINE_ T *memnew_template() {
if (std::is_base_of<_Wrapped, T>::value) {
return T::_new();
}
return _post_initialize(new T);
}
#define memnew_core(m_class) _post_initialize(new ("") m_class)
#define memnew(m_class) memnew_template<m_class>()
_ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) { _ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) {
//void *failptr=0; // void *failptr=0;
//ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */ // ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */
return p_pointer; return p_pointer;
} }
@ -109,6 +121,12 @@ void memdelete(T *p_class) {
if (!predelete_handler(p_class)) { if (!predelete_handler(p_class)) {
return; // doesn't want to be deleted return; // doesn't want to be deleted
} }
if (std::is_base_of<_Wrapped, T>::value) {
reinterpret_cast<_Wrapped *>(p_class)->free();
return;
}
if (!HAS_TRIVIAL_DESTRUCTOR(T)) { if (!HAS_TRIVIAL_DESTRUCTOR(T)) {
p_class->~T(); p_class->~T();
} }
@ -121,6 +139,12 @@ void memdelete_allocator(T *p_class) {
if (!predelete_handler(p_class)) { if (!predelete_handler(p_class)) {
return; // doesn't want to be deleted return; // doesn't want to be deleted
} }
if (std::is_base_of<_Wrapped, T>::value) {
reinterpret_cast<_Wrapped *>(p_class)->free();
return;
}
if (!HAS_TRIVIAL_DESTRUCTOR(T)) { if (!HAS_TRIVIAL_DESTRUCTOR(T)) {
p_class->~T(); p_class->~T();
} }
@ -146,7 +170,7 @@ T *memnew_arr_template(size_t p_elements, const char *p_descr = "") {
size_t len = sizeof(T) * p_elements; size_t len = sizeof(T) * p_elements;
uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true); uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true);
T *failptr = nullptr; //get rid of a warning T *failptr = nullptr; // get rid of a warning
ERR_FAIL_COND_V(!mem, failptr); ERR_FAIL_COND_V(!mem, failptr);
*(mem - 1) = p_elements; *(mem - 1) = p_elements;

View File

@ -38,6 +38,8 @@ class _Wrapped {
public: public:
pandemonium_object *_owner; pandemonium_object *_owner;
size_t _type_tag; size_t _type_tag;
virtual void free() {}
}; };
#endif // WRAPPED_H #endif // WRAPPED_H