home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / memtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-06  |  2.0 KB  |  89 lines

  1. /* memtest.c (emx+gcc) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define N 4
  8.  
  9. static char *check[] =
  10. {
  11.   "ok", "empty", "bad begin", "bad node", "bad end", "bad rover"
  12. };
  13.  
  14. int main (void)
  15. {
  16.   char buf[256], *p;
  17.   void *table[N], *v;
  18.   int idx;
  19.   int i, n;
  20.  
  21.   for (i = 0; i < N; ++i)
  22.     table[i] = NULL;
  23.   idx = 0;
  24.   for (;;)
  25.     {
  26.       n = (int)sbrk (0);
  27.       printf ("sbrk = 0x%x\n", n);
  28.       for (i = 0; i < N; ++i)
  29.         {
  30.           if (i == idx) putchar ('*');
  31.           printf ("%d:0x%x  ", i, (int)table[i]);
  32.         }
  33.       printf ("\n? ");
  34.       if (fgets (buf, sizeof (buf), stdin) == NULL)
  35.         return (0);
  36.       p = strchr (buf, '\n');
  37.       if (p != NULL) *p = 0;
  38.       if (buf[0] == '?')
  39.         {
  40.           puts ("?    help");
  41.           puts ("q    quit");
  42.           puts ("f    free");
  43.           puts ("c    check");
  44.           puts ("t    exception");
  45.           puts ("r#   realloc(#)");
  46.           puts ("#    malloc(#)");
  47.           puts ("     next slot");
  48.         }
  49.       else if (buf[0] == 'q')
  50.         return (0);
  51.       else if (buf[0] == 'f')
  52.         {
  53.           printf ("free\n");
  54.           free (table[idx]); table[idx] = NULL;
  55.         }
  56.       else if (buf[0] == 'c')
  57.         printf ("%s\n", check[_heapset ('/')]);
  58.       else if (buf[0] == 't')
  59.         {
  60.           /* provoke exception */
  61.           char *x;
  62.           x = (char *)0x12345678;
  63.           ++*x;
  64.         }
  65.       else if (buf[0] == 0)
  66.         idx = (idx+1)%N;
  67.       else
  68.         {
  69.           p = buf;
  70.           if (*p == 'r')
  71.             ++p;
  72.           i = atoi (p);
  73.           if (buf[0] == 'r')
  74.             v = realloc (table[idx], i);
  75.           else
  76.             v = malloc (i);
  77.           if (v == NULL)
  78.             printf ("%s() failed\n", (buf[0] == 'r' ? "realloc" : "malloc"));
  79.           else
  80.             {
  81.               if ((long)v & 3)
  82.                 printf ("Wrong alignment!\n");
  83.               table[idx] = v;
  84.               memset (v, '*', i);
  85.             }
  86.         }
  87.     }
  88. }
  89.