home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l331 / 1.ddi / EXAMPLES / 386VMM / VM.C < prev   
Encoding:
C/C++ Source or Header  |  1991-02-04  |  2.4 KB  |  72 lines

  1. /*
  2.  * This virtual memory example program allocates a buffer larger than
  3.  * available physical memory and then scans it to cause page faults.
  4.  *
  5.  * This is a "poorly behaved" application for virtual memory, in that it
  6.  * repeatedly does linear scans of the entire address space, which will 
  7.  * cause thrashing under any VM system.  It is intended only to demonstrate
  8.  * paging activity under 386|VMM.
  9.  */
  10.  
  11. pragma on(quiet);
  12. #include <stdio.h>
  13. #include <pltypes.h>
  14. #include <pharlap.h>
  15.  
  16. void main()
  17. {
  18.     VM_STATS statbuf;    /* memory statistics obtained from DOS-X */
  19.     ULONG    nbytes;        /* number of bytes to allocate */
  20.     long    *bufp;        /* ptr to allocated memory buffer */
  21.     long    *scanp;        /* scan ptr thru buffer */
  22.     int    i;        /* loop counter */
  23.     int    errv;        /* error return from system call */
  24.  
  25. /*
  26.  * Get the memory statistics.  We will allocate ten pages more than the
  27.  * total physical memory on the system, which will guarantee the buffer is
  28.  * larger than available physical memory.
  29.  */
  30.      errv = _dx_vm_stats(&statbuf, FALSE);
  31.     nbytes = (statbuf.vm_nconvpg + statbuf.vm_nextpg + 10) * 4096;
  32.  
  33. /*
  34.  * Allocate the buffer.  Since malloc actually goes and touches the memory
  35.  * after it grows the segment, it will cause some paging.
  36.  */
  37.     printf("Allocating 0x%08X bytes\n", nbytes);
  38.      if ((bufp = (long *) malloc(nbytes)) == NULL)
  39.     {
  40.         printf("Error allocating buffer of 0x%08X bytes\n", nbytes);
  41.         return;
  42.     }
  43.  
  44. /*
  45.  * Initialize the buffer.  This will cause either some or a lot of paging,
  46.  * depending on how malloc touched the memory (i.e., depending on whether
  47.  * the pages at the beginning or the end of the buffer are the ones that
  48.  * are initially paged out).
  49.  */
  50.     printf("Initializing buffer\n");
  51.      for (i = 0, scanp = bufp; i < nbytes / 4; ++ i, ++ scanp)
  52.         *scanp = i;
  53.  
  54. /*
  55.  * Scan the buffer to check the data.  Assuming the buffer is too big to fit
  56.  * in physical memory, this should cause a page fault on every page, because
  57.  * the pages at the beginning of the buffer should have been paged out as the
  58.  * end of the buffer was initialized by the loop above, and as each page gets
  59.  * brought in during this loop, it should force out a page further along in 
  60.  * the buffer.
  61.  */
  62.     printf("Scanning buffer\n");
  63.      for (i = 0, scanp = bufp; i < nbytes / 4; ++ i, ++ scanp)
  64.         if (*scanp != i)
  65.         {
  66.             printf("Data error on buffer check\n");
  67.             return;
  68.         }
  69.  
  70.     return;
  71. }
  72.