VSTORAGE.MOD 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. IMPLEMENTATION MODULE VStorage;
  2. (*
  3. * REPERTOIRE
  4. * Release 1.6
  5. * By Charles Bradford and Cole Brecheen
  6. * (c) Copyright 1985-1992 PMI
  7. * Green Bay, Wisconsin
  8. * All rights reserved
  9. * (414) 468-6040
  10. *
  11. * $Header: D:/logfiles/mods/vstorage.mov 1.6 10 Mar 1991 15:32:48 coleb $
  12. *
  13. *)
  14. IMPORT ErrorManager;
  15. IMPORT LowLevel;
  16. IMPORT Numbers;
  17. IMPORT StringIO;
  18. IMPORT SYSTEM;
  19. IMPORT FAPI;
  20. IMPORT Storage;
  21. VAR
  22. Initialized : BOOLEAN;
  23. TYPE
  24. MemHandle = POINTER TO SYSTEM.ADDRESS;
  25. VAR
  26. LockedHandles: CARDINAL;
  27. PROCEDURE MemAvail(within: CARDINAL) : CARDINAL;
  28. VAR
  29. lasttry, lowtry, try : CARDINAL;
  30. BEGIN
  31. lasttry := 65535;
  32. try := 65535;
  33. lowtry := 0;
  34. LOOP
  35. WHILE (NOT DosAvail(try)) AND (try>16) DO
  36. lasttry := try;
  37. try := lowtry + (try-lowtry) DIV 2;
  38. END;
  39. IF ((lasttry-try) > within) AND (try>16) THEN
  40. lowtry := try;
  41. try := try + (lasttry-try) DIV 2;
  42. ELSE
  43. EXIT;
  44. END;
  45. END;
  46. IF try<=16 THEN
  47. RETURN 0;
  48. ELSE
  49. RETURN try;
  50. END;
  51. END MemAvail;
  52. PROCEDURE AvailDOS(): LONGINT;
  53. (* Returns the # of bytes of DOS memory currently available; the
  54. standard Storage module doesn't have such a function.*)
  55. BEGIN
  56. RETURN Numbers.Lc( MemAvail( 50) );
  57. END AvailDOS;
  58. PROCEDURE IsAvailDOS( NumOfBytes: CARDINAL ): BOOLEAN;
  59. BEGIN
  60. RETURN DosAvail( NumOfBytes );
  61. END IsAvailDOS;
  62. PROCEDURE AllocDOS( VAR handle: MemHandle; size: CARDINAL): BOOLEAN;
  63. (* Allocate a block of DOS memory of given size in bytes, and
  64. return a handle to that memory. *)
  65. BEGIN
  66. IF NOT DosAvail( size) THEN
  67. RETURN FALSE;
  68. ELSE
  69. DosAlloc( handle, size);
  70. RETURN TRUE;
  71. END;
  72. END AllocDOS;
  73. PROCEDURE DeallocDOS( handle: MemHandle; size: CARDINAL);
  74. (* Deallocate the block of DOS memory of specified size
  75. associated with the given handle. *)
  76. BEGIN
  77. DosDealloc( handle, size);
  78. END DeallocDOS;
  79. PROCEDURE LockDOS( handle: MemHandle): SYSTEM.ADDRESS;
  80. (* Locks the DOS memory associated with the given handle into
  81. real memory and returns the real starting address. *)
  82. BEGIN
  83. INC( LockedHandles );
  84. IF LockedHandles > 4 THEN
  85. ErrorManager.WARN( "Danger. More than 4 handles locked");
  86. END;
  87. RETURN handle;
  88. END LockDOS;
  89. PROCEDURE UnLockDOS( handle: MemHandle);
  90. (* Unlock the DOS memory associated with the given handle. *)
  91. BEGIN
  92. (* we don't need to do anything to unlock DOS memory *)
  93. IF LockedHandles = 0 THEN
  94. ErrorManager.WARN( "Handle not locked");
  95. ELSE
  96. DEC( LockedHandles );
  97. END;
  98. END UnLockDOS;
  99. PROCEDURE ReadDOS( handle: MemHandle; offset: CARDINAL;
  100. dest: SYSTEM.ADDRESS; size: CARDINAL);
  101. (* Moves size bytes starting at offset bytes from the beginning
  102. of virtual memory for handle to real memory address dest. *)
  103. BEGIN
  104. LowLevel.Move( LowLevel.AddAddr(SYSTEM.ADDRESS( handle),
  105. offset), dest, size);
  106. END ReadDOS;
  107. PROCEDURE WriteDOS( handle: MemHandle; offset: CARDINAL;
  108. source: SYSTEM.ADDRESS; size: CARDINAL);
  109. (* Moves size bytes from real memory address source to offset
  110. bytes from the beginning of virtual memory for handle. *)
  111. BEGIN
  112. LowLevel.Move( source, LowLevel.AddAddr(SYSTEM.ADDRESS(
  113. handle), offset), size);
  114. END WriteDOS;
  115. PROCEDURE NotInEms( TheHandle: MemHandle ): BOOLEAN;
  116. BEGIN
  117. RETURN FALSE;
  118. END NotInEms;
  119. PROCEDURE NotUsing(): BOOLEAN;
  120. BEGIN
  121. RETURN FALSE;
  122. END NotUsing;
  123. PROCEDURE NilHandle( VAR TheHandle: MemHandle );
  124. BEGIN
  125. TheHandle := NIL;
  126. END NilHandle;
  127. PROCEDURE SameHandle( handle1, handle2: MemHandle ): BOOLEAN;
  128. BEGIN
  129. RETURN handle1 = handle2;
  130. END SameHandle;
  131. PROCEDURE Reminder1( dumc: CARDINAL): BOOLEAN;
  132. BEGIN
  133. StringIO.WriteStr( StringIO.outp,
  134. "Please assign VStorage.DosAvail (IMPORT Init...;).");
  135. HALT;
  136. RETURN FALSE;
  137. END Reminder1;
  138. PROCEDURE Reminder2( VAR dumadr: SYSTEM.ADDRESS; dumsize: CARDINAL );
  139. BEGIN
  140. StringIO.WriteStr( StringIO.outp,
  141. "Please assign a DOS memory allocator to VStorage.DosAlloc (IMPORT Init...;).");
  142. HALT;
  143. END Reminder2;
  144. PROCEDURE Reminder3( VAR dumadr: SYSTEM.ADDRESS; dumsize: CARDINAL );
  145. BEGIN
  146. StringIO.WriteStr( StringIO.outp, "Please assign VStorage.DosDealloc.");
  147. HALT;
  148. END Reminder3;
  149. PROCEDURE AvailShell( dumc: CARDINAL): BOOLEAN;
  150. VAR
  151. errcode: CARDINAL;
  152. machineMode:CARDINAL;
  153. BEGIN
  154. machineMode:=0;
  155. errcode := FAPI.DOSGETMACHINEMODE(SYSTEM.ADR(machineMode));
  156. IF machineMode = FAPI.MODE_REAL THEN
  157. RETURN Storage.Available( dumc );
  158. ELSE
  159. RETURN TRUE;
  160. END;
  161. END AvailShell;
  162. PROCEDURE AllocShell( VAR dumadr: SYSTEM.ADDRESS; dumsize: CARDINAL );
  163. BEGIN
  164. Storage.ALLOCATE( dumadr, dumsize );
  165. END AllocShell;
  166. PROCEDURE DeallocShell( VAR dumadr: SYSTEM.ADDRESS; dumsize: CARDINAL );
  167. BEGIN
  168. Storage.DEALLOCATE( dumadr, dumsize );
  169. END DeallocShell;
  170. PROCEDURE Init();
  171. BEGIN
  172. IF Initialized THEN
  173. RETURN;
  174. ELSE
  175. Initialized := TRUE;
  176. END;
  177. ErrorManager.Init();
  178. LowLevel.Init();
  179. Numbers.Init();
  180. StringIO.Init();
  181. (*EntryDiag:
  182. StringIO.WriteEol( 1, 'Entering VStorage' );
  183. :EntryDiag*)
  184. LockedHandles := 0;
  185. AvailMem := AvailDOS;
  186. InEms := NotInEms;
  187. IsAvailable := IsAvailDOS;
  188. AllocMem := AllocDOS;
  189. DeallocMem := DeallocDOS;
  190. LockMem := LockDOS;
  191. UnLockMem := UnLockDOS;
  192. ReadMem := ReadDOS;
  193. WriteMem := WriteDOS;
  194. UsingEms := NotUsing;
  195. DosAvail := AvailShell;
  196. DosAlloc := AllocShell;
  197. DosDealloc := DeallocShell;
  198. (*EntryDiag:
  199. StringIO.WriteEol( 1, 'Exiting VStorage' );
  200. :EntryDiag*)
  201. END Init;
  202. BEGIN
  203. Initialized := FALSE;
  204. Init();
  205. END VStorage.