home *** CD-ROM | disk | FTP | other *** search
- /*
- * mstress.c
- *
- * Malloc stress test.
- *
- */
-
- #ifndef MAX_STRESS
- #define MAX_STRESS 1
- #endif
-
- #include <stdio.h>
- #include <stdlib.h>
-
- /* Hex pattern is - deadbadbeeff */
- char fillpatt[] = { 0xde, 0xad, 0xba, 0xdb, 0xee, 0xff };
- char fillpatt2[] = { 0xfe, 0xef, 0xaa };
- char * mmwatch = 0;
-
- mmwatchhitset(){} /* Symbol to set break when mmwatch is fill()ed*/
- mmwatchhitcheck(){} /* Symbol to set break when mmwatch is check()ed*/
-
- dofill( char * p, int s, char * fillpatt, int fpsize )
- {
- #if MAX_STRESS
- int i;
- int j = 0;
-
- if ( ! p ) {
- return;
- }
-
- for ( i = 0; i < s; i ++ ) {
- p[ i ] = fillpatt[ j ];
-
- if ( mmwatch == p + i ) {
- mmwatchhitset();
- }
-
- j ++;
- if ( j >= fpsize ) {
- j = 0;
- }
- }
- #endif
- }
-
- fill( char * p, int s )
- {
- dofill( p, s, fillpatt, sizeof( fillpatt ) );
- }
-
- fill2( char * p, int s )
- {
- dofill( p, s, fillpatt2, sizeof( fillpatt2 ) );
- }
-
-
- /* Dump a buffer in the following style */
- /* 00 00 00 00 - 00 00 00 00 - 00 00 00 00 - 00 00 00 00 ................*/
-
- void dumpbytes( int count, unsigned char * p_buf )
- {
- int i;
- int j;
- unsigned char * p_tmp;
-
- printf( "Count = %d\n", count );
-
-
- for ( i = 0, p_tmp = p_buf; i < count; i += 16, p_tmp += 16 ) {
-
- for ( j = i; j < ( i + 16 ); j ++ ) {
-
- if ( ( j - i ) && ( ( ( j - i ) & 0x3 ) == 0 ) ) {
- printf( "- " );
- }
-
- if ( j < count ) {
- printf( "%02x ", p_tmp[ j - i ] );
- } else {
- printf( " " );
- }
- }
-
- printf( " " );
- for ( j = i; j < ( i + 16 ); j ++ ) {
-
- if ( j < count ) {
- if ( isprint( p_tmp[ j - i ] ) ) {
- printf( "%c", p_tmp[ j - i ] );
- } else {
- printf( "." );
- }
- } else {
- printf( " " );
- }
-
- }
- printf( "\n" );
-
- }
-
- }
-
- docheckfill( char * p, int s, char * fillpatt, int fpsize, int id )
- {
- #if MAX_STRESS
- int i;
- int j = 0;
- int z;
- char * x;
-
- if ( ! p ) {
- return;
- }
-
- for ( i = 0; i < s; i ++ ) {
- if ( p[ i ] != fillpatt[ j ] ) {
-
- printf( "memory corrupted by memcpy at addr 0x%08x\n", p + i );
- printf( "addr of block 0x%08x, corrupted at offset %d\n", p, i );
- printf(
- "memory allocation id %d - version %d\n", id, id
- );
- doagain();
- /* dumpbytes( s, ( unsigned char * ) p ); */
- /* Lets dump core, the hard way */
- x = * ( char ** ) ( p + i ); /* possibly bus error here */
- z = * x; /* possibly segv here */
- x = 0;
- * x = 0; /* Segv here for sure */
- }
-
- if ( mmwatch == p + i ) {
- mmwatchhitcheck();
- }
-
- j ++;
- if ( j >= fpsize ) {
- j = 0;
- }
- }
- #endif
- }
-
- printbuf( char * p, int s )
- {
- printf( "addr of block 0x%08x, size %d\n", p, s );
- dumpbytes( s, ( unsigned char * ) p );
- }
-
- checkfill( char * p, int s, int id )
- {
- docheckfill( p, s, fillpatt, sizeof( fillpatt ), id );
- }
-
- checkfill2( char * p, int s, int id )
- {
- docheckfill( p, s, fillpatt2, sizeof( fillpatt2 ), id );
- }
-
- volatile int watch_allocate = -1;
- volatile int watch_version = -1;
- int watch_last_id;
-
- watch_break(){}
- watch_set( int wa, int wv )
- {
- watch_allocate = wa;
- watch_version = wv;
- return wa;
- }
-
- watch_look( int wa, int wv )
- {
- if (
- ( watch_allocate == wa )
- && ( watch_version == wv )
- ) {
- watch_break();
- }
- }
-
- #define ONEMB /*(1024*1024)*/ 1000
-
- double kk;
- char mem[ ONEMB * 2 ];
- double jj;
- double jj1;
- char mem2[ ONEMB * 2 ];
- double jj1;
-
- unsigned int i;
- unsigned int k;
- unsigned int j;
- unsigned int l;
-
- ps()
- {
- printbuf( mem + j, l );
- }
-
- pd()
- {
- printbuf( mem + k, l );
- }
-
- doagain()
- {
- printf( " src %d dst %d len %d\n", j, k, l );
-
- if ( ( j & 7 ) == ( k & 7 ) ) {
- printf( "Double aligned with %d padding at start\n", 8 - ( j & 7 ) );
- printf( " and %d padding at end\n", ( (j+l) & 7 ) );
- } else if (( j & 3 ) == ( k & 3 ) ) {
- printf( "Word aligned with %d padding at start\n", 4 - ( j & 3 ) );
- printf( " and %d padding at end\n", ( (j+l) & 3 ) );
- } else {
- printf( "Not aligned src w %d padding at start\n", 4 - ( j & 3 ) );
- printf( " and %d padding at end\n", ( (j+l) & 3 ) );
-
- }
-
- if ( ( j < k ) && ( ( j + l ) > k ) ) {
- printf( "Backward copy\n" );
- } else {
- printf( "Forward copy\n" );
- }
-
- fill2( mem + j, l );
- ps();
- memcpy( mem + k, mem + j, l );
- pd();
- }
-
-
- cpy( char * dst, char * src, int i )
- {
- while ( i ) {
- * dst = * src;
- i --;
- src ++;
- dst ++;
- }
- }
-
- check( char * dst, char * src, int i )
- {
- while ( i ) {
- if ( * dst != * src ) {
- printf( "memcpy wrote over borders at addr 0x%08x\n", src );
- doagain();
- }
- i --;
- src ++;
- dst ++;
- }
- }
-
- main()
- {
-
- #define NMEMS 1000000
-
- for ( i = 0; i < NMEMS; i ++ ) {
-
- j = rand() % ONEMB;
- k = rand() % ONEMB;
- l = rand() % ONEMB;
-
- fill( mem + k - 4, l + 8 );
- fill2( mem + j, l );
- cpy( mem2 + k - 4, mem + k - 4, 4 );
- cpy( mem2 + k + l, mem + k + l, 4 );
-
- watch_look( i, 0 );
-
- memcpy( mem + k, mem + j, l );
-
- checkfill2( mem + k, l, l );
- check( mem2 + k - 4, mem + k - 4, 4 );
- check( mem2 + k + l, mem + k + l, 4 );
-
- j = rand() % ONEMB;
- k = rand() % ONEMB;
- l = rand() % ONEMB;
-
- fill2( mem + k - 4, l + 8 );
- fill( mem + j, l );
- cpy( mem2 + k - 4, mem + k - 4, 4 );
- cpy( mem2 + k + l, mem + k + l, 4 );
-
- watch_look( i, 1 );
- memcpy( mem + k, mem + j, l );
-
- checkfill( mem + k, l, l );
- check( mem2 + k - 4, mem + k - 4, 4 );
- check( mem2 + k + l, mem + k + l, 4 );
-
- }
-
- }
-
-