/***************************************************************************** * * * DMEM.C * * * ***************************************************************************** * * Dynamic Memory C Source File * *****************************************************************************/ /***************************************************************************** * * * INCLUDE FILES * * * *****************************************************************************/ #include #include #include #include "global.h" #include "dmem.h" #define SIZE_OFFSET 2 #define SIZE_ADJUST 5 #define MARGIN 2048 /***************************************************************************** * * * dmem_alloc(...) * * * ***************************************************************************** * * NOTE: * * Allocation occurs on even byte boundaries; therfore, if an * odd number of byte is requested, the number is increased to * the next even number. * *****************************************************************************/ char *dmem_alloc(req_size) long *req_size; { long amt_mem; char *buf; if (*req_size & 0x0000000F) /* Odd paragraph? */ *req_size = (*req_size & 0x0000FFF0) + 0x00000010; /* Make even paragraph */ amt_mem = coreleft(); if (((*req_size) + MARGIN) >= amt_mem) { printf("Only %lu bytes of memory left and %ld+2048 bytes needed.\n", amt_mem,*req_size); error("Not enough memory left to allocate"); return (NULL); } buf = farmalloc(*req_size); if (!buf) { error("Cannot allocate memory for buffer"); return (NULL); } if (diag_here(DIAG_HEAP_CHECK) && (heapcheck() == _HEAPCORRUPT)) error("Heap is corrupt when allocating"); return (buf); } /***************************************************************************** * * * dmem_free(...) * * * ***************************************************************************** * * *****************************************************************************/ FLAG dmem_free(buf) char **buf; { if (!*buf) { error ("NULL, cannot free memory"); return (FALSE); } if (diag_here(DIAG_HEAP_CHECK) && (heapcheck() == _HEAPCORRUPT)) error("Heap is corrupt when freeing"); farfree(*buf); *buf = NULL; return (TRUE); } /***************************************************************************** * * * *****************************************************************************/ long dmem_size() { return (coreleft()); }