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

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