home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 322_01 / test.c < prev    next >
C/C++ Source or Header  |  1990-08-06  |  2KB  |  70 lines

  1. /* Test program for dynamic memory allocation leak trace tool.  By Mike
  2.    Schwartz, 3-20-87. */
  3.  
  4. #include "btree.h"
  5. #include <stdio.h>
  6.  
  7. main()
  8. {
  9.     int size, n;
  10.     extern int MalTraceEnbld;
  11.     char buf[10];
  12.     char *p, *malloc();
  13.     extern BTREE MalBtree;
  14.  
  15.     MalTraceEnbld = 0;    /* Only turn on malloc info printing/checking
  16.                    for direct calls (not for prints, etc.) */
  17.     do {
  18.         fputs("m/f/p/b: ", stdout);
  19.         gets(buf);
  20.         switch (buf[0]) {
  21.             case 'm':    /* interactive malloc */
  22.                 fputs("\t# bytes: ", stdout);
  23.                 scanf("%d", &size);
  24.                 gets(buf);    /* throw away CR */
  25.                 MalTraceEnbld = 1;
  26.                 p = malloc(size);
  27.                 MalTraceEnbld = 0;
  28.                 printf("\tmalloc returned x%x\n", p);
  29.                 break;
  30.  
  31.             case 'f':    /* interactive free */
  32.                 fputs("\taddr: ", stdout);
  33.                 scanf("%x", &p);
  34.                 gets(buf);    /* throw away CR */
  35.                 MalTraceEnbld = 1;
  36.                 free(p);
  37.                 MalTraceEnbld = 0;
  38.                 break;
  39.  
  40.                         case 'p':    /* print pending (not yet freed)
  41.                                            mallocs.  If n > 0, print (at
  42.                                            most) the first n entries.  If n
  43.                                            == 0, print all entries.  If n <
  44.                                            0, print (at most) the last -n
  45.                                            entries */
  46.                 fputs("\tnumber of mallocs to print: ", stdout);
  47.                 scanf("%d", &n);
  48.                 gets(buf);    /* throw away CR */
  49.                 MalTraceEnbld = 1;
  50.                 PMal(n);
  51.                 fflush(stdout);
  52.                 MalTraceEnbld = 0;
  53.                 break;
  54.             
  55.             case 'b':    /* print btree of pending mallocs */
  56.                 puts("btree of pending mallocs:");
  57.                 ShowTree(MalBtree, 0);
  58.                 break;
  59.  
  60.             default:
  61.                 puts("Invalid command; choose one of:");
  62.                 puts("\tm to malloc");
  63.                 puts("\tf to free");
  64.                 puts("\tp to print pending (not yet freed) mallocs");
  65.                 puts("\t(b to print the btree associated with pending mallocs)");
  66.                 break;
  67.         }
  68.     } while(1);
  69. }
  70.