void bsp_libc_init( void *heap_begin, uintptr_t heap_size, size_t sbrk_amount ) { RTEMS_Malloc_Initialize( heap_begin, heap_size, sbrk_amount ); /* * Init the RTEMS libio facility to provide UNIX-like system * calls for use by newlib (ie: provide open, close, etc) * Uses malloc() to get area for the iops, so must be after malloc init */ if (rtems_libio_init_helper) (*rtems_libio_init_helper)(); /* * Set up for the libc handling. */ libc_init(); }
rtems_task Init( rtems_task_argument argument ) { Heap_Control *real_heap; Heap_Area area; void *p; puts( "\n\n*** TEST MALLOC 04 ***" ); /* Safe information on real heap */ real_heap = malloc_get_heap_pointer(); malloc_set_heap_pointer( &TempHeap ); rtems_heap_set_sbrk_amount( 0 ); puts( "No sbrk() amount" ); sbrk_count = 0; offset = 256; area.begin = &Malloc_Heap [0]; area.size = offset; RTEMS_Malloc_Initialize( &area, 1, NULL ); errno = 0; p = malloc( 256 ); rtems_test_assert( p == NULL ); rtems_test_assert( errno == ENOMEM ); rtems_test_assert( sbrk_count == 0 ); rtems_heap_set_sbrk_amount( 256 ); puts( "Misaligned extend" ); sbrk_count = 0; offset = 256; area.begin = &Malloc_Heap [0]; area.size = offset; RTEMS_Malloc_Initialize( &area, 1, NULL ); p = malloc(1); rtems_test_assert( p != NULL ); rtems_test_assert( sbrk_count == 0 ); p = malloc(257); rtems_test_assert( p != NULL ); rtems_test_assert( sbrk_count == 1 ); puts( "Not enough sbrk() space" ); sbrk_count = 0; offset = 256; area.begin = &Malloc_Heap [0]; area.size = offset; RTEMS_Malloc_Initialize( &area, 1, NULL ); errno = 0; p = malloc( sizeof( Malloc_Heap ) ); rtems_test_assert( p == NULL ); rtems_test_assert( errno == ENOMEM ); rtems_test_assert( sbrk_count == 1 ); puts( "Valid heap extend" ); sbrk_count = 0; offset = 256; area.begin = &Malloc_Heap [0]; area.size = offset; RTEMS_Malloc_Initialize( &area, 1, NULL ); p = malloc( 128 ); rtems_test_assert( p != NULL ); rtems_test_assert( sbrk_count == 0 ); p = malloc( 128 ); rtems_test_assert( p != NULL ); rtems_test_assert( sbrk_count == 1 ); puts( "Invalid heap extend" ); sbrk_count = -1; offset = 256; area.begin = &Malloc_Heap [0]; area.size = offset; RTEMS_Malloc_Initialize( &area, 1, NULL ); errno = 0; p = malloc( 256 ); rtems_test_assert( p == NULL ); rtems_test_assert( errno == ENOMEM ); rtems_test_assert( sbrk_count == 2 ); /* Restore information on real heap */ malloc_set_heap_pointer( real_heap ); puts( "*** END OF TEST MALLOC 04 ***" ); rtems_test_exit(0); }