2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								/**************************************************************************/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/*  memory.h                                                              */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/**************************************************************************/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/*                         This file is part of:                          */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/*                             GODOT ENGINE                               */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/*                        https://godotengine.org                         */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/**************************************************************************/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/*                                                                        */  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* 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 00:50:27 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# ifndef MEMORY_H 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# define MEMORY_H 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-07 19:33:38 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  "core/error/error_macros.h" 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  "core/templates/safe_refcount.h" 
  
						 
					
						
							
								
									
										
										
										
											2018-09-11 18:13:45 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# include  <stddef.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-08-11 13:09:54 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <new> 
  
						 
					
						
							
								
									
										
										
										
											2022-08-04 13:53:03 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <type_traits> 
  
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class  Memory  {  
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# ifdef DEBUG_ENABLED 
  
						 
					
						
							
								
									
										
										
										
											2021-02-10 19:22:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									static  SafeNumeric < uint64_t >  mem_usage ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  SafeNumeric < uint64_t >  max_usage ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# endif 
  
						 
					
						
							
								
									
										
										
										
											2017-07-11 14:26:34 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-10 19:22:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									static  SafeNumeric < uint64_t >  alloc_count ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								public :  
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Alignment:  ↓ max_align_t        ↓ uint64_t          ↓ max_align_t
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//             ┌─────────────────┬──┬────────────────┬──┬───────────...
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//             │ uint64_t        │░░│ uint64_t       │░░│ T[]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//             │ alloc size      │░░│ element count  │░░│ data
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//             └─────────────────┴──┴────────────────┴──┴───────────...
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Offset:     ↑ SIZE_OFFSET        ↑ ELEMENT_OFFSET    ↑ DATA_OFFSET
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  constexpr  size_t  SIZE_OFFSET  =  0 ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  constexpr  size_t  ELEMENT_OFFSET  =  ( ( SIZE_OFFSET  +  sizeof ( uint64_t ) )  %  alignof ( uint64_t )  = =  0 )  ?  ( SIZE_OFFSET  +  sizeof ( uint64_t ) )  :  ( ( SIZE_OFFSET  +  sizeof ( uint64_t ) )  +  alignof ( uint64_t )  -  ( ( SIZE_OFFSET  +  sizeof ( uint64_t ) )  %  alignof ( uint64_t ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  constexpr  size_t  DATA_OFFSET  =  ( ( ELEMENT_OFFSET  +  sizeof ( uint64_t ) )  %  alignof ( max_align_t )  = =  0 )  ?  ( ELEMENT_OFFSET  +  sizeof ( uint64_t ) )  :  ( ( ELEMENT_OFFSET  +  sizeof ( uint64_t ) )  +  alignof ( max_align_t )  -  ( ( ELEMENT_OFFSET  +  sizeof ( uint64_t ) )  %  alignof ( max_align_t ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03: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 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-30 19:30:54 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									//	                            ↓ return value of alloc_aligned_static
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//	┌─────────────────┬─────────┬─────────┬──────────────────┐
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//	│ padding (up to  │ uint32_t│ void*   │ padding (up to   │
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//	│ p_alignment - 1)│ offset  │ p_bytes │ p_alignment - 1) │
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//	└─────────────────┴─────────┴─────────┴──────────────────┘
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// alloc_aligned_static will allocate p_bytes + p_alignment - 1 + sizeof(uint32_t) and
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// then offset the pointer until alignment is satisfied.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// This offset is stored before the start of the returned ptr so we can retrieve the original/real
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// start of the ptr in order to free it.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The rest is wasted as padding in the beginning and end of the ptr. The sum of padding at
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// both start and end of the block must add exactly to p_alignment - 1.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// p_alignment MUST be a power of 2.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  void  * alloc_aligned_static ( size_t  p_bytes ,  size_t  p_alignment ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  void  * realloc_aligned_static ( void  * p_memory ,  size_t  p_bytes ,  size_t  p_prev_bytes ,  size_t  p_alignment ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Pass the ptr returned by alloc_aligned_static to free it.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// e.g.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//	void *data = realloc_aligned_static( bytes, 16 );
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									//  free_aligned_static( data );
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  void  free_aligned_static ( void  * p_memory ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-25 02:29:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									static  uint64_t  get_mem_available ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  uint64_t  get_mem_usage ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  uint64_t  get_mem_max_usage ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class  DefaultAllocator  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public :  
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									_FORCE_INLINE_  static  void  * alloc ( size_t  p_memory )  {  return  Memory : : alloc_static ( p_memory ,  false ) ;  } 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-01 12:59:42 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									_FORCE_INLINE_  static  void  free ( void  * p_ptr )  {  Memory : : free_static ( p_ptr ,  false ) ;  } 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03: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
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 08:46:53 +01: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-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# define memalloc(m_size) Memory::alloc_static(m_size) 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size) 
  
						 
					
						
							
								
									
										
										
										
											2021-07-09 14:41:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# define memfree(m_mem) Memory::free_static(m_mem) 
  
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-12 18:27:48 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_ALWAYS_INLINE_  void  postinitialize_handler ( void  * )  { }  
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
									
										
										
										
											2015-06-12 18:27:48 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_ALWAYS_INLINE_  T  * _post_initialize ( T  * p_obj )  {  
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									postinitialize_handler ( p_obj ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  p_obj ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-12-11 13:57:25 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# define memnew(m_class) _post_initialize(::new ("") m_class) 
  
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-12-11 13:57:25 +01: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) m_class) 
  
						 
					
						
							
								
									
										
										
										
											2015-06-12 18:27:48 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								_ALWAYS_INLINE_  bool  predelete_handler ( void  * )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  true ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  memdelete ( T  * p_class )  {  
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ( ! predelete_handler ( p_class ) )  { 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return ;  // doesn't want to be deleted
 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2024-02-02 15:43:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  constexpr  ( ! std : : is_trivially_destructible_v < T > )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										p_class - > ~ T ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									Memory : : free_static ( p_class ,  false ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								template  < typename  T ,  typename  A >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  memdelete_allocator ( T  * p_class )  {  
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ( ! predelete_handler ( p_class ) )  { 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return ;  // doesn't want to be deleted
 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2024-02-02 15:43:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  constexpr  ( ! std : : is_trivially_destructible_v < T > )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										p_class - > ~ T ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									A : : free ( p_class ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# define memdelete_notnull(m_v) \ 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{                           \
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( m_v )  {              \
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											memdelete ( m_v ) ;     \
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										}                       \
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# define memnew_arr(m_class, m_count) memnew_arr_template<m_class>(m_count) 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_FORCE_INLINE_  uint64_t  * _get_element_count_ptr ( uint8_t  * p_ptr )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  ( uint64_t  * ) ( p_ptr  -  Memory : : DATA_OFFSET  +  Memory : : ELEMENT_OFFSET ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
									
										
										
										
											2021-08-11 13:09:54 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								T  * memnew_arr_template ( size_t  p_elements )  {  
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ( p_elements  = =  0 )  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 10:15:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  nullptr ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 16:41:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									/** 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
 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 18:06:54 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									same  strategy  used  by  std : : vector ,  and  the  Vector  class ,   so  it  should  be  safe . */ 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									size_t  len  =  sizeof ( T )  *  p_elements ; 
							 
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									uint8_t  * mem  =  ( uint8_t  * ) Memory : : alloc_static ( len ,  true ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 10:15:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									T  * failptr  =  nullptr ;  //get rid of a warning
 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-09 16:11:33 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ERR_FAIL_NULL_V ( mem ,  failptr ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									uint64_t  * _elem_count_ptr  =  _get_element_count_ptr ( mem ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									* ( _elem_count_ptr )  =  p_elements ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-02-02 15:43:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  constexpr  ( ! std : : is_trivially_constructible_v < T > )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										T  * elems  =  ( T  * ) mem ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										/* call operator new */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  ( size_t  i  =  0 ;  i  <  p_elements ;  i + + )  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-12-11 13:57:25 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											: : new  ( & elems [ i ] )  T ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  ( T  * ) mem ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/**
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  Wonders  of  having  own  array  functions ,  you  can  actually  check  the  length  of 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  an  allocated - with  memnew_arr ( )  array 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								size_t  memarr_len ( const  T  * p_class )  {  
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									uint8_t  * ptr  =  ( uint8_t  * ) p_class ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									uint64_t  * _elem_count_ptr  =  _get_element_count_ptr ( ptr ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  * ( _elem_count_ptr ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  memdelete_arr ( T  * p_class )  {  
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									uint8_t  * ptr  =  ( uint8_t  * ) p_class ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-02-02 15:43:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  constexpr  ( ! std : : is_trivially_destructible_v < T > )  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-02-01 10:15:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										uint64_t  * _elem_count_ptr  =  _get_element_count_ptr ( ptr ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										uint64_t  elem_count  =  * ( _elem_count_ptr ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 05:56:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  ( uint64_t  i  =  0 ;  i  <  elem_count ;  i + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											p_class [ i ] . ~ T ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-06 10:15:44 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									Memory : : free_static ( ptr ,  true ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								struct  _GlobalNil  {  
						 
					
						
							
								
									
										
										
										
											2020-05-12 17:01:17 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									int  color  =  1 ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-04 15:06:57 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									_GlobalNil  * right  =  nullptr ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_GlobalNil  * left  =  nullptr ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_GlobalNil  * parent  =  nullptr ; 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-09 22:10:30 -03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_GlobalNil ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								struct  _GlobalNilClass  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									static  _GlobalNil  _nil ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-05-08 10:09:19 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class  DefaultTypedAllocator  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public :  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									template  < typename . . .  Args > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_FORCE_INLINE_  T  * new_allocation ( const  Args  & & . . . p_args )  {  return  memnew ( T ( p_args . . . ) ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_FORCE_INLINE_  void  delete_allocation ( T  * p_allocation )  {  memdelete ( p_allocation ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-25 11:10:34 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# endif  // MEMORY_H