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);
}
Element *elem = memnew(Element(p_key, p_value));
Element *elem = memnew_core(Element(p_key, p_value));
if (tail_element == nullptr) {
head_element = elem;

View File

@ -32,8 +32,10 @@
#include "core/defs.h"
#include "core/os/safe_refcount.h"
#include "core/wrapped.h"
#include <stddef.h>
#include <type_traits>
#ifndef PAD_ALIGN
#define PAD_ALIGN 16 // must always be greater than this at much
@ -88,7 +90,17 @@ _ALWAYS_INLINE_ T *_post_initialize(T *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) {
// void *failptr=0;
@ -109,6 +121,12 @@ void memdelete(T *p_class) {
if (!predelete_handler(p_class)) {
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)) {
p_class->~T();
}
@ -121,6 +139,12 @@ void memdelete_allocator(T *p_class) {
if (!predelete_handler(p_class)) {
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)) {
p_class->~T();
}

View File

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