123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #ifndef MEMORY_H
- #define MEMORY_H
- #include "core/safe_refcount.h"
- #include <stddef.h>
- #ifndef PAD_ALIGN
- #define PAD_ALIGN 16
- #endif
- class Memory {
- Memory();
- #ifdef DEBUG_ENABLED
- static uint64_t mem_usage;
- static uint64_t max_usage;
- #endif
- static uint64_t alloc_count;
- public:
- static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
- static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
- static void free_static(void *p_ptr, bool p_pad_align = false);
- static uint64_t get_mem_available();
- static uint64_t get_mem_usage();
- static uint64_t get_mem_max_usage();
- };
- class DefaultAllocator {
- public:
- _FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, false); }
- _FORCE_INLINE_ static void free(void *p_ptr) { return Memory::free_static(p_ptr, false); }
- };
- void *operator new(size_t p_size, const char *p_description);
- void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size));
- void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description);
- #ifdef _MSC_VER
- void operator delete(void *p_mem, const char *p_description);
- void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size));
- void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description);
- #endif
- #define memalloc(m_size) Memory::alloc_static(m_size)
- #define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size)
- #define memfree(m_size) Memory::free_static(m_size)
- _ALWAYS_INLINE_ void postinitialize_handler(void *) {}
- template <class T>
- _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
- postinitialize_handler(p_obj);
- return p_obj;
- }
- #define memnew(m_class) _post_initialize(new ("") m_class)
- _ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) {
-
-
- return p_pointer;
- }
- #define memnew_allocator(m_class, m_allocator) _post_initialize(new (m_allocator::alloc) m_class)
- #define memnew_placement(m_placement, m_class) _post_initialize(new (m_placement, sizeof(m_class), "") m_class)
- _ALWAYS_INLINE_ bool predelete_handler(void *) {
- return true;
- }
- template <class T>
- void memdelete(T *p_class) {
- if (!predelete_handler(p_class))
- return;
- if (!__has_trivial_destructor(T))
- p_class->~T();
- Memory::free_static(p_class, false);
- }
- template <class T, class A>
- void memdelete_allocator(T *p_class) {
- if (!predelete_handler(p_class))
- return;
- if (!__has_trivial_destructor(T))
- p_class->~T();
- A::free(p_class);
- }
- #define memdelete_notnull(m_v) \
- { \
- if (m_v) memdelete(m_v); \
- }
- #define memnew_arr(m_class, m_count) memnew_arr_template<m_class>(m_count)
- template <typename T>
- T *memnew_arr_template(size_t p_elements, const char *p_descr = "") {
- if (p_elements == 0)
- return 0;
-
- size_t len = sizeof(T) * p_elements;
- uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true);
- T *failptr = 0;
- ERR_FAIL_COND_V(!mem, failptr);
- *(mem - 1) = p_elements;
- if (!__has_trivial_constructor(T)) {
- T *elems = (T *)mem;
-
- for (size_t i = 0; i < p_elements; i++) {
- new (&elems[i], sizeof(T), p_descr) T;
- }
- }
- return (T *)mem;
- }
- template <typename T>
- size_t memarr_len(const T *p_class) {
- uint64_t *ptr = (uint64_t *)p_class;
- return *(ptr - 1);
- }
- template <typename T>
- void memdelete_arr(T *p_class) {
- uint64_t *ptr = (uint64_t *)p_class;
- if (!__has_trivial_destructor(T)) {
- uint64_t elem_count = *(ptr - 1);
- for (uint64_t i = 0; i < elem_count; i++) {
- p_class[i].~T();
- }
- }
- Memory::free_static(ptr, true);
- }
- struct _GlobalNil {
- int color;
- _GlobalNil *right;
- _GlobalNil *left;
- _GlobalNil *parent;
- _GlobalNil();
- };
- struct _GlobalNilClass {
- static _GlobalNil _nil;
- };
- #endif
|