2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* memory.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 20:16:55 +08:00
/* https://godotengine.org */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
2019-01-01 19:53:14 +08:00
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 09:10:30 +08:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 07:50:27 +08:00
2014-02-10 09:10:30 +08:00
# ifndef MEMORY_H
# define MEMORY_H
2018-09-12 00:13:45 +08:00
# include "core/safe_refcount.h"
2017-03-05 23:44:50 +08:00
# include <stddef.h>
2014-02-10 09:10:30 +08:00
/**
@ author Juan Linietsky < reduzio @ gmail . com >
*/
2017-01-06 21:15:44 +08:00
# ifndef PAD_ALIGN
# define PAD_ALIGN 16 //must always be greater than this at much
# endif
2017-03-05 23:44:50 +08:00
class Memory {
2014-02-10 09:10:30 +08:00
Memory ( ) ;
2017-01-06 21:15:44 +08:00
# ifdef DEBUG_ENABLED
2017-07-25 08:29:46 +08:00
static uint64_t mem_usage ;
static uint64_t max_usage ;
2017-01-06 21:15:44 +08:00
# endif
2017-07-11 20:26:34 +08:00
2017-07-25 08:29:46 +08:00
static uint64_t alloc_count ;
2017-01-06 21:15:44 +08:00
2014-02-10 09:10:30 +08:00
public :
2017-03-05 23:44:50 +08:00
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 ) ;
2017-01-06 21:15:44 +08:00
2017-07-25 08:29:46 +08:00
static uint64_t get_mem_available ( ) ;
static uint64_t get_mem_usage ( ) ;
static uint64_t get_mem_max_usage ( ) ;
2014-02-10 09:10:30 +08:00
} ;
class DefaultAllocator {
public :
2017-01-06 21:15:44 +08:00
_FORCE_INLINE_ static void * alloc ( size_t p_memory ) { return Memory : : alloc_static ( p_memory , false ) ; }
2019-07-01 18:59:42 +08:00
_FORCE_INLINE_ static void free ( void * p_ptr ) { Memory : : free_static ( p_ptr , false ) ; }
2014-02-10 09:10:30 +08:00
} ;
2017-03-05 23:44:50 +08:00
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
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
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
2014-02-10 09:10:30 +08:00
2017-11-18 15:46:53 +08:00
# ifdef _MSC_VER
// When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291).
// The purpose of the following definitions is to muffle these warnings, not to provide a usable implementation of placement delete.
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
2014-02-10 09:10:30 +08:00
# define memalloc(m_size) Memory::alloc_static(m_size)
2017-03-05 23:44:50 +08:00
# define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size)
2014-02-10 09:10:30 +08:00
# define memfree(m_size) Memory::free_static(m_size)
2015-06-13 05:27:48 +08:00
_ALWAYS_INLINE_ void postinitialize_handler ( void * ) { }
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
template < class T >
2015-06-13 05:27:48 +08:00
_ALWAYS_INLINE_ T * _post_initialize ( T * p_obj ) {
2016-03-09 07:00:52 +08:00
2014-02-10 09:10:30 +08:00
postinitialize_handler ( p_obj ) ;
return p_obj ;
}
2017-03-05 23:44:50 +08:00
# define memnew(m_class) _post_initialize(new ("") m_class)
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
_ALWAYS_INLINE_ void * operator new ( size_t p_size , void * p_pointer , size_t check , const char * p_description ) {
2017-01-14 19:26:56 +08:00
//void *failptr=0;
//ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */
2015-06-13 05:27:48 +08:00
return p_pointer ;
}
2017-03-05 23:44:50 +08:00
# 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)
2015-06-13 05:27:48 +08:00
2017-03-05 23:44:50 +08:00
_ALWAYS_INLINE_ bool predelete_handler ( void * ) {
return true ;
}
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
template < class T >
2014-02-10 09:10:30 +08:00
void memdelete ( T * p_class ) {
2016-03-09 07:00:52 +08:00
2014-02-10 09:10:30 +08:00
if ( ! predelete_handler ( p_class ) )
return ; // doesn't want to be deleted
2019-01-03 12:56:41 +08:00
if ( ! __has_trivial_destructor ( T ) )
p_class - > ~ T ( ) ;
2017-03-05 23:44:50 +08:00
Memory : : free_static ( p_class , false ) ;
2014-02-10 09:10:30 +08:00
}
2017-03-05 23:44:50 +08:00
template < class T , class A >
2014-02-10 09:10:30 +08:00
void memdelete_allocator ( T * p_class ) {
if ( ! predelete_handler ( p_class ) )
return ; // doesn't want to be deleted
2019-01-03 12:56:41 +08:00
if ( ! __has_trivial_destructor ( T ) )
p_class - > ~ T ( ) ;
2014-02-10 09:10:30 +08:00
A : : free ( p_class ) ;
}
2017-03-05 23:44:50 +08:00
# define memdelete_notnull(m_v) \
{ \
if ( m_v ) memdelete ( m_v ) ; \
}
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
# define memnew_arr(m_class, m_count) memnew_arr_template<m_class>(m_count)
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
template < typename T >
T * memnew_arr_template ( size_t p_elements , const char * p_descr = " " ) {
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
if ( p_elements = = 0 )
2014-02-10 09:10:30 +08:00
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
2017-01-08 05:25:37 +08:00
same strategy used by std : : vector , and the PoolVector class , so it should be safe . */
2014-02-10 09:10:30 +08:00
size_t len = sizeof ( T ) * p_elements ;
2017-03-05 23:44:50 +08:00
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 ;
2017-01-06 21:15:44 +08:00
2019-01-03 12:56:41 +08:00
if ( ! __has_trivial_constructor ( T ) ) {
T * elems = ( T * ) mem ;
2014-02-10 09:10:30 +08:00
2019-01-03 12:56:41 +08:00
/* call operator new */
for ( size_t i = 0 ; i < p_elements ; i + + ) {
new ( & elems [ i ] , sizeof ( T ) , p_descr ) T ;
}
2014-02-10 09:10:30 +08:00
}
2017-03-05 23:44:50 +08:00
return ( T * ) mem ;
2014-02-10 09:10:30 +08:00
}
/**
* Wonders of having own array functions , you can actually check the length of
* an allocated - with memnew_arr ( ) array
*/
2017-03-05 23:44:50 +08:00
template < typename T >
2014-02-10 09:10:30 +08:00
size_t memarr_len ( const T * p_class ) {
2017-03-05 23:44:50 +08:00
uint64_t * ptr = ( uint64_t * ) p_class ;
return * ( ptr - 1 ) ;
2014-02-10 09:10:30 +08:00
}
2017-03-05 23:44:50 +08:00
template < typename T >
2014-02-10 09:10:30 +08:00
void memdelete_arr ( T * p_class ) {
2017-03-05 23:44:50 +08:00
uint64_t * ptr = ( uint64_t * ) p_class ;
2017-01-06 21:15:44 +08:00
2019-01-03 12:56:41 +08:00
if ( ! __has_trivial_destructor ( T ) ) {
uint64_t elem_count = * ( ptr - 1 ) ;
2014-02-10 09:10:30 +08:00
2019-01-03 12:56:41 +08:00
for ( uint64_t i = 0 ; i < elem_count ; i + + ) {
p_class [ i ] . ~ T ( ) ;
}
}
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
Memory : : free_static ( ptr , true ) ;
2014-02-10 09:10:30 +08:00
}
struct _GlobalNil {
int color ;
_GlobalNil * right ;
_GlobalNil * left ;
_GlobalNil * parent ;
_GlobalNil ( ) ;
} ;
struct _GlobalNilClass {
static _GlobalNil _nil ;
} ;
# endif