home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_05 / 1005077a < prev    next >
Text File  |  1992-03-09  |  2KB  |  112 lines

  1. /*
  2.  *    listing 7 - facade malloc routines
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. extern    char    *malloc();
  8. extern    void    free();
  9.  
  10.  
  11. #define    PTAB_SIZE    1000
  12.  
  13. static    struct    {
  14.     int    size;        /* allocated sized */
  15.     char    *ptr;        /* allocated pointer */
  16. }    p_tab[PTAB_SIZE];    /* table of allocated entries */
  17. static    int    cnt = 0;    /* maximum entry number */
  18. static    int    amount = 0;    /* number of bytes allocated */
  19.  
  20. #ifdef    ANSI_C
  21. char    *loc_malloc(int size, char *fn, int ln)
  22. #else
  23. char    *loc_malloc(size, fn, ln)
  24. int    size;
  25. char    *fn;
  26. int    ln;
  27. #endif
  28. /*
  29.  *    malloc knock off
  30.  *    size - the number of bytes to allocate
  31.  *    fn - the file name of where malloc occured
  32.  *    ln - the line number
  33.  */
  34. {
  35. char    *ptr;
  36.  
  37.     ptr = malloc(size);
  38.     /*printf("mallocing %d bytes at 0x%lx, %s:%d\n",size,ptr,fn,ln);*/
  39.     if (cnt != -1) {
  40.         int    i;
  41.         int    n;
  42.  
  43.         n = -1;
  44.         for (i = 0; i < cnt; i++) {
  45.             if (p_tab[i].ptr == ptr) {
  46.                 printf("!!!!!! ptr was previously allocated\n");
  47.                 printf("FILE: \"%s\" LINE: %d\n",fn,ln);
  48.             } else if (p_tab[i].ptr == NULL) {
  49.                 n = i;
  50.             }
  51.         }
  52.         if (n == -1) n = cnt++;
  53.         p_tab[n].ptr = ptr;
  54.         p_tab[n].size = size;
  55.         amount += size;
  56.         if (cnt >= PTAB_SIZE) {
  57.             cnt = -1;
  58.             printf("overflowing the malloc table\n");
  59.         }
  60.     }
  61.     return(ptr);
  62. }
  63.  
  64. #ifdef    ANSI_C
  65. void    loc_free(char *ptr, char *fn, int ln)
  66. #else
  67. void    loc_free(ptr, fn, ln)
  68. char    *ptr;
  69. char    *fn;
  70. int    ln;
  71. #endif
  72. /*
  73.  *    free knock off
  74.  *    ptr - the address to free
  75.  *    fn - the file name of where malloc occured
  76.  *    ln - the line number
  77.  */
  78. {
  79.     /*printf("free 0x%lx, %s:%d\n",ptr,fn,ln);*/
  80.     if (cnt != -1) {
  81.         int    i;
  82.  
  83.         for (i = 0; i < cnt; i++) {
  84.             if (p_tab[i].ptr == ptr) break;
  85.         }
  86.         if (i >= cnt) {
  87.             printf("!!!!!! pointer not found in malloc table\n");
  88.             printf("FILE: \"%s\" LINE: %d\n",fn,ln);
  89.         } else {
  90.             p_tab[i].ptr = NULL;
  91.             amount -= p_tab[i].size;
  92.         }
  93.         while((cnt > 0) && (p_tab[cnt-1].ptr == NULL)) cnt--;
  94.     }
  95.     free(ptr);
  96. }
  97.  
  98.  
  99. void    print_heap_size()
  100. /*
  101.  *    print out the amount of heap currently being used
  102.  */
  103. {
  104. char    buf[80];
  105.  
  106.     if (cnt == -1) {
  107.         printf("Too many heap entries; lost track of size\n");
  108.     } else {
  109.         printf("Heap size = %d bytes\n", amount);
  110.     }
  111. }
  112.