static void test_heap_extend(void) { bool ret = false; Heap_Control *heap = &TestHeap; uint8_t *area_begin = TestHeapMemory; uint8_t *sub_area_begin; uint8_t *sub_area_end; _Heap_Initialize( heap, area_begin + 768, 256, 0 ); sub_area_begin = (uint8_t *) heap->first_block; sub_area_end = (uint8_t *) heap->first_block->prev_size; puts( "heap extend - link below" ); ret = _Protected_heap_Extend( heap, area_begin + 0, 256 ); test_heap_assert( ret, true ); puts( "heap extend - merge below overlap" ); ret = _Protected_heap_Extend( heap, sub_area_begin - 128, 256 ); test_heap_assert( ret, false ); puts( "heap extend - merge below" ); ret = _Protected_heap_Extend( heap, sub_area_begin - 256, 256 ); test_heap_assert( ret, true ); puts( "heap extend - merge above overlap" ); ret = _Protected_heap_Extend( heap, sub_area_end - 128, 256 ); test_heap_assert( ret, false ); puts( "heap extend - merge above" ); ret = _Protected_heap_Extend( heap, sub_area_end, 256 ); test_heap_assert( ret, true ); puts( "heap extend - link above" ); ret = _Protected_heap_Extend( heap, area_begin + 1536, 256 ); test_heap_assert( ret, true ); puts( "heap extend - area too small" ); ret = _Protected_heap_Extend( heap, area_begin + 2048, 0 ); test_heap_assert( ret, false ); puts( "heap extend - invalid area" ); ret = _Protected_heap_Extend( heap, (void *) -1, 2 ); test_heap_assert( ret, false ); area_begin = (uint8_t *) (((uintptr_t) area_begin) | 1); _Heap_Initialize( heap, area_begin + 768, 256, 0 ); puts( "heap extend - merge below with align up" ); ret = _Protected_heap_Extend( heap, area_begin + 512, 256 ); test_heap_assert( ret, true ); }
/* * Exercise case in heapresize.c around line 125 when new_block_size * < min_block_size */ void test_case_one(void) { uint32_t heap_size; void *ptr1; uintptr_t old; uintptr_t avail; Heap_Resize_status hc; puts( "Init - _Heap_Initialize (for test one) - OK" ); heap_size = _Heap_Initialize( &Heap, Memory, sizeof(Memory), 8 ); printf( "Init - Heap size=%" PRIu32 "\n", heap_size ); rtems_test_assert( heap_size ); puts( "Init - _Heap_Allocate - too large size (overflow)- not OK"); ptr1 = _Heap_Allocate( &Heap, UINTPTR_MAX ); rtems_test_assert( !ptr1 ); puts( "Init - _Heap_Allocate_aligned - OK"); ptr1 = _Heap_Allocate_aligned( &Heap, 64, 32 ); rtems_test_assert( ptr1 ); puts( "Init - _Heap_Resize_block - OK"); hc = _Heap_Resize_block( &Heap, ptr1, 4, &old, &avail ); rtems_test_assert( !hc ); }
rtems_status_code rtems_region_create( rtems_name name, void *starting_address, uintptr_t length, uintptr_t page_size, rtems_attribute attribute_set, rtems_id *id ) { rtems_status_code return_status; Region_Control *the_region; if ( !rtems_is_name_valid( name ) ) return RTEMS_INVALID_NAME; if ( !starting_address ) return RTEMS_INVALID_ADDRESS; if ( !id ) return RTEMS_INVALID_ADDRESS; the_region = _Region_Allocate(); if ( !the_region ) return_status = RTEMS_TOO_MANY; else { _Thread_queue_Initialize( &the_region->Wait_queue ); if ( _Attributes_Is_priority( attribute_set ) ) { the_region->wait_operations = &_Thread_queue_Operations_priority; } else { the_region->wait_operations = &_Thread_queue_Operations_FIFO; } the_region->maximum_segment_size = _Heap_Initialize( &the_region->Memory, starting_address, length, page_size ); if ( !the_region->maximum_segment_size ) { _Region_Free( the_region ); return_status = RTEMS_INVALID_SIZE; } else { the_region->attribute_set = attribute_set; _Objects_Open( &_Region_Information, &the_region->Object, (Objects_Name) name ); *id = the_region->Object.id; return_status = RTEMS_SUCCESSFUL; } } _Objects_Allocator_unlock(); return return_status; }
void bsp_stack_allocate_init(size_t stack_space_size) { _Heap_Initialize( &bsp_stack_heap, bsp_section_stack_begin, (uintptr_t) bsp_section_stack_size, CPU_STACK_ALIGNMENT ); }
static void test_heap_init(uintptr_t page_size ) { uintptr_t rv = 0; memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE ); rv = _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, page_size ); rtems_test_assert( rv > 0 ); }
static void task_stack_init(size_t stack_space_size) { bool ok = _Heap_Initialize( &task_stack_heap, task_stack_space, sizeof(task_stack_space), STACK_HEAP_PAGE_SIZE ); rtems_test_assert(stack_space_size == _CONFIGURE_STACK_SPACE_SIZE); rtems_test_assert(ok); }
static void test_heap_do_initialize( uintptr_t area_size, uintptr_t page_size, uintptr_t success_expected ) { uintptr_t rv = _Heap_Initialize( &TestHeap, TestHeapMemory, area_size, page_size ); if ( success_expected ) { rtems_test_assert( rv > 0 && _Heap_Walk( &TestHeap, 0, false ) ); } else { rtems_test_assert( rv == 0 ); } }
/* * Exercise case in heapallocatealigned.c around line 223 when ... */ void test_case_three(void) { uint32_t heap_size; void *ptr1; #if 0 Heap_Resize_status hc; #endif int pg, al, alloc, sz; puts( "Init - _Heap_Allocate_aligned - request impossible - not OK"); #if 0 heap_size = _Heap_Initialize( &Heap, Memory[32], sizeof(Memory), 1 << 16 ); ptr1 = _Heap_Allocate_aligned( &Heap, 4, 1 << 16 ); ptr1 = _Heap_Allocate_aligned( &Heap, 256, 1 << 16 ); #endif #if 1 for ( sz=32 ; sz <= 80 ; sz+=4 ) { for ( pg=2 ; pg < 12 ; pg++ ) { for ( al=16 ; al >=4 ; al-- ) { for ( alloc=4 ; alloc < sizeof(Memory)/2 ; alloc+=4 ) { heap_size = _Heap_Initialize( &Heap, &Memory[sz], sizeof(Memory)/2, 1 << pg ); if ( heap_size != 0 ) { do { ptr1 = _Heap_Allocate_aligned( &Heap, alloc, 1 <<al ); } while ( ptr1 ); } } } } } #endif }
static void test_heap_extend_allocation_order(void) { Heap_Control *heap = &TestHeap; uintptr_t size = 256; uintptr_t gap = 256; uint8_t *init_area_begin = TestHeapMemory; uint8_t *extend_area_begin = init_area_begin + size + gap; bool ret; uint8_t *p; _Heap_Initialize( heap, init_area_begin, size, 0 ); ret = _Protected_heap_Extend( heap, extend_area_begin, size ); test_heap_assert( ret, true ); p = _Heap_Allocate( heap, 1 ); rtems_test_assert( (uintptr_t) (p - init_area_begin) < size ); }
static void add_area( void *area_begin, uintptr_t area_size ) { Heap_Control *heap = cache_coherent_heap; if ( heap == NULL ) { bool ok; heap = &cache_coherent_heap_instance; ok = _Heap_Initialize( heap, area_begin, area_size, 0 ); if ( ok ) { cache_coherent_heap = heap; } } else { _Heap_Extend( heap, area_begin, area_size, 0 ); } }
/* * Exercise case in heapresize.c around line 140 when next_is_used AND * free_block_size < min_block_size. */ void test_case_two(void) { uint32_t heap_size; void *ptr1; uintptr_t old; uintptr_t avail; Heap_Resize_status hc; puts( "\nInit - _Heap_Initialize (for test two) - OK" ); heap_size = _Heap_Initialize( &Heap, Memory, sizeof(Memory), 8 ); printf( "Init - Heap size=%" PRIu32 "\n", heap_size ); rtems_test_assert( heap_size ); puts( "Init - _Heap_Allocate_aligned - OK"); ptr1 = _Heap_Allocate_aligned( &Heap, 64, 4 ); rtems_test_assert( ptr1 ); puts( "Init - _Heap_Resize_block - OK"); hc = _Heap_Resize_block( &Heap, ptr1, 56, &old, &avail ); rtems_test_assert( !hc ); }
static void test_greedy_allocate(void) { Heap_Control *heap = &TestHeap; uintptr_t block_size = 1; void *p; _Heap_Initialize( heap, &TestHeapMemory[0], sizeof(TestHeapMemory), 0 ); _Heap_Greedy_allocate( heap, &block_size, 1 ); p = _Heap_Allocate( heap, 1 ); rtems_test_assert( p != NULL ); p = _Heap_Allocate( heap, 1 ); rtems_test_assert( p == NULL ); /* The internal allocation fails */ _Heap_Greedy_allocate( heap, &block_size, 1 ); p = _Heap_Allocate( heap, 1 ); rtems_test_assert( p == NULL ); }
static void test_heap_free(void) { Heap_Control *heap = &TestHeap; void *p; Heap_Block *block; bool ok; _Heap_Initialize( heap, &TestHeapMemory[0], sizeof(TestHeapMemory), 0 ); p = _Heap_Allocate( heap, 1 ); rtems_test_assert( p != NULL ); block = _Heap_Block_of_alloc_area( (uintptr_t) p, heap->page_size ); /* * This will kick the next block outside of the heap area and the next * _Heap_Free() will detect this. */ block->size_and_flag += sizeof(TestHeapMemory); ok = _Heap_Free( heap, p ); rtems_test_assert( !ok ); }
/* * _Workspace_Handler_initialization */ void _Workspace_Handler_initialization(void) { uintptr_t memory_available = 0; void *starting_address = rtems_configuration_get_work_space_start(); uintptr_t size = rtems_configuration_get_work_space_size(); if ( rtems_configuration_get_do_zero_of_workspace() ) memset( starting_address, 0, size ); memory_available = _Heap_Initialize( &_Workspace_Area, starting_address, size, CPU_HEAP_ALIGNMENT ); if ( memory_available == 0 ) _Internal_error_Occurred( INTERNAL_ERROR_CORE, true, INTERNAL_ERROR_TOO_LITTLE_WORKSPACE ); }
void _Workspace_Handler_initialization( void *starting_address, size_t size ) { uint32_t *zero_out_array; uint32_t index; uint32_t memory_available; if ( !starting_address || !_Addresses_Is_aligned( starting_address ) ) _Internal_error_Occurred( INTERNAL_ERROR_CORE, TRUE, INTERNAL_ERROR_INVALID_WORKSPACE_ADDRESS ); if ( _CPU_Table.do_zero_of_workspace ) { for( zero_out_array = (uint32_t *) starting_address, index = 0 ; index < size / sizeof( uint32_t ) ; index++ ) zero_out_array[ index ] = 0; } memory_available = _Heap_Initialize( &_Workspace_Area, starting_address, size, CPU_HEAP_ALIGNMENT ); if ( memory_available == 0 ) _Internal_error_Occurred( INTERNAL_ERROR_CORE, TRUE, INTERNAL_ERROR_TOO_LITTLE_WORKSPACE ); }
static void test_heap_default_init(void) { memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE ); _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, 0 ); }
static void test_heap_init_with_page_size( uintptr_t page_size ) { memset( TestHeapMemory, 0xFF, sizeof(TestHeapMemory) ); _Heap_Initialize( &TestHeap, TestHeapMemory, sizeof(TestHeapMemory), page_size ); }
rtems_status_code rtems_region_create( rtems_name name, void *starting_address, uintptr_t length, uintptr_t page_size, rtems_attribute attribute_set, rtems_id *id ) { rtems_status_code return_status; Region_Control *the_region; if ( !rtems_is_name_valid( name ) ) return RTEMS_INVALID_NAME; if ( !starting_address ) return RTEMS_INVALID_ADDRESS; if ( !id ) return RTEMS_INVALID_ADDRESS; _RTEMS_Lock_allocator(); /* to prevent deletion */ the_region = _Region_Allocate(); if ( !the_region ) return_status = RTEMS_TOO_MANY; else { the_region->maximum_segment_size = _Heap_Initialize( &the_region->Memory, starting_address, length, page_size ); if ( !the_region->maximum_segment_size ) { _Region_Free( the_region ); return_status = RTEMS_INVALID_SIZE; } else { the_region->starting_address = starting_address; the_region->length = length; the_region->page_size = page_size; the_region->attribute_set = attribute_set; the_region->number_of_used_blocks = 0; _Thread_queue_Initialize( &the_region->Wait_queue, _Attributes_Is_priority( attribute_set ) ? THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO, STATES_WAITING_FOR_SEGMENT, RTEMS_TIMEOUT ); _Objects_Open( &_Region_Information, &the_region->Object, (Objects_Name) name ); *id = the_region->Object.id; return_status = RTEMS_SUCCESSFUL; } } _RTEMS_Unlock_allocator(); return return_status; }