3939
4040#include < type_traits>
4141
42- // p_dummy argument is added to avoid conflicts with the engine functions when both engine and GDExtension are built as a static library on iOS.
43- void *operator new (size_t p_size, const char *p_dummy, const char *p_description); // /< operator new that takes a description and uses MemoryStaticPool
44- void *operator new (size_t p_size, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)); // /< operator new that takes a description and uses MemoryStaticPool
45- void *operator new (size_t p_size, const char *p_dummy, void *p_pointer, size_t check, const char *p_description); // /< operator new that takes a description and uses a pointer to the preallocated memory
46-
47- _ALWAYS_INLINE_ void *operator new (size_t p_size, const char *p_dummy, void *p_pointer, size_t check, const char *p_description) {
48- return p_pointer;
49- }
50-
51- #ifdef _MSC_VER
52- // When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291).
53- // The purpose of the following definitions is to muffle these warnings, not to provide a usable implementation of placement delete.
54- void operator delete (void *p_mem, const char *p_dummy, const char *p_description);
55- void operator delete (void *p_mem, const char *p_dummy, void *(*p_allocfunc)(size_t p_size));
56- void operator delete (void *p_mem, const char *p_dummy, void *p_pointer, size_t check, const char *p_description);
57- #endif
58-
5942namespace godot {
60-
6143class Wrapped ;
6244
6345namespace Memory {
@@ -92,6 +74,35 @@ void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
9274void free_static (void *p_ptr, bool p_pad_align = false );
9375}; // namespace Memory
9476
77+ class DefaultAllocator {
78+ public:
79+ _ALWAYS_INLINE_ static void *alloc (size_t p_memory) { return Memory::alloc_static (p_memory); }
80+ _ALWAYS_INLINE_ static void free (void *p_ptr) { Memory::free_static (p_ptr); }
81+ };
82+
83+ } // namespace godot
84+
85+ // Overload of `new` operator to use the `Memory::alloc_static()` function.
86+ // The `DefaultAllocator` parameter is just a tag to select this overload.
87+ // NOTE: do not inline `new` operators due to GCC+LTO compiler bug (see GH-119752).
88+ void *operator new (size_t p_size, godot::DefaultAllocator p_allocator);
89+
90+ // Overload of `new` operator to use a custom allocation function.
91+ // p_allocator argument is added to avoid conflicts with the engine functions when both engine and GDExtension are built as a static library on iOS.
92+ void *operator new (size_t p_size, godot::DefaultAllocator p_allocator, void *(*p_allocfunc)(size_t p_size));
93+
94+ #if defined(_MSC_VER) && !defined(__clang__)
95+ // When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291).
96+ // The purpose of the following definitions is to muffle these warnings, not to provide a usable implementation of placement delete.
97+ inline void operator delete (void *p_mem, godot::DefaultAllocator p_allocator) {
98+ CRASH_NOW_MSG (" Call to placement delete should not happen." );
99+ }
100+ inline void operator delete (void *p_mem, godot::DefaultAllocator, void *(*p_allocfunc)(size_t p_size)) {
101+ CRASH_NOW_MSG (" Call to placement delete should not happen." );
102+ }
103+ #endif // defined(_MSC_VER) && !defined(__clang__)
104+
105+ namespace godot {
95106template <typename T, std::enable_if_t <!std::is_base_of<::godot::Wrapped, T>::value, bool > = true >
96107_ALWAYS_INLINE_ void _pre_initialize () {}
97108
@@ -117,10 +128,10 @@ _ALWAYS_INLINE_ memnew_result_t<T> _post_initialize(T *p_obj) {
117128#define memrealloc (m_mem, m_size ) ::godot::Memory::realloc_static(m_mem, m_size)
118129#define memfree (m_mem ) ::godot::Memory::free_static(m_mem)
119130
120- #define memnew (m_class ) (::godot::_pre_initialize<std::remove_pointer_t <decltype (new (" " , " " ) m_class)>>(), ::godot::_post_initialize(new (" " , " " ) m_class))
131+ #define memnew (m_class ) (::godot::_pre_initialize<std::remove_pointer_t <decltype (new (godot::DefaultAllocator{} ) m_class)>>(), ::godot::_post_initialize(new (godot::DefaultAllocator{} ) m_class))
121132
122- #define memnew_allocator (m_class, m_allocator ) (::godot::_pre_initialize<std::remove_pointer_t <decltype (new (" " , " " ) m_class)>>(), ::godot::_post_initialize(new (" " , m_allocator::alloc) m_class))
123- #define memnew_placement (m_placement, m_class ) (::godot::_pre_initialize<std::remove_pointer_t <decltype (new (" " , " " ) m_class)>>(), ::godot::_post_initialize(new (" " , m_placement, sizeof (m_class), " " ) m_class))
133+ #define memnew_allocator (m_class, m_allocator ) (::godot::_pre_initialize<std::remove_pointer_t <decltype (new (godot::DefaultAllocator{}, m_allocator::alloc ) m_class)>>(), ::godot::_post_initialize(new (godot::DefaultAllocator{} , m_allocator::alloc) m_class))
134+ #define memnew_placement (m_placement, m_class ) (::godot::_pre_initialize<std::remove_pointer_t <decltype (new (m_placement ) m_class)>>(), ::godot::_post_initialize(new (m_placement) m_class))
124135
125136template <typename T>
126137void memdelete (T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = nullptr ) {
@@ -145,12 +156,6 @@ void memdelete_allocator(T *p_class) {
145156 A::free (p_class);
146157}
147158
148- class DefaultAllocator {
149- public:
150- _ALWAYS_INLINE_ static void *alloc (size_t p_memory) { return Memory::alloc_static (p_memory); }
151- _ALWAYS_INLINE_ static void free (void *p_ptr) { Memory::free_static (p_ptr); }
152- };
153-
154159template <typename T>
155160class DefaultTypedAllocator {
156161public:
@@ -166,7 +171,7 @@ _FORCE_INLINE_ uint64_t *_get_element_count_ptr(uint8_t *p_ptr) {
166171}
167172
168173template <typename T>
169- T *memnew_arr_template (size_t p_elements, const char *p_descr = " " ) {
174+ T *memnew_arr_template (size_t p_elements) {
170175 if (p_elements == 0 ) {
171176 return nullptr ;
172177 }
@@ -186,7 +191,7 @@ T *memnew_arr_template(size_t p_elements, const char *p_descr = "") {
186191
187192 /* call operator new */
188193 for (size_t i = 0 ; i < p_elements; i++) {
189- new (" " , &elems[i], sizeof (T), p_descr ) T;
194+ new (&elems[i]) T;
190195 }
191196 }
192197
0 commit comments