diff options
Diffstat (limited to 'core/os/memory.h')
-rw-r--r-- | core/os/memory.h | 102 |
1 files changed, 45 insertions, 57 deletions
diff --git a/core/os/memory.h b/core/os/memory.h index 49424037ff..b788068f53 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -29,9 +29,8 @@ #ifndef MEMORY_H #define MEMORY_H -#include <stddef.h> #include "safe_refcount.h" - +#include <stddef.h> /** @author Juan Linietsky <reduzio@gmail.com> @@ -41,9 +40,7 @@ #define PAD_ALIGN 16 //must always be greater than this at much #endif - - -class Memory{ +class Memory { Memory(); #ifdef DEBUG_ENABLED @@ -54,72 +51,65 @@ class Memory{ static size_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 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 size_t get_mem_available(); static size_t get_mem_usage(); static size_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); } - + _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); ///< operator new that takes a description and uses MemoryStaticPool +void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool -void * operator new(size_t p_size,const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool -void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool - -void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory +void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory #define memalloc(m_size) Memory::alloc_static(m_size) -#define memrealloc(m_mem,m_size) Memory::realloc_static(m_mem,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> +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) +#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) { +_ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) { //void *failptr=0; //ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */ 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) -#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; } +_ALWAYS_INLINE_ bool predelete_handler(void *) { + return true; +} -template<class T> +template <class T> void memdelete(T *p_class) { if (!predelete_handler(p_class)) return; // doesn't want to be deleted p_class->~T(); - Memory::free_static(p_class,false); + Memory::free_static(p_class, false); } -template<class T,class A> +template <class T, class A> void memdelete_allocator(T *p_class) { if (!predelete_handler(p_class)) @@ -128,33 +118,35 @@ void memdelete_allocator(T *p_class) { 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) +#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="") { +template <typename T> +T *memnew_arr_template(size_t p_elements, const char *p_descr = "") { - if (p_elements==0) + if (p_elements == 0) return 0; /** overloading operator new[] cannot be done , because it may not return the real allocated address (it may pad the 'element count' before the actual array). Because of that, it must be done by hand. This is the same strategy used by std::vector, and the PoolVector class, so it should be safe.*/ size_t len = sizeof(T) * p_elements; - uint64_t *mem = (uint64_t*)Memory::alloc_static( len , true ); - T *failptr=0; //get rid of a warning - ERR_FAIL_COND_V( !mem, failptr ); - *(mem-1)=p_elements; + uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true); + T *failptr = 0; //get rid of a warning + ERR_FAIL_COND_V(!mem, failptr); + *(mem - 1) = p_elements; - T* elems = (T*)mem; + T *elems = (T *)mem; /* call operator new */ - for (size_t i=0;i<p_elements;i++) { - new(&elems[i],sizeof(T),p_descr) T; + for (size_t i = 0; i < p_elements; i++) { + new (&elems[i], sizeof(T), p_descr) T; } - return (T*)mem; + return (T *)mem; } /** @@ -162,28 +154,27 @@ T* memnew_arr_template(size_t p_elements,const char *p_descr="") { * an allocated-with memnew_arr() array */ -template<typename T> +template <typename T> size_t memarr_len(const T *p_class) { - uint64_t* ptr = (uint64_t*)p_class; - return *(ptr-1); + uint64_t *ptr = (uint64_t *)p_class; + return *(ptr - 1); } -template<typename T> +template <typename T> void memdelete_arr(T *p_class) { - uint64_t* ptr = (uint64_t*)p_class; + uint64_t *ptr = (uint64_t *)p_class; - uint64_t elem_count = *(ptr-1); + uint64_t elem_count = *(ptr - 1); - for (uint64_t i=0;i<elem_count;i++) { + for (uint64_t i = 0; i < elem_count; i++) { p_class[i].~T(); }; - Memory::free_static(ptr,true); + Memory::free_static(ptr, true); } - struct _GlobalNil { int color; @@ -192,7 +183,6 @@ struct _GlobalNil { _GlobalNil *parent; _GlobalNil(); - }; struct _GlobalNilClass { @@ -200,6 +190,4 @@ struct _GlobalNilClass { static _GlobalNil _nil; }; - - #endif |