home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / msc7.zip / XMEM.CPP < prev    next >
C/C++ Source or Header  |  1992-02-14  |  4KB  |  201 lines

  1. // (C) Copyright 1992 Qualitas, Inc.  All rights reserved.
  2. //
  3. // xmem.cpp - example usage of memory classes in DPMI class library
  4. //
  5.  
  6. #include <iostream.h>
  7. #include <iomanip.h>
  8. #include "dpmihost.h"
  9. #include "segment.h"
  10.  
  11. //
  12. // Function to output AbstractSegment and derived classes
  13. //
  14. ostream& operator << (ostream& str, AbstractSegment& s)
  15. {
  16.     void far *p=s.ptrTo();
  17.  
  18.     str.fill('0');
  19.     str << "Segment: selector=" << setw(4) << hex << FP_SEG(p);
  20.     str << " Base=" << setw(8) << s.segmentBase();
  21.     str << " Size=" << setw(8) << s.segmentSize();
  22.  
  23.     if (s.queryProp(present))
  24.         str << " present";
  25.     else
  26.         str << " not present";
  27.  
  28.     if (s.queryProp(executable))
  29.     {
  30.         str << " code";
  31.         if (s.queryProp(readable))
  32.             str << " readable";
  33.         else
  34.             str << " execute_only";
  35.     }
  36.     else
  37.     {
  38.         str << " data";
  39.         if (s.queryProp(readable))
  40.             str << " writable";
  41.         else
  42.             str << " read_only";
  43.     }
  44.  
  45.  
  46.     if (s.queryProp(big))
  47.         str << " big";
  48.  
  49.  
  50.     if (s.queryProp(expandDown))
  51.         str << " expand_down";
  52.  
  53.  
  54.     str << "\n";
  55.     return str;
  56. }
  57.  
  58.  
  59. #define MAXMEMSEG 512
  60. //
  61. // This function illustrates the use of the MemorySegment class for 
  62. // allocating 64 KB blocks of memory from DPMI.
  63. //
  64. void AllocateMemory(void)
  65. {
  66.     int i;
  67.     uLong totmem;
  68.  
  69.     // declare an array of pointers to MemorySegment
  70.     MemorySegment* pMemSegs[MAXMEMSEG];
  71.  
  72.     cout << "Memory Allocation Test\n";
  73.  
  74.     for (i=0, totmem=0; i < MAXMEMSEG ; i++)
  75.     {
  76.         // allocate a new 64 KB MemorySegment (arg==0 means 64 KB)
  77.         pMemSegs[i] = new MemorySegment(0);
  78.  
  79.         // if the pointer to the segment is zero,the allocation failed
  80.         if (pMemSegs[i]->ptrTo() == 0)
  81.             break;
  82.         else
  83.         {    //output the segment information
  84.             cout << *pMemSegs[i];
  85.  
  86.             totmem += 0x10000;    // tally total memory alloc'ed
  87.         }
  88.     }
  89.     cout << "Total Memory Allocated: " << dec << totmem << " bytes\n";
  90.  
  91.     // free the MemorySegments - releases memory to DPMI host
  92.     for (--i; i >= 0; i--)
  93.         delete pMemSegs[i];
  94. }
  95.  
  96.  
  97. //
  98. // This function illustrates the use of the Segment class for creating
  99. // a window on an arbitrary region of the linear address space.
  100. //
  101. void PeekMemory(void)
  102. {
  103.     Segment s;                // create a segment
  104.     uChar far *pSeg = (uChar far *)s.ptrTo();//   and a pointer to it
  105.     s.resize(0x80);                // set its size to 128 bytes
  106.  
  107.     uLong linearAddress;            // address to examine
  108.     
  109.     while (1)
  110.     {
  111.         cout << "\nEnter linear address to examine (0 to end): ";
  112.         cin >> hex >> linearAddress;
  113.  
  114.         if (linearAddress == 0)
  115.             break;
  116.  
  117.         s.move(linearAddress);        // position the segment
  118.  
  119.         int i=0;
  120.         do                // display segment data
  121.         {
  122.             cout << hex << setw(2) << (int)pSeg[i] << " ";
  123.             i++;
  124.             if ((i % 16) == 0) cout << "\n";
  125.  
  126.         } while (i < 128);
  127.         cout << "\n";
  128.     };
  129. }
  130.  
  131.  
  132. void ShowProperties(void)
  133. {
  134.     Segment s;
  135.     cout << "\nTest Segment Properties\n";
  136.     cout << " Default properties:\n " << s;
  137.  
  138.     s-present;
  139.     cout << " Make it not present:\n " << s;
  140.  
  141.     s+executable;
  142.     cout << " Make it a code segment:\n " << s;
  143.  
  144.     s-executable; s+present; s-writable;
  145.     cout << " Make it present read-only data:\n " << s;
  146. }
  147.  
  148. // 
  149. // This function illustrates other members and derived classes of 
  150. // AbstractSegment
  151. //
  152. void ShowMisc(void)
  153. {
  154.     // DOSMemory class allocates memory from DOS
  155.  
  156.     cout << "\nTest other memory classes\n";
  157.  
  158.     DOSMemory d(0x10);        // create a 0x10 paragraph block
  159.     cout << " A 0x10 paragraph DOSMemory block:\n " << d;
  160.  
  161.     // Resize it to 8 paragraphs
  162.     d.resize(8);
  163.     cout << " DOSMemory block resized to 8 paragraphs:\n " << d;
  164.  
  165.     // Create an alias to this using an alternate Segment constructor
  166.  
  167.     Segment aliasToDOSMemory(d); aliasToDOSMemory+executable;
  168.     cout << " A code alias to that segment:\n " << aliasToDOSMemory;
  169.  
  170.     // Create a specific descriptor
  171.  
  172.     Segment specific(0xf);
  173.     cout << " Specific descriptor 0xFh:\n " << specific;
  174.  
  175.     // Create a CommonRealSegment at paragraph 0xb800
  176.  
  177.     CommonRealSegment crs(0xb800);
  178.     cout << " A common real segment at paragraph 0xb800:\n " << crs;
  179.  
  180.     // Create a Huge Segment that maps the low megabyte:
  181.  
  182.     HugeSegment h(0x100000); h.move(0);
  183.     cout << " A huge segment spanning the low 1 MB:\n " << h;
  184. }
  185.  
  186.  
  187. void main(void)
  188. {
  189.     DPMIhost dpmi;
  190.  
  191.     if (dpmi.getStatus() == DPMIok)
  192.     {
  193.         dpmi.enterProtectedMode();
  194.         AllocateMemory();
  195.         PeekMemory();
  196.         ShowProperties();
  197.         ShowMisc();
  198.     }
  199.     else
  200.         cout << "DPMI host not present\n";
  201. }