home *** CD-ROM | disk | FTP | other *** search
/ The Education Master 1994 (4th Edition) / EDUCATIONS_MASTER_4TH_EDITION.bin / files / progmisc / qparser2 / lib / qalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  2.9 KB  |  136 lines

  1. /*  alloc.c
  2.  
  3. Copyright (c) QCAD Systems, Inc. 1990.  All rights reserved.
  4.  
  5.     Related to memory allocation, freeing, malloc space, etc.
  6. Strategy depends heavily on whether a UNIX or MICROSOFT system is being
  7. used.
  8.  
  9.     */
  10.  
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #ifdef DBG_ALLOC
  15. #  include <dos.h>
  16. #endif
  17.  
  18. #ifdef MSDOS
  19. #  include <sys\types.h>
  20. #  include <sys\stat.h>
  21. #  include <io.h>
  22. #  include <malloc.h>
  23.    long int totalspace= 0, max_space= 0;
  24. #endif
  25.  
  26. #ifdef UNIX
  27. #  include <ctype.h>
  28. #  include <sys/types.h>
  29. #  include <sys/stat.h>
  30. #endif
  31.  
  32. extern FILE *rfile;
  33.  
  34. #ifdef MSDOS
  35. /* ............... */
  36. void memory_failure()
  37. {
  38.   fprintf(rfile, "Out of memory -- sorry!\n");
  39.   exit(-1);
  40.   }
  41.  
  42. /* ............... */
  43. char *our_realloc(pntr, newsize)
  44.   char *pntr;
  45.   int newsize;
  46. { char *our_malloc();
  47.   void mem_avail();
  48.  
  49.   if (pntr) {
  50.     char *pntr1;
  51.  
  52.     pntr1= realloc(pntr, newsize);
  53.     if (pntr1==NULL) memory_failure();
  54. #ifdef DBG_ALLOC
  55.     printf("R %04x:%04x (%6ld)\n", FP_SEG(pntr1), FP_OFF(pntr1), totalspace);
  56. #endif
  57.     totalspace += _msize(pntr1) - _msize(pntr);
  58.     if (max_space < totalspace) max_space= totalspace;
  59.     return pntr1;
  60.     }
  61.   else pntr= our_malloc(newsize);
  62.   if (pntr==NULL) memory_failure();
  63.   return pntr;
  64.   }
  65.  
  66. /* ................... */
  67. void mem_avail(msg)
  68.   char msg[];
  69. {
  70.   fprintf(rfile, "Memory now in use= %ld, max used= %ld: %s\n",
  71.          totalspace, max_space, msg);
  72.   }
  73.  
  74. /* ............... */
  75. char *our_malloc(size)
  76.   int size;
  77. { char *pntr;
  78.  
  79.   if (size <= 0) size= 4;
  80.   pntr= (char *) malloc(size);
  81.   if (pntr==NULL) memory_failure();
  82. #ifdef DBG_ALLOC
  83.   printf("M %04x:%04x (%6ld)\n", FP_SEG(pntr), FP_OFF(pntr), totalspace);
  84. #endif
  85.   totalspace += _msize(pntr);
  86.   if (max_space < totalspace) max_space= totalspace;
  87.   return pntr;
  88.   }
  89.  
  90. /* ............... */
  91. void our_free(pntr)
  92.   char *pntr;
  93. {
  94. #ifndef NOFREE
  95.   if (pntr==NULL) return;
  96.   totalspace -= _msize(pntr);
  97. #ifdef DBG_ALLOC
  98.   printf("F (%6ld)\n", totalspace);
  99. #endif
  100.   free((char *) pntr);
  101. #endif
  102.   }
  103.  
  104. /* .............. */
  105. char *our_strdup(str)
  106.   char *str;
  107. { char *nstr= strdup(str);
  108.   
  109.   if (nstr==NULL) memory_failure();
  110.   totalspace += _msize(nstr);
  111. #ifdef DBG_ALLOC
  112.   printf("S %04x:%04x (%6ld)\n", FP_SEG(nstr), FP_OFF(nstr), totalspace);
  113. #endif
  114.   if (max_space < totalspace) max_space= totalspace;
  115.   return nstr;
  116.   } 
  117.   
  118. /* .............. */
  119. void heapcheck(msg)
  120.   char msg[];
  121. {
  122. #ifdef HCHECK
  123.   fprintf(rfile, "Checking heap: %s\n", msg);
  124.   switch (_heapset('Z')) {
  125.     case _HEAPOK: fprintf(rfile, " ... is OK\n"); break;
  126.     case _HEAPEMPTY: fprintf(rfile, " ... is empty\n"); break;
  127.     case _HEAPBADBEGIN: fprintf(rfile, " ERROR - bad start\n"); exit(-1);
  128.     case _HEAPBADNODE: fprintf(rfile, " ERROR - bad node\n"); exit(-1);
  129.     default: fprintf(rfile, "heapcheck call BUG: %s\n", msg); exit(-1);
  130.     }
  131. #endif
  132.   }
  133.  
  134. #endif
  135.  
  136.