home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 319_01 / mem.c < prev    next >
C/C++ Source or Header  |  1990-06-18  |  1KB  |  81 lines

  1. /*
  2.     CPP V5 -- Memory management routines.
  3.  
  4.     source:  mem.c
  5.     started: September 20, 1985
  6.     version: October 15, 1987; March 24, 1988
  7.  
  8.     Written by Edward K. Ream.
  9.     This software is in the public domain.
  10.  
  11.     See the read.me file for disclaimer and other information.
  12. */
  13.  
  14. #include "cpp.h"
  15. #ifdef MICRO_SOFT
  16. #include <malloc.h>
  17. #else
  18. #include <alloc.h>
  19. #endif
  20.  
  21. /* Define local data structures. */
  22. static long m_tot = 0;    /* Total number of bytes allocated.    */
  23.  
  24. /*
  25.     Allocate n bytes using calloc(), assumed to get memory from system
  26.  
  27.     The returned memory IS zeroed.
  28. */
  29. void *
  30. m_alloc(n)
  31. int n;
  32. {
  33.     register char *p;
  34.  
  35.     TRACEP("m_alloc", printf("(%d): ", n));
  36.  
  37.     /* Align the request. */
  38.     while (n & (sizeof(short int)-1) ) {
  39.         n++;
  40.     }
  41.  
  42.     p = calloc(1, (unsigned) n);
  43.     if (p == NULL) {
  44.         printf("sorry, out of memory\n");
  45.         m_stat();
  46.         exit(BAD_EXIT);
  47.     }
  48.  
  49.     /* Update statistics. */
  50.     m_tot += (long) n;
  51.  
  52.     TRACEN("m_alloc", printf("returns %p\n", p));
  53.     return p;
  54. }
  55.  
  56. /*
  57.     Free memory allocated by m_alloc().
  58. */
  59. void
  60. m_free(p)
  61. void *p;
  62. {
  63.     TRACEP("m_free", printf("(%p)\n", p));
  64.  
  65.     if (p == NULL) {
  66.         syserr("m_free: trying to free a NULL pointer.");
  67.     }
  68.     else {
  69.         free(p);
  70.     }
  71. }
  72.  
  73. /*
  74.     Print statistics about memory manager.
  75. */
  76. void
  77. m_stat()
  78. {
  79.     TRACEP("m_stat", printf("m_tot: %ld (0x%lx)\n", m_tot, m_tot));
  80. }
  81.