home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 355_02 / slk2.exe / SPP / MEM.C < prev    next >
C/C++ Source or Header  |  1991-06-09  |  2KB  |  99 lines

  1. /*
  2.     Sherlock Preprocessor -- Memory management routines.
  3.  
  4.     source:  mem.c
  5.     started: September 20, 1985
  6.     version: March 29, 1988; July 2, 1988
  7.  
  8.  
  9.     PUBLIC DOMAIN SOFTWARE
  10.  
  11.     Sherlock, including the SPP, SDEL and SDIF programs, was placed in
  12.     the public domain on June 15, 1991, by its author,
  13.  
  14.         Edward K. Ream
  15.         166 North Prospect Ave.
  16.         Madison, WI 53705.
  17.         (608) 257-0802
  18.  
  19.     Sherlock may be used for any commercial or non-commercial purpose.
  20.  
  21.  
  22.     DISCLAIMER OF WARRANTIES
  23.  
  24.     Edward K. Ream (Ream) specifically disclaims all warranties,
  25.     expressed or implied, with respect to this computer software,
  26.     including but not limited to implied warranties of merchantability
  27.     and fitness for a particular purpose.  In no event shall Ream be
  28.     liable for any loss of profit or any commercial damage, including
  29.     but not limited to special, incidental consequential or other damages.
  30. */
  31.  
  32. #include "spp.h"
  33.  
  34. #ifdef TURBOC
  35. #include <alloc.h>
  36. #else
  37. #include <malloc.h>
  38. #endif
  39.  
  40. /* Define local data structures. */
  41. static long m_tot = 0;    /* Total number of bytes allocated.    */
  42.  
  43. /*
  44.     Allocate n bytes using calloc(), assumed to get memory from system
  45.  
  46.     The returned memory IS zeroed.
  47. */
  48. void *
  49. m_alloc(int n)
  50. {
  51.     register char *p;
  52.  
  53.     TRACEP("m_alloc", printf("(%d) ", n));
  54.  
  55.     /* Align the request. */
  56.     while (n & (sizeof(short int)-1) ) {
  57.         n++;
  58.     }
  59.  
  60.     p = calloc(1, (unsigned) n);
  61.     if (p == NULL) {
  62.         printf("sorry, out of memory\n");
  63.         m_stat();
  64.         exit(BAD_EXIT);
  65.     }
  66.  
  67.     /* Update statistics. */
  68.     m_tot += (long) n;
  69.  
  70.     TRACEN("m_alloc", printf("returns %p\n", p));
  71.     return p;
  72. }
  73.  
  74. /*
  75.     Free memory allocated by m_alloc().
  76. */
  77. void
  78. m_free(void *p)
  79. {
  80.     TRACEP("m_free", printf("(%p)\n", p));
  81.  
  82.     if (p == NULL) {
  83.         syserr("m_free: trying to free a NULL pointer");
  84.     }
  85.     else {
  86.         free(p);
  87.     }
  88. }
  89.  
  90. /*
  91.     Print statistics about memory manager.
  92. */
  93. void
  94. m_stat(void)
  95. {
  96.     TRACEP("m_stat",
  97.         printf("m_tot: %ld (0x%lx)\n", m_tot, m_tot));
  98. }
  99.