rtems_task Task_1_through_5( rtems_task_argument argument ) { int i; unsigned int passes = 0; rtems_id tid; rtems_time_of_day time; rtems_status_code status; unsigned char *mem_ptr; int mem_amt; status = rtems_task_ident( RTEMS_SELF, RTEMS_SEARCH_ALL_NODES, &tid ); directive_failed( status, "rtems_task_ident" ); while (TRUE) { bool malloc_walk_ok; if ( passes++ > NUM_PASSES ) { TEST_END(); rtems_test_exit(0); } status = rtems_clock_get_tod( &time ); directive_failed( status, "rtems_clock_get_tod" ); put_name( Task_name[ task_number( tid ) ], FALSE ); print_time( " - rtems_clock_get_tod - ", &time, "\n" ); mem_amt = ((int)((float)rand()*1000.0/(float)RAND_MAX)); while (!(mem_ptr = malloc ( mem_amt))) { printf("out of memory... trying again.\n"); mem_amt = ((int)((float)rand()*1000.0/(float)RAND_MAX)); } printf("mallocing %d bytes\n",mem_amt); memset( mem_ptr, mem_amt, mem_amt ); malloc_report_statistics(); malloc_walk_ok = malloc_walk( 1, false ); rtems_test_assert( malloc_walk_ok ); status = rtems_task_wake_after( task_number( tid ) * 1 * rtems_clock_get_ticks_per_second()/4 ); for (i=0; i < mem_amt; i++) { if ( mem_ptr[i] != (mem_amt & 0xff)) { printf("failed %d, %d, 0x%x, 0x%x\n",i,mem_amt,mem_ptr[i],mem_amt&0xff); rtems_test_exit(1); } } directive_failed( status, "rtems_task_wake_after" ); free( mem_ptr ); } }
/* * A simple test of realloc */ static void test_realloc(void) { void *p1, *p2, *p3, *p4; size_t i; int sc; bool malloc_walk_ok; /* Test growing reallocation "in place" */ p1 = malloc(1); for (i=2 ; i<2048 ; i++) { p2 = realloc(p1, i); if (p2 != p1) printf( "realloc - failed grow in place: " "%p != realloc(%p,%zu)\n", p1, p2, i); p1 = p2; } free(p1); /* Test shrinking reallocation "in place" */ p1 = malloc(2048); for (i=2047 ; i>=1; i--) { p2 = realloc(p1, i); if (p2 != p1) printf( "realloc - failed shrink in place: " "%p != realloc(%p,%zu)\n", p1, p2, i); p1 = p2; } free(p1); /* Test realloc that should fail "in place", i.e., * fallback to free()-- malloc() */ p1 = malloc(32); p2 = malloc(32); p3 = realloc(p1, 64); if (p3 == p1 || p3 == NULL) printf( "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3); free(p3); free(p2); /* * Yet another case */ p1 = malloc(8); p2 = malloc(8); free(p1); sc = posix_memalign(&p1, 16, 32); if (!sc) free(p1); /* * Allocate with default alignment coverage */ sc = rtems_memalign( &p4, 0, 8 ); if ( !sc && p4 ) free( p4 ); /* * Walk the C Program Heap */ puts( "malloc_walk - normal path" ); malloc_walk_ok = malloc_walk( 1234, false ); rtems_test_assert( malloc_walk_ok ); puts( "malloc_walk - in critical section path" ); _Thread_Dispatch_disable(); malloc_walk_ok = malloc_walk( 1234, false ); rtems_test_assert( malloc_walk_ok ); _Thread_Dispatch_enable( _Per_CPU_Get() ); /* * Realloc with a bad pointer to force a point */ p4 = realloc( test_realloc, 32 ); p4 = _realloc_r( NULL, NULL, 1 ); }