home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 125.img / PRO-C4.ZIP / BENCH1.ZIP / BENCH / MEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-28  |  7.4 KB  |  342 lines

  1. /* ==( bench/memchk.c )== */
  2.  
  3. /* ----------------------------------------------- */
  4. /* Pro-C  Copyright (C) 1989 - 1990 Vestronix Inc. */
  5. /* Modification to this source is not supported    */
  6. /* by Vestronix Inc.                               */
  7. /*            All Rights Reserved                  */
  8. /* ----------------------------------------------- */
  9. /* Written   BRC  13-Sep-89                        */
  10. /* Modified  Geo  11-Dec-89  See comments below    */
  11. /* ----------------------------------------------- */
  12. /* %W%  (%H% %T%) */
  13.  
  14. /*
  15.  *  Modifications
  16.  *
  17.  *  11-Dec-89  Geo - V2 version
  18. */
  19.  
  20. /* Debugging memory allocater */
  21.  
  22. # include <stdio.h>
  23. # include <string.h>
  24.  
  25. /*
  26.  * Actual debugging section
  27.  *
  28. */
  29. typedef struct mem_st
  30. {
  31.     unsigned       len;
  32.     char          *ptr;
  33.     char          *dup;
  34.     struct mem_st *next;
  35.     long           sum;
  36.     char          *file1;
  37.     int            line1;
  38.     char          *file2;
  39.     int            line2;
  40. } MEM;
  41.  
  42. static MEM *Mem = NULL;
  43. static long RealT = 0L;
  44. static long RealC = 0L;
  45. static long MaxT = 0L;
  46. static long MaxC = 0L;
  47. static long Total = 0L;
  48. static long Count = 0L;
  49.  
  50. extern char *ck_alloc();
  51. extern void  ck_free();
  52. extern void  ck_mem();
  53. extern void  ck_info();
  54. extern char *calloc(), *malloc();
  55.  
  56. char *
  57. ck_alloc(file, line, size)
  58.     char *file;
  59.     int line;
  60.     unsigned size;
  61. {
  62.     char *ptr = NULL;
  63.     MEM *new, *mem = NULL;
  64.  
  65.     ck_mem(__FILE__, __LINE__, "ck_alloc");
  66.  
  67.     if (size <= 0)
  68.     {
  69.         fprintf(stderr, "!!!CK_ALLOC(%s %d): %d is an illegal argument\n",
  70.           file, line, size);
  71.         return(NULL);
  72.     }
  73.  
  74.     size = (size + 1) / 2;
  75.     size *= 2;
  76.     if (!(ptr = calloc(size * 3, sizeof(char))))
  77.     {
  78.         fprintf(stderr, "\n!!!CK_ALLOC(%s %d): calloc(%d,%d) failed!\n",
  79.           file, line, size * 3, sizeof(char));
  80.     }
  81.     else
  82.     {
  83.         memset(ptr, 0xa5, size * 3);
  84.         memset(ptr + size, 0, size);
  85.         RealT += size * 3 * sizeof(char);
  86.         RealC++;
  87.     }
  88.  
  89.     if (!(new = (MEM *)malloc(sizeof(MEM))))
  90.     {
  91.         fprintf(stderr, "\n!!!CK_ALLOC(%s %d): 1st malloc(%d) failed!\n",
  92.           file, line, sizeof(MEM));
  93.     }
  94.     else
  95.     {
  96.         RealT += sizeof(MEM);
  97.         RealC++;
  98.     }
  99.  
  100.     if (!(new->file1 = malloc(strlen(file) + 1)))
  101.     {
  102.         fprintf(stderr, "\n!!!CK_ALLOC(%s %d): 2nd malloc(%d) failed!\n",
  103.           file, line, strlen(file) + 1);
  104.     }
  105.     else
  106.     {
  107.         strcpy(new->file1, file);
  108.         new->line1 = line;
  109.         RealT += strlen(file) + 1;
  110.         RealC++;
  111.     }
  112.  
  113.     new->len = size;
  114.     new->ptr = ptr;
  115.     new->dup = NULL;
  116.  
  117.     if (!Mem || Mem->ptr < ptr)
  118.     {
  119.         new->next = Mem;
  120.         Mem = new;
  121.     }
  122.     else if (Mem->ptr == ptr)
  123.         mem = Mem;
  124.     else
  125.     {
  126.         for (mem = Mem; mem->next && (long)mem->next->ptr > (long)ptr; mem = mem->next)
  127.             ;
  128.         new->next = mem->next;
  129.         mem->next = new;
  130.         mem->sum = (long)mem->len + (long)mem->ptr + (long)mem->dup +
  131.           (long)mem->next;
  132.     }
  133.  
  134.     if (mem && mem->ptr == ptr)
  135.         fprintf(stderr, "\n!!!CK_ALLOC(%s %d): 0x%lx was already allocated!\n",
  136.           file, line, ptr + size);
  137.  
  138.     new->sum = (long)new->len + (long)new->ptr + (long)new->dup +
  139.       (long)new->next;
  140.  
  141.     Total += size * sizeof(char);
  142.     Count++;
  143.  
  144.     if (Count > MaxC)
  145.         MaxC = Count;
  146.     if (Total > MaxT)
  147.         MaxT = Total;
  148.  
  149.     /*ck_info(__FILE__, __LINE__, "Ck_Alloc");*/
  150.  
  151.     return(ptr + size);
  152. }
  153.  
  154.  
  155. void
  156. ck_free(file, line, ptr)
  157.     char *file;
  158.     int line;
  159.     char *ptr;
  160. {
  161.     MEM *mem = NULL;
  162.     int err = 0;
  163.  
  164.     ck_mem(__FILE__, __LINE__, "Ck_Free");
  165.  
  166.     /*ck_info(__FILE__, __LINE__, "Ck_Free");*/
  167.  
  168.     if (ptr && Mem)
  169.     {
  170.         if (Mem->ptr + Mem->len == ptr)
  171.             mem = Mem;
  172.         else
  173.             for (mem = Mem->next; mem && (long)mem->ptr + mem->len > (long)ptr;
  174.               mem = mem->next)
  175.                 ;
  176.         
  177.         if (mem->ptr + mem->len != ptr)
  178.         {
  179.             fprintf(stderr, "\n!!!CK_FREE(%s %d): 0x%lx wasn't allocated!\n",
  180.               file, line, ptr);
  181.             err = 1;
  182.         }
  183.  
  184.         if (!err && mem->dup)
  185.         {
  186.             fprintf(stderr, "\n!!!CK_FREE(%s %d): 0x%lx already free'd\n",
  187.               file, line, ptr);
  188.             err = 1;
  189.         }
  190.  
  191.         if (!err && !(mem->dup = malloc(mem->len * 3 * sizeof(char))))
  192.         {
  193.             fprintf(stderr, "\n!!!CK_FREE(%s %d): 1st malloc(%u) failed!\n",
  194.               file, line, sizeof(mem->len * 3));
  195.             err = 1;
  196.         }
  197.         else if (!err)
  198.         {
  199.             RealT += mem->len * 3 * sizeof(char);
  200.             RealC++;
  201.         }
  202.  
  203.         if (!err && !(mem->file2 = malloc(strlen(file) + 1)))
  204.         {
  205.             fprintf(stderr, "\n!!!CK_FREE(%s %d): 2nd malloc(%u) failed!\n",
  206.               file, line, strlen(file) + 1);
  207.             err = 1;
  208.         }
  209.         else if (!err)
  210.         {
  211.             strcpy(mem->file2, file);
  212.             mem->line2 = line;
  213.             RealT += strlen(file) + 1;
  214.             RealC++;
  215.         }
  216.  
  217.         mem->sum = (long)mem->len + (long)mem->ptr + (long)mem->dup +
  218.           (long)mem->next;
  219.         memcpy(mem->dup, mem->ptr, mem->len * 3);
  220.  
  221.         if (err && mem->file1)
  222.             fprintf(stderr, "\tchunk alloc'd at %s %d\n", mem->file1, mem->line1);
  223.         if (err && mem->file2)
  224.             fprintf(stderr, "\tchunk free'd at %s %d\n", mem->file2, mem->line2);
  225.  
  226.         if (!err)
  227.         {
  228.             Total -= mem->len;
  229.             Count--;
  230.         }
  231.  
  232.         if (Count <= 0 || Total <= 0)
  233.             ck_info(__FILE__, __LINE__, "Ck_Free");
  234.     }
  235. }
  236.  
  237.  
  238. void
  239. ck_mem(file, line, msg)
  240.     char *file;
  241.     int line;
  242.     char *msg;
  243. {
  244.     MEM *mem;
  245.     long sum;
  246.     int err, i;
  247.  
  248.     for (mem = Mem; mem; mem = mem->next)
  249.     {
  250.         err = 0;
  251.         sum = (long)mem->len + (long)mem->ptr + (long)mem->dup + (long)mem->next;
  252.         if (mem->sum != sum)
  253.         {
  254.             fprintf(stderr, "\n!!!%s(%s %d): 0x%lx:0x%lx memory corruption!\n",
  255.                 (msg) ? msg : "CK_MEM",
  256.                 file, line, mem->ptr + mem->len, mem->dup);
  257.             err = 1;
  258.         }
  259.         else if (mem->dup)
  260.         {
  261.             if (memcmp(mem->ptr, mem->dup, mem->len * 3))
  262.             {
  263.                 fprintf(stderr,
  264.                     "\n!!!%s(%s %d): 0x%lx:0x%lx free memory changed!\n",
  265.                     (msg) ? msg : "CK_MEM",
  266.                     file, line, mem->ptr + mem->len, mem->dup);
  267.                 err = 1;
  268.             }
  269.         }
  270.         else
  271.         {
  272.             for (i = 0; i < mem->len; i++)
  273.             {
  274.                 if ((unsigned char)mem->ptr[i] != 0xa5)
  275.                 {
  276.                     fprintf(stderr,
  277.                     "\n!!!%s(%s %d): 0x%lx:0x%lx overrun backward!\n",
  278.                     (msg) ? msg : "CK_MEM",
  279.                     file, line, mem->ptr + mem->len, mem->dup);
  280.  
  281.                     fprintf(stderr, "\t%d bytes before %d length buffer (0x%02x)\n",
  282.                       i + 1, mem->len, (unsigned char)mem->ptr[i]);
  283.                     err = 1;
  284.                 }
  285.             }
  286.             for (i = 0; i < mem->len; i++)
  287.             {
  288.                 if ((unsigned char)mem->ptr[mem->len * 2 + i] != 0xa5)
  289.                 {
  290.                     fprintf(stderr,
  291.                         "\n!!!%s(%s %d): 0x%lx:0x%lx overrun forward!\n",
  292.                         (msg) ? msg : "CK_MEM",
  293.                         file, line, mem->ptr + mem->len, mem->dup);
  294.  
  295.                     fprintf(stderr, "\t%d bytes after %d length buffer (0x%02x)\n",
  296.                       i + 1, mem->len, (unsigned char)mem->ptr[mem->len * 2 + i]);
  297.                     err = 1;
  298.                 }
  299.             }
  300.         }
  301.         if (err && mem->file1)
  302.             fprintf(stderr, "\tchunk alloc'd at %s %d\n", mem->file1, mem->line1);
  303.         if (err && mem->file2)
  304.             fprintf(stderr, "\tchunk free'd at %s %d\n", mem->file2, mem->line2);
  305.     }
  306. }
  307.  
  308.  
  309. void
  310. ck_info(file, line, msg)
  311.     char *file;
  312.     int line;
  313.     char *msg;
  314. {
  315.     MEM *mem;
  316.     int flag = 0;
  317.  
  318. /***
  319.     fprintf(stderr,
  320.         "\n!!!%s(%s %d): RealT=%ld RealC=%ld Total=%ld Count=%ld\n",
  321.         (msg) ? msg : "CK_INFO",
  322.         file, line, RealT, RealC, Total, Count);
  323. ***/
  324.  
  325.     for (mem = Mem; mem; mem = mem->next)
  326.         if (!mem->dup)
  327.         {
  328.             flag++;
  329.             errmsg("LEFTOVER %d: %s %d: %u bytes 0x%lx", flag, mem->file1,
  330.               mem->line1, mem->len, mem->ptr + mem->len);
  331.         }
  332.     if (flag)
  333.         errmsg("%d LEFTOVERS: Total=%ld Count=%ld RealT=%ld RealC=%ld\n",
  334.           flag, Total, Count, RealT, RealC, Total, Count);
  335.     else
  336.         errmsg("CLEAN!: MaxT=%ld MaxC=%ld RealT=%ld RealC=%ld\n",
  337.           MaxT, MaxC, RealT, RealC);
  338.  
  339. fprintf(stderr, "\nAll done\n");
  340. }
  341.  
  342.