home *** CD-ROM | disk | FTP | other *** search
/ cs.rhul.ac.uk / www.cs.rhul.ac.uk.zip / www.cs.rhul.ac.uk / pub / rdp / rdp_cs3470.tar / rdp_supp / memalloc.c < prev    next >
C/C++ Source or Header  |  1998-05-07  |  2KB  |  60 lines

  1. /*******************************************************************************
  2. *
  3. * RDP release 1.50 by Adrian Johnstone (A.Johnstone@rhbnc.ac.uk) 20 December 1997
  4. *
  5. * memalloc.c - robust memory allocation with helpful messages on error
  6. *
  7. * This file may be freely distributed. Please mail improvements to the author.
  8. *
  9. *******************************************************************************/
  10. #include <stdlib.h>
  11. #include "textio.h"
  12. #include "memalloc.h"
  13.  
  14. static unsigned long mem_calloced = 0; 
  15. static unsigned long mem_malloced = 0; 
  16. static unsigned long mem_realloced = 0; 
  17.  
  18. static void * mem_check(void * p, const char * str)
  19. {
  20.   if (p == NULL)
  21.     text_message(TEXT_FATAL, "insufficient memory for %salloc\n", str); 
  22.   return p; 
  23. }
  24.  
  25. void * mem_calloc(size_t nitems, size_t size)
  26. {
  27.   mem_calloced +=(nitems * size); 
  28.   
  29.   return mem_check(calloc(nitems, size), "c"); 
  30. }
  31.  
  32. void mem_free(void * block)
  33. {
  34.   if (block == NULL)          /* Is this a pointer actually allocated? */
  35.     text_message(TEXT_FATAL, "attempted to free a null block\n"); 
  36.   
  37.   free(block);                /* free the block */
  38. }
  39.  
  40. void * mem_malloc(size_t size)
  41. {
  42.   mem_malloced += size; 
  43.   
  44.   return mem_check(malloc(size), "m"); 
  45. }
  46.  
  47. void mem_print_statistics(void)
  48. {
  49.   text_message(TEXT_INFO, "Heap manager calloc\'ed %lu bytes, malloc\'ed %lu bytes and realloc\'ed %lu bytes\n", 
  50.   mem_calloced, mem_malloced, mem_realloced); 
  51. }
  52.  
  53. void * mem_realloc(void * block, size_t size)
  54. {
  55.   mem_realloced += size; 
  56.   
  57.   return mem_check(realloc(block, size), "re"); 
  58. }
  59. /* End of memalloc.c */
  60.