/************************************************************************ This example uses _ucreate to create an expandable heap. The functions for expanding and shrinking the heap are get_fn and release_fn. The program then opens the heap and performs operations on it, and then closes and destroys the heap. ************************************************************************/ #define INCL_DOSMEMMGR /* Memory Manager values */ #include #include /* Get flags for memory management */ #include #include #include 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); } static void _Optlink release_fn(Heap_t usrheap, void *p, size_t size) { DosFreeMem(p); return; } int main(void) { void *initial_block; APIRET rc; Heap_t myheap; char *ptr; int iRAM,i; /* Call DosAllocMem to get the initial block of memory */ if (0 != (rc = DosAllocMem(&initial_block, 65536, PAG_WRITE | PAG_READ | PAG_COMMIT))) { printf("DosAllocMem error: return code = %ld\n", rc); exit(EXIT_FAILURE); } /* 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))) { puts("_ucreate failed."); exit(EXIT_FAILURE); } if (0 != _uopen(myheap)) { puts("_uopen failed."); exit(EXIT_FAILURE); } iRAM = 0; /* кушаем 1Гб */ for(i=0;i<1024;i++) { /* Force user heap to grow */ ptr = (char *)_umalloc(myheap, 1024*1024); iRAM += 1; printf("RAM eat:%iMb\n",iRAM); } /* пишем в файл */ { FILE *fp; fp = fopen("test.tst","wb"); for(i=0;i<1024;i++) ptr[i] = (i&0xff); rc = fwrite(ptr,1024,1,fp); printf("fwrite rc=%i\n",rc); fclose(fp); fp = fopen("test.tst","rb"); rc = fread(ptr,1024,1,fp); printf("fread rc=%i\n",rc); for(i=0;i<1024;i++) { if(ptr[i]!= (i&0xff)) { printf("%i ptr=%i != %i\n",i,ptr[i],(i&0xff)); } } fclose(fp); } _uclose(myheap); if (0 != _udestroy(myheap, _FORCE)) { puts("_udestroy failed."); exit(EXIT_FAILURE); } if (0 != (rc = DosFreeMem(initial_block))) { printf("DosFreeMem error: return code = %ld\n", rc); exit(EXIT_FAILURE); } return 0; }