home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bc3.zip / SEGMENT.H < prev    next >
C/C++ Source or Header  |  1992-02-14  |  8KB  |  264 lines

  1. //
  2. // (c) Copyright 1992, Qualitas, Inc. All Rights Reserved
  3. //
  4. // segment.h - header file for AbstractSegment classes
  5. //
  6. //
  7. // The class hierarchy is 
  8. //
  9. //
  10. //      Block            AbstractSegment
  11. //        |                |
  12. //    ---------    --------------------------------------------
  13. //    |    |    |      |        |        |
  14. //    |    | Segment   HugeSegment          DOSMemory       CommonRealSegment
  15. //    |    |    |        |
  16. //    |    ------        |
  17. //    |       |        |
  18. //    |     MemorySegment    |
  19. //    |            |
  20. //    -------------------------
  21. //            |
  22. //          HugeMemorySegment
  23.  
  24.  
  25. #include <dos.h>
  26. #include "dpmi.h"
  27.  
  28. #ifndef __SEGMENT__
  29. #define __SEGMENT__
  30.  
  31.  
  32. //
  33. // Segment Properties
  34. //
  35. // The members of the SegmentProperty enumeration correspond to the 
  36. // fields of the machine descriptor.  The enumeration is used to provide
  37. // an abstraction for manipulating descriptor attributes.
  38. //
  39. typedef enum SegmentProperty
  40. {
  41.     present,
  42.     executable,
  43.     readable,
  44.     writable,
  45.     big,
  46.     expandDown
  47. } SegmentProp_t;
  48.  
  49.  
  50. //
  51. // Class Block
  52. //
  53. // A Block corresponds to raw memory allocated from DPMI via service
  54. // 0501h.  The class holds the DPMI memory handle, the block base address,
  55. // and its size.  The allowable operations on raw blocks are querying its
  56. // parameters, and resizing.  The destructor releases the block to the
  57. // DPMI host.
  58. //
  59. // A Block may be used directly, but more commonly is used to
  60. // derive a MemorySegment or HugeMemorySegment object (see below).
  61.  
  62. class Block
  63. {
  64. public:
  65.     Block(uLong size);            // constructor
  66.     ~Block(void);                // destructor
  67.     boolean setSize(uLong);            // resize function
  68.     uLong blockHandle(void){return handle;};// query block handle
  69.     uLong blockSize(void){return size;};    // query block size
  70.     uLong blockBase(void){return base;};    // query block base
  71.         
  72. protected:
  73.     uLong handle;
  74.     uLong base;
  75.     uLong size;
  76. };
  77.  
  78.  
  79. //
  80. // Class AbstractSegment
  81. //
  82. // An AbstractSegment corresponds to a descriptor. The class is used to
  83. // derive the three types of descriptors available from DPMI: (1) generic
  84. // descriptors, which correspond to class Segment (including specific
  85. // descriptors, (2) Common real descriptors (DPMI function 0002h), which
  86. // correspond to class CommonRealSegment, and (3) DOS memory descriptors,
  87. // which correspond to class DOSMemory.
  88. //
  89. // The only state information maintained in the object is the selector of
  90. // the descriptor.  All methods exchange data with the descriptor itself
  91. // via DPMI services.  
  92. //
  93. // The ptrTo method yields a far pointer that can be used to access 
  94. // the address space mapped by the descriptor.  If for any reason the DPMI
  95. // fails to create the descriptor, ptrTo returns a NULL pointer.
  96. //
  97. class AbstractSegment
  98. {
  99. public:
  100.     virtual    uLong    segmentSize(void);       // query segment size
  101.     virtual    uLong    segmentBase(void);       // query segment base
  102.     virtual boolean resize(uShort)=0;       // resize function
  103.     virtual boolean move(uLong)=0;           // set segment base
  104.     virtual boolean    operator+(SegmentProp_t)=0;// add a property
  105.     virtual boolean    operator-(SegmentProp_t)=0;// remove a property
  106.     virtual boolean    queryProp(SegmentProp_t);  // query property
  107.  
  108.     void far *ptrTo(void)               // return pointer
  109.         { return MK_FP(selector, 0);};
  110.  
  111. protected:
  112.     selector_t selector;
  113.  
  114. };
  115.  
  116.  
  117. // Class Segment
  118. //
  119. // A Segment corresponds to a fully modifiable LDT descriptor created by
  120. // either DPMI function 0000h (Allocate Descriptor), DPMI function
  121. // 000Ah (Create Alias Descriptor), or DPMI function 000Dh (Allocate
  122. // Specific Descriptor).  The DPMI service invoked depends on which
  123. // constructor is used.
  124. //
  125. // The default properties are present and writable.  New Segments have
  126. // a base of zero and a size of one.
  127. //
  128. // The argument for the resize method is in bytes, and size==0
  129. // specifies a size of 64 KB.
  130. //
  131. // Specific descriptors must be in the range 04h to 0FFh.
  132. //
  133. class Segment : public AbstractSegment
  134. {
  135. public:
  136.     Segment(void);                // generic descriptor
  137.     Segment(selector_t specific);        // specific descriptor
  138.     Segment(AbstractSegment& segment);    // alias existing descriptor
  139.     ~Segment(void);                // release descriptor to DPMI
  140.  
  141.     virtual boolean operator+(SegmentProp_t);//add property
  142.     virtual boolean operator-(SegmentProp_t);//remove property
  143.     virtual boolean resize(uShort);         //resize
  144.     virtual boolean move(uLong);         //set base
  145. };
  146.  
  147.  
  148. // Class CommonRealSegment
  149. //
  150. // CommonRealSegments correspond to descriptors created by DPMI function
  151. // 0002h (Segment to Descriptor).  DPMI restricts these segments from
  152. // being modified or deleted. Thus, the move, resize, add and remove 
  153. // property methods are degenerate, and there is no destructor defined 
  154. // to free the descriptor.
  155. //
  156. class CommonRealSegment : public AbstractSegment
  157. {
  158. public:
  159.     CommonRealSegment(uShort paragraph);     // Create descriptor for para
  160.  
  161.     virtual boolean operator+(SegmentProp_t) // add property returns FALSE
  162.         {return FALSE;};
  163.     virtual boolean operator-(SegmentProp_t) // rem property returns FALSE
  164.         {return FALSE;};
  165.     virtual boolean resize(uShort)          // returns FALSE
  166.         {return FALSE;};
  167.     virtual boolean move(uLong)          // returns FALSE
  168.         {return FALSE;};
  169. };
  170.  
  171. // Class DOSMemory
  172. //
  173. // A DOSMemory object corresponds to segments allocated via DPMI function
  174. // 0100h.  The class requires a special destructor which calls DPMI function
  175. // 0101h.  DPMI restricts DOSMemory descriptors from being modified, so
  176. // the add property, remove property, and move methods are degenerate.  The
  177. // resize method maps to DPMI function 0102h.
  178. //
  179. // Note that the argument to the constructor is the number of paragraphs to
  180. // allocate.  DOSMemory objects are present and writable.
  181. //
  182. class DOSMemory : public AbstractSegment
  183. {
  184. public:
  185.     DOSMemory(uShort nParagraphs);          // constructor
  186.     ~DOSMemory(void);              // destructor
  187.     virtual boolean resize(uShort);          // arg is paragraph count
  188.     virtual boolean operator+(SegmentProp_t)  // returns FALSE
  189.         {return FALSE;};
  190.     virtual boolean operator-(SegmentProp_t)  // returns FALSE
  191.         {return FALSE;};
  192.     virtual boolean move(uLong)          // returns FALSE
  193.         {return FALSE;};
  194.  
  195. };
  196.  
  197. // Class HugeSegment
  198. //
  199. // A HugeSegment corresponds to a set of consecutive descriptors, which 
  200. // are set up to span a memory region of arbitrary size.  Operations on
  201. // a HugeSegment, such as add or remove property, affect all of its
  202. // component descriptors.  The difference between the bases of consecutive
  203. // descriptors is 64 KB.  The destructor deletes all component descriptors.
  204. //
  205. class HugeSegment : public AbstractSegment
  206. {
  207. public:
  208.     HugeSegment(uLong size);          // constructor
  209.     ~HugeSegment(void);              // destructor
  210.     virtual boolean operator+(SegmentProp_t); // add property to all descs
  211.     virtual boolean operator-(SegmentProp_t); // rem property
  212.     virtual boolean resize(uShort);          // resize
  213.     virtual boolean move(uLong);          // set base
  214.     virtual    uLong    segmentSize(void)      // query segment size
  215.         {return abstractSize;};
  216.     virtual boolean    resize(uLong);          // resize
  217.  
  218. protected:
  219.     int nDescriptors;
  220.     uLong abstractSize;
  221. };
  222.  
  223. //
  224. // Class MemorySegment
  225. //
  226. // A MemorySegment is a generic descriptor mapped to a block of
  227. // DPMI memory of size 64 KB or less.  The constructors 
  228. // allow the descriptor to be created by either DPMI function 0000h
  229. // (Allocate Descriptor) or DPMI function 000Dh (Allocate Specific
  230. // Descriptor). The move method is degenerate, as there is no way
  231. // to move a DPMI memory block.  
  232. //
  233. // The size arguments to the constructors are in bytes, and size==0
  234. // specifies a size of 64 KB.
  235. //
  236. class MemorySegment : public Segment, public Block
  237. {
  238. public:
  239.     MemorySegment(uShort size);        // constructor
  240.     MemorySegment(uShort size, selector_t specific);
  241.     virtual boolean resize(uShort);        // resize
  242.     virtual boolean move(uLong)        // returns FALSE
  243.         {return FALSE;};
  244.     virtual uLong segmentSize(void);    // return block size
  245. };
  246.  
  247. //
  248. // Class HugeMemorySegment
  249. //
  250. // A HugeMemorySegment is a Block whose linear space is mapped by 
  251. // a HugeSegment.  The move method is degenerate.
  252. //
  253. class HugeMemorySegment : public HugeSegment, public Block
  254. {
  255. public:
  256.     HugeMemorySegment(uLong size);        // constructor
  257.     virtual boolean resize(uShort);        // resize
  258.     virtual boolean resize(uLong);        // resize
  259.     virtual boolean move(uLong)        // returns FALSE
  260.         {return FALSE;};
  261. };
  262.  
  263. #endif
  264.