home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_02 / v7n2046c.txt < prev    next >
Text File  |  1988-11-21  |  4KB  |  194 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.  
  5. char *vmalloc();
  6. void vfree();
  7. void vverify();
  8. char *vrealloc();
  9. char *vcalloc();
  10. void vdump();
  11.  
  12. /* maximum number of separate buffers which have been
  13.    allocated but not freed */
  14. #define MAXMALLOCS 200
  15. #define KP 0xaa     /* definition of known pattern */
  16. #define KPW 2       /* definition of known pattern width */
  17.  
  18. typedef unsigned size_t;
  19.  
  20. static void trace();
  21.  
  22. static int nummallocs = 0;
  23. struct mtype {
  24.     unsigned char *addr;
  25.     int size;
  26. };
  27. static struct mtype m[MAXMALLOCS];
  28.  
  29. static void dumpbuf(x)
  30. int x;
  31. {
  32.     unsigned char *c;
  33.     char s[80];
  34.     c = (unsigned char *)m[x].addr - 2;
  35.     /* dump malloc buffer to the vmalloc file */
  36.     while (c <= m[x].addr + m[x].size + KPW + KPW - 1) {
  37.         sprintf(s, "%04.4lx : %02x ", (long)c, *c);
  38.         if (c == m[x].addr)
  39.             strcat(s, "<= leading known pattern");
  40.         if (c == m[x].addr + KPW)
  41.             strcat(s, "<= address of malloc buffer");
  42.         if (c == m[x].addr + m[x].size + KPW)
  43.             strcat(s, "<= trailing known pattern");
  44.         strcat(s, "\n");
  45.         trace(s);
  46.         ++c;
  47.     }
  48. }
  49.  
  50. void vverify(id)
  51. char *id;
  52. {
  53.     char s[80];
  54.     int x, c;
  55.     /* verify the entire malloc heap */
  56.     for (x = 0; x < nummallocs; ++x)
  57.         if (m[x].addr != NULL)
  58.             for (c = 0; c < KPW; ++c) {
  59.                 if (*(m[x].addr + c) != KP ||
  60.                   *(m[x].addr + m[x].size + KPW + c) != KP) {
  61.                     sprintf(s,
  62.                       "ERROR: Malloc Area Corrupted (%s)\n",
  63.                       id);
  64.                     trace(s);
  65.                     fputs(s, stderr);
  66.                     dumpbuf(x);
  67.                     exit(1);
  68.                 }
  69.             }
  70. }
  71.  
  72. char *vmalloc(size)
  73. size_t size;
  74. {
  75.     char *buffer, s[80];
  76.     int c, x;
  77.     vverify("vmalloc");
  78.     if ((buffer = calloc(size + KPW + KPW, 1)) == NULL) {
  79.         fprintf(stderr, "ERROR: calloc returned NULL\n");
  80.         trace("ERROR: calloc returned NULL\n");
  81.         exit(1);
  82.     }
  83.     sprintf(s, "%04.4lx:vmalloc  size = %ld\n", (long)buffer,
  84.       (long)size);
  85.     trace(s);
  86.     /* find a place for an entry in m */
  87.     for (x = 0; x < MAXMALLOCS && m[x].addr != NULL; ++x);
  88.     if (x == MAXMALLOCS) {
  89.         sprintf(s, "ERROR: too many mallocs\n");
  90.         fprintf(stderr, s);
  91.         trace(s);
  92.         exit(1);
  93.     }
  94.     m[x].addr = buffer;
  95.     m[x].size = size;
  96.     if (x == nummallocs)
  97.         ++nummallocs;
  98.     for (c = 0; c < KPW; ++c) {
  99.         *(m[x].addr + c) = KP;
  100.         *(m[x].addr + m[x].size + KPW + c) = KP;
  101.     }
  102.     return(buffer + KPW);
  103. }
  104.  
  105. char *vcalloc(n, size)
  106. size_t n, size;
  107. {
  108.     return(vmalloc(n * size));
  109. }
  110.  
  111. void vfree(buffer)
  112. char *buffer;
  113. {
  114.     char *b;
  115.     char s[80];
  116.     int x;
  117.     b = buffer - KPW;
  118.     vverify("vfree");
  119.     for (x = 0; x < nummallocs && m[x].addr != b; ++x);
  120.     if (x == nummallocs) {
  121.         strcpy(s, "ERROR: location to free is not in list\n");
  122.         fprintf(stderr, s);
  123.         trace(s);
  124.         exit(1);
  125.     }
  126.     sprintf(s, "%04.4lx:vfree\n", (long)b);
  127.     trace(s);
  128.     free(b);
  129.     m[x].addr = NULL;
  130.     if (x == nummallocs - 1)
  131.         --nummallocs;
  132. }
  133.  
  134. char *vrealloc(buffer, size)
  135. char *buffer;
  136. size_t size;
  137. {
  138.     char *b, *b2, s[80];
  139.     int c, x;
  140.     b = buffer - KPW;
  141.     vverify("vrealloc");
  142.     for (x = 0; x < nummallocs && m[x].addr != b; ++x);
  143.     if (x == nummallocs) {
  144.         sprintf(s, "ERROR: location to realloc not in list\n");
  145.         fprintf(stderr, s);
  146.         trace("s");
  147.         exit(1);
  148.     }
  149.     sprintf(s, "%04.4lx:vrealloc  size = %ld\n", (long)b,
  150.       (long)size);
  151.     trace(s);
  152.     for (c = 0; c < KPW; ++c)
  153.         *(m[x].addr + m[x].size + KPW + c) = 0;
  154.     b2 = realloc(b, size + KPW + KPW);
  155.     m[x].addr = b2;
  156.     m[x].size = size;
  157.     for (c = 0; c < KPW; ++c)
  158.         *(m[x].addr + m[x].size + KPW + c) = KP;
  159.     return(b2 + KPW);
  160. }
  161.  
  162. void vdump(id)
  163. char *id;
  164. {
  165.     char s[80];
  166.     int x;
  167.     /* dump the entire malloc heap to the vmalloc file */
  168.     sprintf(s,
  169.       "========== Dump of malloc heap (%s) ==========\n", id);
  170.     trace(s);
  171.     for (x = 0; x < nummallocs; ++x)
  172.         if (m[x].addr != NULL) {
  173.             sprintf(s, "===== Malloc buffer addr : %04.4lx\n",
  174.               (long)m[x].addr);
  175.             trace(s);
  176.             sprintf(s, "===== Malloc buffer size : %04x\n",
  177.               m[x].size + KPW + KPW);
  178.             trace(s);
  179.             dumpbuf(x);
  180.         }
  181. }
  182.  
  183. static void trace(s)
  184. char *s;
  185. {
  186.     static FILE *out = NULL;
  187.     if (out == NULL) {
  188.         unlink("vmalloc");
  189.         out = fopen("vmalloc", "w");
  190.         setbuf(out, NULL);
  191.     }
  192.     fputs(s, out);
  193. }
  194.