File indexing completed on 2025-05-11 08:24:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #ifdef HAVE_CONFIG_H
0035 #include "config.h"
0036 #endif
0037
0038 #include <rtems.h>
0039 #include <rtems/malloc.h>
0040 #include <rtems/score/apimutex.h>
0041 #include <rtems/score/heapimpl.h>
0042 #include <rtems/score/sysstate.h>
0043
0044 static Heap_Control cache_coherent_heap_instance;
0045
0046 static Heap_Control *cache_coherent_heap;
0047
0048 void *rtems_cache_coherent_allocate(
0049 size_t size,
0050 uintptr_t alignment,
0051 uintptr_t boundary
0052 )
0053 {
0054 void *ptr;
0055 Heap_Control *heap;
0056
0057 _RTEMS_Lock_allocator();
0058
0059 heap = cache_coherent_heap;
0060 if ( heap == NULL ) {
0061 heap = RTEMS_Malloc_Heap;
0062 }
0063
0064 ptr = _Heap_Allocate_aligned_with_boundary(
0065 heap,
0066 size,
0067 alignment,
0068 boundary
0069 );
0070
0071 _RTEMS_Unlock_allocator();
0072
0073 return ptr;
0074 }
0075
0076 void rtems_cache_coherent_free( void *ptr )
0077 {
0078 Heap_Control *heap;
0079
0080 _RTEMS_Lock_allocator();
0081
0082 heap = cache_coherent_heap;
0083 if ( heap != NULL ) {
0084 if ( _Heap_Free( heap, ptr ) ) {
0085 heap = NULL;
0086 } else {
0087 heap = RTEMS_Malloc_Heap;
0088 }
0089 } else {
0090 heap = RTEMS_Malloc_Heap;
0091 }
0092
0093 if ( heap != NULL ) {
0094 _Heap_Free( heap, ptr );
0095 }
0096
0097 _RTEMS_Unlock_allocator();
0098 }
0099
0100 static rtems_status_code add_area(
0101 void *area_begin,
0102 uintptr_t area_size
0103 )
0104 {
0105 Heap_Control *heap = cache_coherent_heap;
0106
0107 if ( heap == NULL ) {
0108 heap = &cache_coherent_heap_instance;
0109
0110 if ( _Heap_Initialize( heap, area_begin, area_size, 0 ) == 0 ) {
0111 return RTEMS_UNSATISFIED;
0112 }
0113 cache_coherent_heap = heap;
0114 } else {
0115 if (_Heap_Extend( heap, area_begin, area_size, 0 ) == 0) {
0116 return RTEMS_UNSATISFIED;
0117 }
0118 }
0119 return RTEMS_SUCCESSFUL;
0120 }
0121
0122 rtems_status_code rtems_cache_coherent_add_area(
0123 void *area_begin,
0124 uintptr_t area_size
0125 )
0126 {
0127 rtems_status_code sc;
0128 bool needs_locking = _System_state_Is_up( _System_state_Get());
0129
0130 if ( needs_locking ) {
0131 _RTEMS_Lock_allocator();
0132 }
0133
0134 sc = add_area( area_begin, area_size );
0135
0136 if ( needs_locking ) {
0137 _RTEMS_Unlock_allocator();
0138 }
0139
0140 return sc;
0141 }