/* Memory2.cpp */ /* Heap managment for HiMemory */ #define INCL_DOSMEMMGR /* Memory Manager values */ #include #include /* Get flags for memory management */ #include #include #include static int UseHiMemory = 0; static Heap_t myheap; static void *initial_block; void * _mesa2_malloc(size_t bytes); void * _mesa2_calloc(size_t num, size_t bytes); void * _mesa0_malloc(size_t bytes); void * _mesa0_calloc(size_t num, size_t bytes); void * (* _pmesa_malloc)(size_t bytes) = _mesa0_malloc; void * (* _pmesa_calloc)(size_t num, size_t bytes) = _mesa0_calloc; void * _mesa0_malloc(size_t bytes) { return malloc(bytes); } void * _mesa0_calloc(size_t num, size_t bytes) { return calloc(num, bytes); } void * _mesa2_malloc(size_t bytes) { return _umalloc(myheap, bytes); } void * _mesa2_calloc(size_t num, size_t bytes) { return _ucalloc(myheap, num, bytes); } //add memory block to heap static void * _Optlink get_fn(Heap_t usrheap, size_t *length, int *clean) { void *p; int rc; /* Round up to the next chunk size */ *length = ((*length) / 65536) * 65536 + 65536; *clean = _BLOCK_CLEAN; rc = DosAllocMem(&p, *length, PAG_COMMIT | PAG_READ | PAG_WRITE| OBJ_ANY); if(rc) { printf("DosAllocMem rc =%i\n",rc); exit(1); } return (p); } //release memory block from heap static void _Optlink release_fn(Heap_t usrheap, void *p, size_t size) { DosFreeMem(p); return; } static void _Optlink destroy_fn(void) { int rc; _uclose(myheap); if (0 != _udestroy(myheap, _FORCE)) { puts("_udestroy failed."); // exit(EXIT_FAILURE); } rc = DosFreeMem(initial_block); if (0 != rc) { printf("DosFreeMem error: return code = %ld\n", rc); } } int InitHiMemory(void) { APIRET rc; char *ptr; /* Call DosAllocMem to get the initial block of memory */ if (0 != (rc = DosAllocMem(&initial_block, 65536, PAG_WRITE | PAG_READ | PAG_COMMIT| OBJ_ANY))) { UseHiMemory = 0; //detect presence of High Memory/old kernels return 1; } UseHiMemory = 1; _pmesa_malloc = _mesa2_malloc; _pmesa_calloc = _mesa2_calloc; /* Create an expandable heap starting with the block declared earlier */ if (NULL == (myheap = _ucreate(initial_block, 65536, _BLOCK_CLEAN, _HEAP_REGULAR, get_fn, release_fn))) { return 2; // puts("_ucreate failed."); // exit(EXIT_FAILURE); } if (0 != _uopen(myheap)) { return 3; // puts("_uopen failed."); // exit(EXIT_FAILURE); } atexit(destroy_fn); return 0; }