/* * _Workspace_Allocate_or_fatal_error */ void *_Workspace_Allocate_or_fatal_error( size_t size ) { void *memory; memory = _Heap_Allocate( &_Workspace_Area, size ); #if defined(DEBUG_WORKSPACE) printk( "Workspace_Allocate_or_fatal_error(%d) from %p/%p -> %p\n", size, __builtin_return_address( 0 ), __builtin_return_address( 1 ), memory ); #endif if ( memory == NULL ) _Internal_error_Occurred( INTERNAL_ERROR_CORE, true, INTERNAL_ERROR_WORKSPACE_ALLOCATION ); return memory; }
/* * 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 ); }
static void test_heap_cases_1(void) { void *p1, *p2, *p3; uintptr_t u1, u2; Heap_Resize_status rsc; /* * Another odd case. What we are trying to do from Sergei * * 32-bit CPU when CPU_ALIGNMENT = 4 (most targets have 8) with the * code like this: */ test_heap_default_init(); p1 = _Heap_Allocate( &TestHeap, 12 ); p2 = _Heap_Allocate( &TestHeap, 32 ); p3 = _Heap_Allocate( &TestHeap, 32 ); test_free( p2 ); p2 = _Heap_Allocate_aligned( &TestHeap, 8, 28 ); test_free( p1 ); test_free( p2 ); test_free( p3 ); /* * Odd case in resizing a block. Again test case outline per Sergei */ test_heap_default_init(); p1 = _Heap_Allocate( &TestHeap, 32 ); p2 = _Heap_Allocate( &TestHeap, 8 ); p3 = _Heap_Allocate( &TestHeap, 32 ); test_free( p2 ); rsc = _Heap_Resize_block( &TestHeap, p1, 41, &u1, &u2 ); /* XXX what should we expect */ test_free( p3 ); test_free( p1 ); /* * To tackle a special case of resizing a block in order to cover the * code in heapresizeblock.c * * Re-initialise the heap, so that the blocks created from now on * are contiguous. */ test_heap_default_init(); puts( "Heap Initialized" ); p1 = _Heap_Allocate( &TestHeap, 400 ); rtems_test_assert( p1 != NULL ); p2 = _Heap_Allocate( &TestHeap, 496 ); rtems_test_assert( p2 != NULL ); rsc = _Heap_Resize_block( &TestHeap, p1, 256, &u1, &u2 ); rtems_test_assert( rsc == HEAP_RESIZE_SUCCESSFUL ); test_free( p1 ); test_free( p2 ); }
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 ); }
void *bsp_stack_allocate(size_t size) { void *stack = NULL; if (bsp_stack_heap.area_begin != 0) { stack = _Heap_Allocate(&bsp_stack_heap, size); } if (stack == NULL) { stack = _Workspace_Allocate(size); } return stack; }
Heap_Block *_Heap_Greedy_allocate( Heap_Control *heap, const uintptr_t *block_sizes, size_t block_count ) { Heap_Block *const free_list_tail = _Heap_Free_list_tail( heap ); Heap_Block *allocated_blocks = NULL; Heap_Block *blocks = NULL; Heap_Block *current; size_t i; _Heap_Protection_free_all_delayed_blocks( heap ); for (i = 0; i < block_count; ++i) { void *next = _Heap_Allocate( heap, block_sizes [i] ); if ( next != NULL ) { Heap_Block *next_block = _Heap_Block_of_alloc_area( (uintptr_t) next, heap->page_size ); next_block->next = allocated_blocks; allocated_blocks = next_block; } } while ( (current = _Heap_Free_list_first( heap )) != free_list_tail ) { _Heap_Block_allocate( heap, current, _Heap_Alloc_area_of_block( current ), _Heap_Block_size( current ) - HEAP_BLOCK_HEADER_SIZE ); current->next = blocks; blocks = current; } while ( allocated_blocks != NULL ) { current = allocated_blocks; allocated_blocks = allocated_blocks->next; _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) ); } return blocks; }
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 ); }
void *_Workspace_Allocate( size_t size ) { void *memory; memory = _Heap_Allocate( &_Workspace_Area, size ); #if defined(DEBUG_WORKSPACE) printk( "Workspace_Allocate(%d) from %p/%p -> %p\n", size, __builtin_return_address( 0 ), __builtin_return_address( 1 ), memory ); #endif return memory; }
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 ); }
static void *task_stack_allocate(size_t stack_size) { return _Heap_Allocate(&task_stack_heap, stack_size); }