home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_01 / 1n01084a < prev    next >
Text File  |  1990-05-17  |  3KB  |  140 lines

  1. /*
  2.     Module:     PtrDebug
  3.     Version:    1.00
  4.  
  5.     Language:   ANSI C
  6.     Environ:    Any
  7.  
  8.     Purpose:    Provides debug replacements for the standard
  9.                 dynamic memory allocation functions.
  10.  
  11.     Written by: Scott Robert Ladd
  12. */
  13.  
  14. #define NO_PTRDEBUG_DEF
  15.  
  16. #include "limits.h"
  17. #include "ptrdebug.h"
  18. #include "stdlib.h"
  19. #include "stdio.h"
  20. #include "string.h"
  21.  
  22. typedef enum {PE_NoAlloc,     PE_ReallocNull, PE_ArrayTooBig,
  23.               PE_FreeTooMany, PE_FreeNull}    PTR_ERROR;
  24.  
  25. static unsigned long alloc_count = 0;
  26.  
  27. static void dbg_ptr_err(PTR_ERROR err, char * file, int line);
  28.  
  29. void * dbg_malloc(size_t len, char * file, int line)
  30.     {
  31.     void * result = malloc(len);
  32.  
  33.     if (result == NULL)
  34.         dbg_ptr_err(PE_NoAlloc,file,line);
  35.     else
  36.         ++alloc_count;
  37.  
  38.     return result;
  39.     }
  40.  
  41. void * dbg_calloc(size_t num, size_t len, char * file, int line)
  42.     {
  43.     unsigned long total_len = num * len;
  44.  
  45.     if (total_len > UINT_MAX)
  46.         {
  47.         dbg_ptr_err(PE_ArrayTooBig,file,line);
  48.         return NULL;
  49.         }
  50.  
  51.     return dbg_malloc((size_t)total_len, file, line);
  52.     }
  53.  
  54. void * dbg_realloc(void * ptr, size_t len, char * file, int line)
  55.     {
  56.     void * result;
  57.  
  58.     if (ptr == NULL)
  59.         {
  60.         dbg_ptr_err(PE_ReallocNull,file,line);
  61.         return NULL;
  62.         }
  63.  
  64.     result = dbg_malloc(len, file, line);
  65.  
  66.     if (result != NULL)
  67.         {
  68.         memcpy(result,ptr,len);
  69.         ++alloc_count;
  70.         dbg_free(ptr);
  71.         }
  72.  
  73.     return result;
  74.     }
  75.  
  76. void dbg_free(void ** ptr, char * file, int line)
  77.     {
  78.     if (*ptr == NULL)
  79.         {
  80.         dbg_ptr_err(PE_FreeNull,file,line);
  81.         return;
  82.         }
  83.  
  84.     if (alloc_count == 0)
  85.         {
  86.         dbg_ptr_err(PE_FreeTooMany,file,line);
  87.         return;
  88.         }
  89.  
  90.     --alloc_count;
  91.  
  92.     free(*ptr);
  93.  
  94.     *ptr = NULL;
  95.     }
  96.  
  97. char * dbg_strdup(char * str, char * file, int line)
  98.     {
  99.     char * result;
  100.  
  101.     if (str == NULL)
  102.         {
  103.         dbg_ptr_err(PE_FreeNull,file,line);
  104.         return NULL;
  105.         }
  106.  
  107.     result = dbg_malloc(strlen(str) + 1, file, line);
  108.  
  109.     if (result != NULL)
  110.         strcpy(result,str);
  111.  
  112.     return result;
  113.     }
  114.  
  115. static void dbg_ptr_err(PTR_ERROR err, char * file, int line)
  116.     {
  117.     printf("\aPointer Error in %s @ %d: ",file,line);
  118.  
  119.     switch (err)
  120.         {
  121.         case PE_NoAlloc:
  122.             puts("memory not allocated");
  123.             break;
  124.         case PE_ReallocNull:
  125.             puts("attempt to reallocate NULL pointer");
  126.             break;
  127.         case PE_ArrayTooBig:
  128.             puts("calloc array too big");
  129.             break;
  130.         case PE_FreeTooMany:
  131.             puts("attempted to free more pointers than allocated");
  132.             break;
  133.         case PE_FreeNull:
  134.             puts("attempted to free NULL pointer");
  135.             break;
  136.         default:
  137.             puts("unknown");
  138.         }
  139.     }
  140.