home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / MAGAZINE / DDJ9309.ZIP / NETSQL.ZIP / SAFEALOC.C < prev    next >
C/C++ Source or Header  |  1993-04-30  |  4KB  |  142 lines

  1. /*******************************************************************************
  2. *                                                                              *
  3. *                                 SAFEALOC.C                                   *
  4. *                                                                              *
  5. *  A safer way to do malloc and free                                           *
  6. *                                                                              *
  7. *                                                                              *
  8. *******************************************************************************/
  9.  
  10. /*------------------------------  HEADER FILES  ------------------------------*/
  11. #include "stdio.h"
  12. #include "stdlib.h"
  13. #include "string.h"
  14. #include "conio.h"
  15.  
  16. /*----------------  EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S  -----------------*/
  17.  
  18. int alloc_count = 0;
  19. static unsigned bytes=0;
  20.  
  21. /*
  22.  
  23. These functions also include a global variable called alloc_count which is
  24. incremented when a 'malloc' is done, and decremented when a 'free' is done
  25. At the end of your program, you may wish  to check 'alloc_count' to make
  26. sure that it is '0'.  If it is NOT, you are NOT freeing all the memory
  27. you have allocated.
  28.  
  29. */
  30.  
  31. /*------------------------------------------------------------------------------
  32.  
  33. MY_MALLOC        -allocate memory...SAFELY!
  34.  
  35.  
  36. SYNOPSIS:
  37.  
  38.     ptr_temp_bytes_space = char *my_malloc(number,file,line);
  39.  
  40.     unsigned number;        number of bytes requested
  41.     char *file;             name of the function who call my_malloc()
  42.     int line;               source line # who called my_malloc()
  43.  
  44.     char *ptr_temp_bytes_space;     = pointer to the requested amt of space
  45.  
  46.  
  47. DESCRIPTION:
  48.  
  49.     My_malloc allocates memory by calling malloc(3).  It also checks
  50.     for an error, and exits if necessary.
  51.  
  52.     EX: slug = my_malloc(sizeof(INODE), __FILE__, __LINE__);
  53.  
  54.  
  55. ------------------------------------------------------------------------------*/
  56. char *my_malloc(int number,char *file,int line)
  57. {
  58.     char *temp;
  59.  
  60.     if( (temp = (char *)malloc(number)) == (char *)NULL)
  61.     {
  62.         printf("\n%s, %d: malloc failure (%u)",file,line,number);
  63.         printf("\nBytes allocated=%u alloc_count=%d ",bytes,alloc_count);
  64.     getch();
  65.         exit(0);
  66.     }
  67.     memset( (char *)temp, '\0', number);
  68.  
  69.     alloc_count++;
  70.     bytes+=number;
  71.  
  72.     return(temp);
  73. }
  74.  
  75. /*------------------------------------------------------------------------------
  76. My_realloc - reallocate memory
  77.  
  78. SYNOPSIS:
  79.  
  80.     char *my_realloc(pointer,size,file,line)
  81.  
  82. DESCRIPTION:
  83.  
  84.     See "my_malloc" above.
  85.  
  86. ------------------------------------------------------------------------------*/
  87. char *my_realloc(char *pointer,unsigned size,char *file,int line)
  88. {
  89.  
  90.     if( (pointer = (char *)realloc(pointer, size)) == (char *)NULL)
  91.     {
  92.         printf("\n%s, %d: realloc failure (%u)",file,line,size);
  93.     getch();
  94.     exit(0);
  95.     }
  96.     alloc_count++;
  97.     bytes+=size;
  98.     memset( (char *)pointer, 0, size);
  99.     return(pointer);
  100. }
  101. /*------------------------------------------------------------------------------
  102. My_free - free memory
  103.  
  104. SYNOPSIS:
  105.  
  106.     char my_free(pointer)
  107.  
  108. DESCRIPTION:
  109.  
  110.     See "my_malloc" above.
  111.  
  112.  
  113. ------------------------------------------------------------------------------*/
  114.  
  115. int my_free(char *pointer,char *file,int line)
  116. {
  117.     if(pointer == NULL)
  118.     {
  119.     printf("\n%s, %d: free failure ",file,line);
  120.     getch();
  121.     exit(0);
  122.     }
  123.  
  124.     alloc_count--;
  125.  
  126.     if ( (unsigned char)(*pointer)!=255 )
  127.     {
  128.        *pointer=(unsigned char)255;
  129.        free(pointer);
  130.     }
  131.     return(1);
  132. }
  133.  
  134. int report_alloc_balance()
  135. {
  136.    printf("\nAlloc balance is %u ",alloc_count);
  137.    getch();
  138.    return(1);
  139.  
  140. }
  141.  
  142.