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

  1.    alloc.h
  2.  
  3.        /* Copyright 1987  Pugh-Killeen Associates */
  4.        #define free(x) my_free(x)
  5.        #define calloc(a,b)  my_calloc(a,b)
  6.        void *my_free();
  7.        void *my_calloc();
  8.  
  9.    alloc.c
  10.  
  11. /* Copyright 1987  Pugh-Killeen Associates */
  12. #include "alloc.h"
  13. #include <stdio.h>
  14.  
  15. #undef calloc                      /* Get rid of prior defines */
  16. #undef free                
  17.  
  18. #define MAX_POINTS 200             /* Maximum number of allocations */
  19.  
  20. static void *points[MAX_POINTS];   /* Points to allocated memory */
  21. static int point_count;            /* Current index into points */    
  22. static long used;                  /* Number of bytes allocated */
  23.  
  24. void *calloc();
  25.  
  26. void *my_calloc(a,b)
  27. /* Allocate and keep track of allocations */
  28. int a;
  29. int b;
  30.     {
  31.     void *pc;
  32.     pc = calloc(a,b);            
  33.     if (pc != NULL)
  34.         {
  35.         used += (long) a * (long) b;
  36.         if (point_count < MAX_POINTS)
  37.             points[point_count++] = pc;
  38.         else
  39.             printf("\n Too many allocations to keep track off");
  40.         }
  41.     else
  42.         printf("\n No memory left , counter = %d used %ld",
  43.            point_count, used);
  44.     return pc;
  45.     }
  46.  
  47. my_free(pc)    
  48. void *pc;
  49.     {
  50.     int i;
  51.     if (pc == NULL)
  52.         {
  53.         printf("Freeing NULL pointer");    
  54.         goto end;
  55.         }
  56.     /* See which block was allocated */
  57.     for (i = 0; i < point_count; i++)
  58.         {
  59.         if (points[i] == NULL)
  60.             continue;
  61.         else if (points[i] == pc)
  62.             {
  63.             free(pc);
  64.             points[i] = NULL;
  65.             goto end;
  66.             }
  67.         }
  68.     printf("Freeing something not allocated");
  69. end:        
  70.     return;
  71.     }        
  72.  
  73.  
  74.  
  75.                    
  76.