home *** CD-ROM | disk | FTP | other *** search
- /* alloc.c
-
- Copyright (c) QCAD Systems, Inc. 1990. All rights reserved.
-
- Related to memory allocation, freeing, malloc space, etc.
- Strategy depends heavily on whether a UNIX or MICROSOFT system is being
- used.
-
- */
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #ifdef DBG_ALLOC
- # include <dos.h>
- #endif
-
- #ifdef MSDOS
- # include <sys\types.h>
- # include <sys\stat.h>
- # include <io.h>
- # include <malloc.h>
- long int totalspace= 0, max_space= 0;
- #endif
-
- #ifdef UNIX
- # include <ctype.h>
- # include <sys/types.h>
- # include <sys/stat.h>
- #endif
-
- extern FILE *rfile;
-
- #ifdef MSDOS
- /* ............... */
- void memory_failure()
- {
- fprintf(rfile, "Out of memory -- sorry!\n");
- exit(-1);
- }
-
- /* ............... */
- char *our_realloc(pntr, newsize)
- char *pntr;
- int newsize;
- { char *our_malloc();
- void mem_avail();
-
- if (pntr) {
- char *pntr1;
-
- pntr1= realloc(pntr, newsize);
- if (pntr1==NULL) memory_failure();
- #ifdef DBG_ALLOC
- printf("R %04x:%04x (%6ld)\n", FP_SEG(pntr1), FP_OFF(pntr1), totalspace);
- #endif
- totalspace += _msize(pntr1) - _msize(pntr);
- if (max_space < totalspace) max_space= totalspace;
- return pntr1;
- }
- else pntr= our_malloc(newsize);
- if (pntr==NULL) memory_failure();
- return pntr;
- }
-
- /* ................... */
- void mem_avail(msg)
- char msg[];
- {
- fprintf(rfile, "Memory now in use= %ld, max used= %ld: %s\n",
- totalspace, max_space, msg);
- }
-
- /* ............... */
- char *our_malloc(size)
- int size;
- { char *pntr;
-
- if (size <= 0) size= 4;
- pntr= (char *) malloc(size);
- if (pntr==NULL) memory_failure();
- #ifdef DBG_ALLOC
- printf("M %04x:%04x (%6ld)\n", FP_SEG(pntr), FP_OFF(pntr), totalspace);
- #endif
- totalspace += _msize(pntr);
- if (max_space < totalspace) max_space= totalspace;
- return pntr;
- }
-
- /* ............... */
- void our_free(pntr)
- char *pntr;
- {
- #ifndef NOFREE
- if (pntr==NULL) return;
- totalspace -= _msize(pntr);
- #ifdef DBG_ALLOC
- printf("F (%6ld)\n", totalspace);
- #endif
- free((char *) pntr);
- #endif
- }
-
- /* .............. */
- char *our_strdup(str)
- char *str;
- { char *nstr= strdup(str);
-
- if (nstr==NULL) memory_failure();
- totalspace += _msize(nstr);
- #ifdef DBG_ALLOC
- printf("S %04x:%04x (%6ld)\n", FP_SEG(nstr), FP_OFF(nstr), totalspace);
- #endif
- if (max_space < totalspace) max_space= totalspace;
- return nstr;
- }
-
- /* .............. */
- void heapcheck(msg)
- char msg[];
- {
- #ifdef HCHECK
- fprintf(rfile, "Checking heap: %s\n", msg);
- switch (_heapset('Z')) {
- case _HEAPOK: fprintf(rfile, " ... is OK\n"); break;
- case _HEAPEMPTY: fprintf(rfile, " ... is empty\n"); break;
- case _HEAPBADBEGIN: fprintf(rfile, " ERROR - bad start\n"); exit(-1);
- case _HEAPBADNODE: fprintf(rfile, " ERROR - bad node\n"); exit(-1);
- default: fprintf(rfile, "heapcheck call BUG: %s\n", msg); exit(-1);
- }
- #endif
- }
-
- #endif
-