home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_06 / 2n06028a < prev    next >
Text File  |  1991-04-30  |  4KB  |  124 lines

  1. /* ----------------------------------------------------------
  2.  *  Turbo C++
  3.  * ------------------------------------------------------- */
  4.  
  5. #define NO_EXT_MALLOC     /* disable malloc front ends */
  6.  
  7. #include <stdio.h>
  8. #include <alloc.h>
  9. #include "snooper.h"
  10.  
  11. #define MAXBUF  300
  12. static void *Mbuf[MAXBUF];        /* malloc() debug buffer */
  13.  
  14. /* ------------------------------------------------------- */
  15. static void **find_slot( void *hp)
  16. {
  17.        /* Find a specified member in Mbuf[] and return a
  18.         * pointer to the found slot. Return NULL if <hp>
  19.         * is not in Mbuf[].
  20.         */
  21.  
  22.         register void **p;
  23.  
  24.         for( p = Mbuf; p < (Mbuf + MAXBUF); ++p ) {
  25.                 if( *p == hp )
  26.                         return p;
  27.         }
  28.  
  29.         return (void **) 0;
  30. }
  31.  
  32. void *d_malloc(const char *file, int lineno, size_t nbytes)
  33. {
  34.     void *pheap;
  35.     void **slot;
  36.  
  37.     if( (pheap = malloc(nbytes)) == NULL ) {
  38.         __BREAK(__SCRN__,
  39.             d_printf("File %s, Line %d: malloc() error for "
  40.                      "%u bytes", file, lineno, nbytes ));
  41.     }
  42.     else if( (slot = find_slot((void *) 0)) == (void **) 0) {
  43.             __BREAK( __SCRN__,
  44.                 d_printf( "xmalloc: debug buffer full!" ));
  45.     }
  46.     else
  47.             *slot = pheap;      /* install in debug buffer */
  48.  
  49.     return pheap;
  50. }
  51.  
  52. void *d_calloc(const char *file, int lineno,
  53.                                 size_t nitems, size_t size)
  54. {
  55.     void  *pheap;
  56.     void **slot;
  57.  
  58.     if( (pheap = calloc(nitems, size)) == NULL ) {
  59.         __BREAK(__SCRN__,
  60.              d_printf("File %s, Line %d: calloc() error for "
  61.                  "%u bytes", file, lineno, nitems * size));
  62.     }
  63.     else if( (slot = find_slot((void *) 0)) == (void **) 0) {
  64.             __BREAK( __SCRN__,
  65.                   d_printf( "xmalloc: debug buffer full!" ));
  66.     }
  67.     else
  68.             *slot = pheap;      /* install in debug buffer */
  69.  
  70.     return pheap;
  71. }
  72.  
  73. void *d_realloc(const char *file, int lineno,
  74.                            void *oldp, size_t nbytes )
  75. {
  76.     void *newp = (void *) NULL;
  77.     void **slot;
  78.  
  79.     if( (slot = find_slot(oldp)) == (void **) 0 ) {
  80.         #if defined(__SMALL__) || defined(__MEDIUM__)
  81.             __BREAK(__SCRN__, d_printf("File %s, Line %d: "
  82.                 "realloc() error at address %Xh",
  83.                                          file,lineno,oldp));
  84.         #else
  85.             __BREAK(__SCRN__, d_printf("File %s, Line %d: "
  86.                 "realloc() error at address %lXh",
  87.                                          file,lineno,oldp));
  88.         #endif
  89.     }
  90.     else if((newp = realloc( oldp, nbytes )) == (void *) 0) {
  91.         __BREAK(__SCRN__, d_printf("File %s, Line %d: "
  92.         "realloc() error for %u bytes", file,lineno,nbytes));
  93.     }
  94.     else    {
  95.             *slot = newp;   /* replace new -> in old slot */
  96.     }
  97.  
  98.     return newp;
  99. }
  100.  
  101. void d_free(const char *file, int lineno, void *pheap )
  102. {
  103.     void **slot;
  104.  
  105.     if( pheap != (void *) NULL )    {
  106.         if( (slot = find_slot(pheap)) != (void **) 0 ) {
  107.                 *slot = (void *) NULL;
  108.                 free(pheap);
  109.                 return;
  110.         }
  111.     }
  112.  
  113.     #if defined(__SMALL__) || defined(__MEDIUM__)
  114.         __BREAK(__SCRN__,d_printf("File %s, Line %d: free() "
  115.                  "error at address %Xh", file,lineno,pheap));
  116.     #else
  117.         __BREAK(__SCRN__,d_printf("File %s, Line %d: free() "
  118.                 "error at address %lXh", file,lineno,pheap));
  119.     #endif
  120.     return ;
  121. }
  122.  
  123. /* ------------------------------------------------------- */
  124.