|
|
1.1 ! root 1: /* ! 2: * Example of DOSALLOCSEG/DOSFREESEG usage. ! 3: * ! 4: * Shows how to allocate a segment, create a ! 5: * pointer to it, access the segment, and free it. ! 6: * Creates one 16K segment by default, or the number ! 7: * specified on the command line. After all are allocated, ! 8: * writes a 0 into the first byte of each. If you run this on ! 9: * a system with swapping enabled you can allocate more segments ! 10: * segments than fit in physcical memory, and exercise your disk. ! 11: * ! 12: * This program works Family API mode, although obviously ! 13: * without the benefit of virtual memory & swapping. ! 14: * ! 15: * Compile as: cl -AL -G2 -Lp alloc.c ! 16: * ! 17: * Copyright (C) Microsoft Corp. 1986 ! 18: */ ! 19: ! 20: #include <doscalls.h> ! 21: #include <malloc.h> ! 22: #include <dos.h> ! 23: ! 24: #define SEGSIZE (16 * 1024) /* size of each segment allocated */ ! 25: #define NOTSHARED 0 ! 26: ! 27: main(argc, argv) ! 28: int argc; ! 29: char *argv[]; ! 30: { ! 31: unsigned *seglist; /* pointer to list of segments */ ! 32: char *p; /* pointer used to access segments */ ! 33: register int i, j; ! 34: int rc; /* system call return code */ ! 35: ! 36: if(argc == 2) /* number of segments to allocate */ ! 37: i = atoi(argv[1]); ! 38: else ! 39: i = 1; ! 40: ! 41: seglist = (unsigned *)malloc(2 * i); /* list of segment selectors */ ! 42: ! 43: /* allocate segment(s) */ ! 44: for(j = 0; j < i; j++) { ! 45: rc = DOSALLOCSEG(SEGSIZE, &seglist[j], NOTSHARED); ! 46: if(rc == 0) ! 47: printf("allocated segment %d\n", j); ! 48: else { ! 49: printf("allocation failed on segment %d\n", j); ! 50: i = j; ! 51: } ! 52: } ! 53: ! 54: /* write 0 into first byte of each segment */ ! 55: for(j = 0; j < i; j++) { ! 56: FP_SEG(p) = seglist[j]; /* make long pointer to seg */ ! 57: FP_OFF(p) = 0; ! 58: printf("touching segment %d\n", j); ! 59: p[0] = 0; /* put zero in first byte */ ! 60: } ! 61: ! 62: /* free each segment */ ! 63: for(j = 0; j < i; j++) { ! 64: printf("freeing segment %d\n", j); ! 65: DOSFREESEG(seglist[j]); ! 66: } ! 67: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.