home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / dbcopy / memtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.4 KB  |  305 lines

  1. /*
  2.  *  mstress.c
  3.  *
  4.  *    Malloc stress test.
  5.  *
  6.  */
  7.  
  8. #ifndef MAX_STRESS
  9. #define MAX_STRESS  1
  10. #endif
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. /* Hex pattern is - deadbadbeeff                    */
  16. char    fillpatt[] = { 0xde, 0xad, 0xba, 0xdb, 0xee, 0xff };
  17. char    fillpatt2[] = { 0xfe, 0xef, 0xaa };
  18. char    * mmwatch = 0;
  19.  
  20. mmwatchhitset(){}        /* Symbol to set break when mmwatch is fill()ed*/
  21. mmwatchhitcheck(){}        /* Symbol to set break when mmwatch is check()ed*/
  22.         
  23. dofill( char * p, int s, char * fillpatt, int fpsize )
  24. {
  25. #if MAX_STRESS
  26.     int        i;
  27.     int        j = 0;
  28.  
  29.     if ( ! p ) {
  30.     return;
  31.     }
  32.  
  33.     for ( i = 0; i < s; i ++ ) {
  34.     p[ i ] = fillpatt[ j ];
  35.  
  36.     if ( mmwatch == p + i ) {
  37.         mmwatchhitset();
  38.     }
  39.  
  40.     j ++;
  41.     if ( j >= fpsize ) {
  42.         j = 0;
  43.     }
  44.     }
  45. #endif
  46. }
  47.  
  48. fill( char * p, int s )
  49. {
  50.     dofill( p, s, fillpatt, sizeof( fillpatt ) );
  51. }
  52.  
  53. fill2( char * p, int s )
  54. {
  55.     dofill( p, s, fillpatt2, sizeof( fillpatt2 ) );
  56. }
  57.  
  58.  
  59. /* Dump a buffer in the following style                        */
  60. /* 00 00 00 00 - 00 00 00 00 - 00 00 00 00 - 00 00 00 00    ................*/
  61.  
  62. void dumpbytes( int count, unsigned char * p_buf )   
  63. {
  64.     int            i;
  65.     int            j;
  66.     unsigned char * p_tmp;
  67.     
  68.     printf( "Count = %d\n", count );
  69.         
  70.         
  71.     for ( i = 0, p_tmp = p_buf; i < count; i += 16, p_tmp += 16 ) {
  72.      
  73.     for ( j = i; j < ( i + 16 ); j ++ ) {
  74.  
  75.         if ( ( j - i ) && ( ( ( j - i ) & 0x3 ) == 0 ) ) {
  76.         printf( "- " );
  77.         }
  78.     
  79.         if ( j < count ) {
  80.         printf( "%02x ", p_tmp[ j - i ] );
  81.         } else {
  82.         printf( "   " );
  83.         }
  84.     }
  85.  
  86.     printf( "    " );
  87.     for ( j = i; j < ( i + 16 ); j ++ ) {
  88.     
  89.         if ( j < count ) {
  90.         if ( isprint( p_tmp[ j - i ] ) ) {
  91.             printf( "%c", p_tmp[ j - i ] );
  92.         } else {
  93.             printf( "." );
  94.         }
  95.         } else {
  96.         printf( " " );
  97.         }
  98.         
  99.     }       
  100.     printf( "\n" );
  101.     
  102.     }     
  103.         
  104. }            
  105.  
  106. docheckfill( char * p, int s, char * fillpatt, int fpsize, int id )
  107. {
  108. #if MAX_STRESS
  109.     int        i;
  110.     int        j = 0;
  111.     int        z;
  112.     char    * x;
  113.     
  114.     if ( ! p ) {
  115.     return;
  116.     }
  117.  
  118.     for ( i = 0; i < s; i ++ ) {
  119.     if ( p[ i ] != fillpatt[ j ] ) {
  120.  
  121.         printf( "memory corrupted by memcpy at addr 0x%08x\n", p + i );
  122.         printf( "addr of block 0x%08x, corrupted at offset %d\n", p, i );
  123.         printf(
  124.         "memory allocation id %d - version %d\n", id, id
  125.         );
  126.         doagain();
  127. /*        dumpbytes( s, ( unsigned char * ) p ); */
  128.         /* Lets dump core, the hard way                */
  129.         x = * ( char ** ) ( p + i );    /* possibly bus error here    */
  130.         z = * x;        /* possibly segv here            */
  131.         x = 0;
  132.         * x = 0;        /* Segv here for sure            */
  133.     }
  134.  
  135.     if ( mmwatch == p + i ) {
  136.         mmwatchhitcheck();
  137.     }
  138.  
  139.     j ++;
  140.     if ( j >= fpsize ) {
  141.         j = 0;
  142.     }
  143.     }
  144. #endif    
  145. }
  146.  
  147. printbuf( char * p, int s )
  148. {
  149.     printf( "addr of block 0x%08x, size %d\n", p, s );
  150.     dumpbytes( s, ( unsigned char * ) p );
  151. }
  152.  
  153. checkfill( char * p, int s, int id )
  154. {
  155.     docheckfill( p, s, fillpatt, sizeof( fillpatt ), id );
  156. }
  157.  
  158. checkfill2( char * p, int s, int id )
  159. {
  160.     docheckfill( p, s, fillpatt2, sizeof( fillpatt2 ), id );
  161. }
  162.  
  163. volatile int    watch_allocate = -1;
  164. volatile int    watch_version = -1;
  165. int        watch_last_id;
  166.  
  167. watch_break(){}
  168. watch_set( int wa, int wv )
  169. {
  170.     watch_allocate = wa;
  171.     watch_version = wv;
  172.     return wa;
  173. }
  174.  
  175. watch_look( int wa, int wv )
  176. {
  177.     if (
  178.     ( watch_allocate == wa )
  179.     && ( watch_version == wv )
  180.     ) {
  181.     watch_break();
  182.     }
  183. }
  184.  
  185. #define ONEMB    /*(1024*1024)*/ 1000
  186.  
  187. double        kk;
  188. char        mem[ ONEMB * 2 ];
  189. double        jj;
  190. double        jj1;
  191. char        mem2[ ONEMB * 2 ];
  192. double        jj1;
  193.  
  194. unsigned int        i;
  195. unsigned int        k;
  196. unsigned int        j;
  197. unsigned int        l;
  198.  
  199. ps()
  200. {
  201.     printbuf( mem + j, l );
  202. }
  203.  
  204. pd()
  205. {
  206.     printbuf( mem + k, l );
  207. }
  208.  
  209. doagain()
  210. {
  211.     printf( " src %d  dst %d  len %d\n", j, k, l );
  212.  
  213.     if ( ( j & 7 ) == ( k & 7 ) ) {
  214.     printf( "Double aligned with %d padding at start\n", 8 - ( j & 7 ) );
  215.     printf( "               and  %d padding at end\n", ( (j+l) & 7 ) );
  216.     } else if (( j & 3 ) == ( k & 3 ) ) {
  217.     printf( "Word aligned with %d padding at start\n", 4 - ( j & 3 ) );
  218.     printf( "             and  %d padding at end\n", ( (j+l) & 3 ) );
  219.     } else {
  220.     printf( "Not aligned src w %d padding at start\n", 4 - ( j & 3 ) );
  221.     printf( "             and  %d padding at end\n", ( (j+l) & 3 ) );
  222.     
  223.     }
  224.  
  225.     if ( ( j < k ) && ( ( j + l ) > k ) ) {
  226.     printf( "Backward copy\n" );
  227.     } else {
  228.     printf( "Forward copy\n" );
  229.     }
  230.     
  231.     fill2( mem + j, l );
  232.     ps();
  233.     memcpy( mem + k, mem + j, l );
  234.     pd();
  235. }
  236.  
  237.  
  238. cpy( char * dst, char * src, int i )
  239. {
  240.     while ( i ) {
  241.     * dst = * src;
  242.     i --;
  243.     src ++;
  244.     dst ++;
  245.     }
  246. }
  247.  
  248. check( char * dst, char * src, int i )
  249. {
  250.     while ( i ) {
  251.     if ( * dst != * src ) {
  252.         printf( "memcpy wrote over borders at addr 0x%08x\n", src );
  253.         doagain();
  254.     }
  255.     i --;
  256.     src ++;
  257.     dst ++;
  258.     }
  259. }
  260.  
  261. main()
  262. {
  263.  
  264. #define NMEMS        1000000
  265.  
  266.     for ( i = 0; i < NMEMS; i ++ ) {
  267.  
  268.     j = rand() % ONEMB;
  269.     k = rand() % ONEMB;
  270.     l = rand() % ONEMB;
  271.  
  272.     fill( mem + k - 4, l + 8 );
  273.     fill2( mem + j, l );
  274.     cpy( mem2 + k - 4, mem + k - 4, 4 );
  275.     cpy( mem2 + k + l, mem + k + l, 4 );
  276.     
  277.     watch_look( i, 0 );
  278.  
  279.     memcpy( mem + k, mem + j, l );
  280.  
  281.     checkfill2( mem + k, l, l );
  282.     check( mem2 + k - 4, mem + k - 4, 4 );
  283.     check( mem2 + k + l, mem + k + l, 4 );
  284.  
  285.     j = rand() % ONEMB;
  286.     k = rand() % ONEMB;
  287.     l = rand() % ONEMB;
  288.  
  289.     fill2( mem + k - 4, l + 8 );
  290.     fill( mem + j, l );
  291.     cpy( mem2 + k - 4, mem + k - 4, 4 );
  292.     cpy( mem2 + k + l, mem + k + l, 4 );
  293.  
  294.     watch_look( i, 1 );
  295.     memcpy( mem + k, mem + j, l );
  296.  
  297.     checkfill( mem + k, l, l );
  298.     check( mem2 + k - 4, mem + k - 4, 4 );
  299.     check( mem2 + k + l, mem + k + l, 4 );
  300.  
  301.     }
  302.  
  303. }
  304.  
  305.