home *** CD-ROM | disk | FTP | other *** search
- /*
- * This virtual memory example program allocates a buffer larger than
- * available physical memory and then scans it to cause page faults.
- *
- * This is a "poorly behaved" application for virtual memory, in that it
- * repeatedly does linear scans of the entire address space, which will
- * cause thrashing under any VM system. It is intended only to demonstrate
- * paging activity under 386|VMM.
- */
-
- pragma on(quiet);
- #include <stdio.h>
- #include <pltypes.h>
- #include <pharlap.h>
-
- void main()
- {
- VM_STATS statbuf; /* memory statistics obtained from DOS-X */
- ULONG nbytes; /* number of bytes to allocate */
- long *bufp; /* ptr to allocated memory buffer */
- long *scanp; /* scan ptr thru buffer */
- int i; /* loop counter */
- int errv; /* error return from system call */
-
- /*
- * Get the memory statistics. We will allocate ten pages more than the
- * total physical memory on the system, which will guarantee the buffer is
- * larger than available physical memory.
- */
- errv = _dx_vm_stats(&statbuf, FALSE);
- nbytes = (statbuf.vm_nconvpg + statbuf.vm_nextpg + 10) * 4096;
-
- /*
- * Allocate the buffer. Since malloc actually goes and touches the memory
- * after it grows the segment, it will cause some paging.
- */
- printf("Allocating 0x%08X bytes\n", nbytes);
- if ((bufp = (long *) malloc(nbytes)) == NULL)
- {
- printf("Error allocating buffer of 0x%08X bytes\n", nbytes);
- return;
- }
-
- /*
- * Initialize the buffer. This will cause either some or a lot of paging,
- * depending on how malloc touched the memory (i.e., depending on whether
- * the pages at the beginning or the end of the buffer are the ones that
- * are initially paged out).
- */
- printf("Initializing buffer\n");
- for (i = 0, scanp = bufp; i < nbytes / 4; ++ i, ++ scanp)
- *scanp = i;
-
- /*
- * Scan the buffer to check the data. Assuming the buffer is too big to fit
- * in physical memory, this should cause a page fault on every page, because
- * the pages at the beginning of the buffer should have been paged out as the
- * end of the buffer was initialized by the loop above, and as each page gets
- * brought in during this loop, it should force out a page further along in
- * the buffer.
- */
- printf("Scanning buffer\n");
- for (i = 0, scanp = bufp; i < nbytes / 4; ++ i, ++ scanp)
- if (*scanp != i)
- {
- printf("Data error on buffer check\n");
- return;
- }
-
- return;
- }
-