|
|
Microsoft OS/2 SDK 03-01-1988
/*
* Example of DosAllocSeg/DosFreeSeg usage.
*
* Shows how to allocate a segment, create a
* pointer to it, access the segment, and free it.
* Creates one 16K segment by default, or the number
* specified on the command line. After all are allocated,
* writes a 0 into the first byte of each. If you run this on
* a system with swapping enabled you can allocate more segments
* than fit in physcical memory, and exercise your disk.
*
* This program works Family API mode, although obviously
* without the benefit of virtual memory & swapping.
*
* Compile as: cl -AL -G0 -Lp alloc.c
*
* Created by Microsoft Corp. 1986
*/
#define INCL_DOSMEMMGR
#include <os2def.h>
#include <malloc.h>
#include <bsedos.h>
#define SEGSIZE (16 * 1024) /* size of each segment allocated */
#define NOTSHARED 0
main(argc, argv)
int argc;
char *argv[];
{
USHORT *seglist; /* pointer to list of segments */
char *p; /* pointer used to access segments */
register int i, j;
int rc; /* system call return code */
if(argc == 2) /* number of segments to allocate */
i = atoi(argv[1]);
else
i = 1;
seglist = (USHORT *)malloc(2 * i); /* list of segment selectors */
/* allocate segment(s) */
for(j = 0; j < i; j++) {
rc = DosAllocSeg(SEGSIZE, (PSEL)&seglist[j], NOTSHARED);
if(rc == 0)
printf("allocated segment %d\n", j);
else {
printf("allocation failed on segment %d\n", j);
i = j;
}
}
/* write 0 into first byte of each segment */
for(j = 0; j < i; j++) {
SELECTOROF(p) = seglist[j]; /* make long pointer to seg */
OFFSETOF(p) = 0;
printf("touching segment %d\n", j);
p[0] = 0; /* put zero in first byte */
}
/* free each segment */
for(j = 0; j < i; j++) {
printf("freeing segment %d\n", j);
DosFreeSeg(seglist[j]);
}
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.