2022-07-20 13:59:14 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/*
  
						 
					
						
							
								
									
										
										
										
											2024-03-08 13:58:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 *  SPDX - FileCopyrightText :  2015 - 2024  Espressif  Systems  ( Shanghai )  CO  LTD 
							 
						 
					
						
							
								
									
										
										
										
											2022-07-20 13:59:14 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  SPDX - License - Identifier :  Apache - 2.0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 */ 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# pragma once 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <stdlib.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <stdint.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <soc/soc_memory_layout.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  "multi_heap.h" 
  
						 
					
						
							
								
									
										
										
										
											2019-12-18 17:04:49 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  "multi_heap_platform.h" 
  
						 
					
						
							
								
									
										
										
										
											2019-03-14 17:29:32 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  "sys/queue.h" 
  
						 
					
						
							
								
									
										
										
										
											2024-03-08 13:58:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  "esp_attr.h" 
  
						 
					
						
							
								
									
										
										
										
											2017-08-28 17:12:29 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# ifdef __cplusplus 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								extern  " C "  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# endif 
  
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* Some common heap registration data structures used
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   for  heap_caps_init . c  to  share  heap  information  with  heap_caps . c 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-03-11 10:49:51 +11:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# define HEAP_SIZE_MAX (SOC_MAX_CONTIGUOUS_RAM_SIZE) 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								/* Type for describing each registered heap */  
						 
					
						
							
								
									
										
										
										
											2017-08-28 17:12:29 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								typedef  struct  heap_t_  {  
						 
					
						
							
								
									
										
										
										
											2017-07-19 17:10:33 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    uint32_t  caps [ SOC_MEMORY_TYPE_NO_PRIOS ] ;  ///< Capabilities for the type of memory in this heap (as a prioritised set). Copied from soc_memory_types so it's in RAM not flash.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    intptr_t  start ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    intptr_t  end ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-18 17:04:49 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    multi_heap_lock_t  heap_mux ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    multi_heap_handle_t  heap ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-28 17:12:29 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    SLIST_ENTRY ( heap_t_ )  next ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  heap_t ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-28 17:12:29 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/* All registered heaps.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   Forms  a  single  linked  list ,  even  though  most  entries  are  contiguous . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   This  means  at  the  expense  of  4  bytes  per  heap ,  new  heaps  can  be 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   added  at  runtime  in  a  fast  &  thread - safe  way . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								extern  SLIST_HEAD ( registered_heap_ll ,  heap_t_ )  registered_heaps ;  
						 
					
						
							
								
									
										
										
										
											2017-05-03 18:03:28 +10:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-19 17:10:33 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								bool  heap_caps_match ( const  heap_t  * heap ,  uint32_t  caps ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-10 01:14:47 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/* return all possible capabilities (across all priorities) for a given heap */  
						 
					
						
							
								
									
										
										
										
											2024-03-08 13:58:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								FORCE_INLINE_ATTR  uint32_t  get_all_caps ( const  heap_t  * heap )  
						 
					
						
							
								
									
										
										
										
											2018-01-10 01:14:47 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( heap - > heap  = =  NULL )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return  0 ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    uint32_t  all_caps  =  0 ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for  ( int  prio  =  0 ;  prio  <  SOC_MEMORY_TYPE_NO_PRIOS ;  prio + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        all_caps  | =  heap - > caps [ prio ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  all_caps ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2017-09-05 17:29:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-03-08 13:58:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/* Find the heap which belongs to ptr, or return NULL if it's
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   not  in  any  heap . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   ( This  confirms  if  ptr  is  inside  the  heap ' s  region ,  doesn ' t  confirm  if  ' ptr ' 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   is  an  allocated  block  or  is  some  other  random  address  inside  the  heap . ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								FORCE_INLINE_ATTR  heap_t  * find_containing_heap ( void  * ptr  )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    intptr_t  p  =  ( intptr_t ) ptr ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap_t  * heap ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    SLIST_FOREACH ( heap ,  & registered_heaps ,  next )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( heap - > heap  ! =  NULL  & &  p  > =  heap - > start  & &  p  <  heap - > end )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return  heap ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  NULL ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-09-05 17:29:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/*
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 Because  we  don ' t  want  to  add  _another_  known  allocation  method  to  the  stack  of  functions  to  trace  wrt  memory  tracing , 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 18:40:01 +11:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 these  are  declared  private .  The  newlib  malloc ( ) / realloc ( )  implementation  also  calls  these ,  so  they  are  declared 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-05 17:29:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 separately  in  newlib / syscalls . c . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*/  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  * heap_caps_realloc_default ( void  * p ,  size_t  size ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  * heap_caps_malloc_default ( size_t  size ) ;  
						 
					
						
							
								
									
										
										
										
											2023-10-06 16:51:26 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  * heap_caps_aligned_alloc_default ( size_t  alignment ,  size_t  size ) ;  
						 
					
						
							
								
									
										
										
										
											2024-03-08 13:58:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  * heap_caps_realloc_base ( void  * ptr ,  size_t  size ,  uint32_t  caps ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  * heap_caps_calloc_base ( size_t  n ,  size_t  size ,  uint32_t  caps ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  * heap_caps_malloc_base ( size_t  size ,  uint32_t  caps ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  * heap_caps_aligned_alloc_base ( size_t  alignment ,  size_t  size ,  uint32_t  caps ) ;  
						 
					
						
							
								
									
										
										
										
											2017-09-05 17:29:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-28 17:12:29 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# ifdef __cplusplus 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# endif