home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 9
/
CD_ASCQ_09_1193.iso
/
news
/
558
/
virtmem
/
virtmem.doc
< prev
next >
Wrap
Text File
|
1993-09-09
|
17KB
|
464 lines
(***************************************************************)
VIRTUAL MEMORY SYSTEM Version 1.0
From Patel Enterprises
(***************************************************************)
The following set of functions and procedures may be used in
your program to give your applications virtual memory capability.
When requesting memory, the system uses the following resources
(if available) in the order indicated:
Extended memory (XMS)
Expanded memory (EMS)
Conventional memory
Disk space
To properly set up this system, your program must call VMInit
prior to calling any other routines. Also, you must call VMClose
before exiting. The function/procedure descriptions below provide
further information on the functionality of each routine. Certain
limitations of this software is also described below.
You will need an extended memory manager (XMM) such as HIMEM.SYS
in order to access your XMS memory. You will also need an
expanded memory manager (EMM) to access your EMS memory. Both
EMM386 and QEMM386 have been thoroughly tested without any
problems. My recommendation is that you install only the XMM,
whenever possible, because XMS memory can be accessed quicker
than EMM memory. Also, if you have little or none of both types
of memory, or if you anticipate much use of disk space as
memory, some type of disk caching utility will help reduce
frequent disk access which slows down your program.
For your information, this software has been successfully used in
several commercially available applications. However, as with any
software, I cannot guarantee that this software will meet your
requirements or that the software operation will be uninterrupted
or error free.
In no event shall I be liable to you for any damages or loss of
any kind arising out of the use or the inability to use this
software. As such, the entire risk is assumed by you.
You may freely copy and distribute this software, provided that
no modifications are made to any of the files. Also, the files
must be distributed together, not individually.
Files in this package are:
VIRTMEM.DOC - this file
VIRTMEM6.TPU - Unit for TP 6.0
VIRTMEM7.TPU - Unit for TP 7.0
VIRTTEST.PAS - Sample code
If you do use this software, please send US$10 to the following
address. I will try to make available units for Turbo Pascal 5.5
or older upon request. I would appreciate comments and/or
suggestions from all. They may also be sent to me at this
address, or to me on EXEC-PC, the largest BBS in the world.
Jash Patel
325 Fawn Valley
Lansing, KS 66043-1723
(***************************************************************)
DATA TYPES and VARIABLES
(***************************************************************)
Type
VMHandleType - Handle used to identify virtual memory blocks.
VMMemoryType - Values that may be returned by VMQueryInfo to
indicate the type of memory used by the
virtual memory block. Possibles values: XMS,
EMS, CMS and DMS.
VMErrCodeType - A Word.
Var
VMMajor,
VMMinor : Word - These variables contain the version number
for the virtual memory system, for example,
if version 1.0, VMMajor = 1 and VMMinor = 0.
(***************************************************************)
FUNCTION/PROCEDURE DESCRIPTIONS
(***************************************************************)
Procedure VMInit (Path : String; SafetyPool : LongInt);
Parameters:
Path - Directory where disk memory is to be allocated.
Path may not be blank and must include a drive
and a directory. Example: C:\TEMP or A:\
SafetyPool - The virtual memory system will not allocate from
conventional memory when the largest contiguous
conventional memory block is less than or equal
to this value. If this value is less than 0,
SafetyPool defaults to 65536 bytes. Using a large
value (such as MaxLongInt) cause the virtual
memory manager not to dispense any conventional
memory. This is what you should do if you intend
to access many memory block simultaneously - see
VMLock.
Return value:
None
Note:
This procedure performs the necessary virtual memory system
initialization. It must be called prior to calling any other
procedure or functions. VMInit allocates all available
extended and/or expanded memory at this time, and then
allocates 64KB of conventional memory for use by the virtual
memory manager.
(***************************************************************)
Procedure VMClose;
Parameters:
None
Return value:
None
Note:
Your program must call VMClose before exiting. This procedure
performs the necessary clean up, including releasing the
extended, expanded and conventional memory allocated by
VMInit. All memory dispensed to you from the extended and/or
expanded memory pool is effectively releases even if you did
not release it by calling VMFree. Turbo Pascal automatically
releases all unfreed conventional memory. Disk memory,
however, if not released by you, will result in swap files on
disk. If this happens you should delete them.
(***************************************************************)
Function VMInstalled : Boolean;
Parameters:
None
Return value:
TRUE - Indicates that virtual memory manager is installed.
FALSE - Virtual memory manager not installed.
(***************************************************************)
Function VMXMSInstalled : Boolean;
Parameters:
None
Return value:
TRUE - Indicates that extended memory is available and will
be used when memory is requested.
FALSE - Extended memory not available.
Note:
Conventional memory and disk memory is always available even
if extended memory is not available.
(***************************************************************)
Function VMEMSInstalled : Boolean;
Parameters:
None
Return value:
TRUE - Indicates that expanded memory is available and will
be used when memory is requested.
FALSE - Expanded memory not available.
Note:
Conventional memory and disk memory is always available even
if expanded memory is not available.
(***************************************************************)
Function VMXMSAvail : LongInt;
Parameters:
None
Return value:
Returns the total amount of extended memory available.
(***************************************************************)
Function VMEMSAvail : LongInt;
Parameters:
None
Return value:
Returns the total amount of expanded memory available.
(***************************************************************)
Function VMCMSAvail : LongInt;
Parameters:
None
Return value:
Returns the size of the largest contiguous conventional memory
minus safety pool size specified when calling VMInit. This
value may also be obtained as follows:
If VMSafetyPool >= MaxAvail Then
VMCMSAvail := 0
Else
VMCMSAvail := MaxAvail-VMSafetyPool;
(***************************************************************)
Function VMDMSAvail : LongInt;
Parameters:
None
Return value:
Returns the number of free bytes on the disk drive specified
by the path passed to VMInit;
(***************************************************************)
Function VMAvailable : LongInt;
Parameters:
None
Return value:
Returns the sum of values returned by VMXMSAvail, VMEMSAvail,
VMCMSAvail and VMDMSAvail.
(***************************************************************)
Procedure VMAlloc (Var Handle : VMHandleType; Size : Word);
Parameters:
Handle - Handle for the memory block requested. After the
requested memory is allocated, the contents of this
variable is used to identify the memory block. You
must not discard this information in order to use
this memory.
Size - Amount of memory requested. This value must be larger
than 0 and should be an even number. If an odd number
is specified, the size is automatically incremented
by 1. The largest value may be 65535.
Return value:
None
Note:
The virtual memory manager will allow you to allocate a
maximum of 6553 blocks of memory, regardless of how much more
memory you may have. The virtual memory descriptor table must
reside within a single 64KB segment. Each table entry takes 10
bytes, hence 65536 Div 10 = 6553 entries. In order to use the
memory block you allocated, you must lock it first - see
VMLock.
(***************************************************************)
Procedure VMFree (Var Handle : VMHandleType);
Parameters:
Handle - Handle for the memory block to release. You must make
sure that there are no locks on this memory block, or
it will not be released.
Return value:
None
(***************************************************************)
Function VMLock (Var Handle : VMHandleType) : Pointer;
Parameters:
Handle - Handle for the memory block to lock.
Return value:
A pointer to the memory block in conventional memory.
Note:
When a virtual memory block is allocated by VMAlloc, it isn't
immediately available to you. When you lock the memory block,
it is moved into conventional memory unless, of course, if it
is already in conventional memory. For example, if the block
of memory resides on disk, locking it causes it to be read
into conventional memory, and its address return to you as a
pointer. At this point you may write to or read from this
memory. When you unlock it the data is sent back to the disk,
and the conventional memory is freed so that you may lock in
other blocks of virtual memory. The same applies to extended
and expanded memory. It is not necessary to unlock a block in
order to lock another, but it is recommended that you do, as
explained below.
You may have already realized a problem with this - what if
there isn't enough conventional memory to lock the virtual
memory block? VMLock will return NIL in this case. The key is
to allocate small blocks of memory instead of large ones.
Locked blocks take up conventional memory, ignoring safety
pool. Also, you should not have many locked blocks
simultaneously. A block should be locked, read from or written
to, and immediately unlocked.
Locking an already locked memory block do not use addition
conventional memory. Doing so only returns the pointer to the
memory used. In addition, the lock counter is incremented. A
block may be locked consecutively as many as 255 times.
(***************************************************************)
Procedure VMUnlock (Var Handle : VMHandleType);
Parameters:
Handle - Handle for the memory block to lock.
Return value:
None
Note:
Unlocks the virtual memory block if the lock counter is 1.
If the counter is larger than 1, it is decremented by 1. See
VMLock.
(***************************************************************)
Procedure VMQueryInfo ( Handle : VMHandleType;
Var Size : Word;
Var Mem : VMMemoryType;
Var Locked : Boolean);
Parameters:
Handle - Handle for the memory block.
Size - On return, indicates the size of the allocated block.
Mem - On return, indicates the type of memory used when
allocateby VMAlloc. Values may be XMS, EMS, CMS or
DMS.
Locked - Indicates whether this handle is locked or not.
Return value:
None
Note:
Queries for information about a virtual memory block.
(***************************************************************)
Function VMResult : VMErrCodeType;
Parameters:
None
Return value:
Error code.
Note:
Extensive error checking is performed by most of the
functions and procedures. When an error occures, the error
code is stored in an internal variable. Any error code other
than VMOk indicates that an error occured. VMResult returns
this code and resets the code to VMOk. As long as this code is
not VMOk, most of the procedures and functions will just
return without doing anything. Hence, you must call VMResult
after every procedure or function call with the exception of
the following:
Procedure VMQueryInfo;
Function VMXMSInstalled;
Function VMEMSInstalled;
Function VMXMSAvail;
Function VMEMSAvail;
Function VMCMSAvail;
(***************************************************************)
Function VMErrorMsg (ErrCode : VMErrCodeType) : String;
Parameters:
ErrCode - This is the error code returned by VMResult
Return value:
A string containing a brief description of the error.
Note:
The following are the possible error codes and their
descriptions:
VMOk - 'No error'
VMNoDriverErr - 'Driver not installed (VM)'
VMInitDoneErr - 'Driver already installed (VM)'
VMDriverInitErr - 'Insufficent memory to install driver (VM)'
VMInvalidPathErr - 'Invalid file path (VM)'
VMMemParamsErr - 'Invalid memory usage parameters (VM)'
VMHandleErr - 'Invalid handle (VM)'
VMInvalidDrvErr - 'Invalid drive letter (VM)'
VMOutOfMemErr - 'Insufficient virtual memory (VM)'
VMOutOfHandlesErr - 'No free handles available (VM)'
VMFileCreateErr - 'Swap file creation error (VM)'
VMFileAllocErr - 'Swap file allocation error (VM)'
VMFileUnallocErr - 'Swap file unallocation error (VM)'
VMHandleLockedErr - 'Handle is locked (VM)'
VMHandleUnlockedErr - 'Handle is not locked (VM)'
VMNoRequestErr - 'No virtual memory requested (VM)'
VMFileOpenErr - 'Error opening swap file (VM)'
VMFileReadErr - 'Error reading swap file (VM)'
VMFileWriteErr - 'Error writing swap file (VM)'
VMUnallocErr - 'Error unallocating memory (VM)'
VMTooManyLocksErr - 'Handle locked too many times (VM)'
XMSNoDriverErr - 'Driver not installed (XMS)'
XMSInitDoneErr - 'Driver already installed (XMS)'
XMSFunctionCallErr - 'Unknown function call (XMS)'
XMSVDiskFoundErr - 'RAMDISK detected (XMS)'
XMSA20Err - 'Error at handler A20 (XMS)'
XMSGeneralErr - 'General driver error (XMS)'
XMSUnrecoverableErr - 'Unrecoverable error (XMS)'
XMSNoHMAErr - 'HMA does not exist (XMS)'
XMSHMAInUseErr - 'HMA already in use (XMS)'
XMSHMAOutOfSpaceErr - 'HMA out of space (XMS)'
XMSHMANotAllocateErr - 'HMA not allocated (XMS)'
XMSA20StillOnErr - 'Handler A20 still on (XMS)'
XMSOutOfMemoryErr - 'Insufficient extended memory (XMS)'
XMSOutOfHandlesErr - 'No free handles available (XMS)'
XMSHandleErr - 'Invalid handle (XMS)'
XMSSrcHandleErr - 'Inavlid source handle (XMS)'
XMSSrcOffsetErr - 'Invalid source offset (XMS)'
XMSDestHandleErr - 'Invalid destination handle (XMS)'
XMSDestOffsetErr - 'Invalid destination offset (XMS)'
XMSInvalidLenErr - 'Invalid length (XMS)'
XMSOverlapErr - 'Illegal overlapping (XMS)'
XMSParityErr - 'Parity error (XMS)'
XMSUMBUnlockedErr - 'UMB is not locked (XMS)'
XMSUMBLockedErr - 'UMB is still locked (XMS)'
XMSUMBOverflowErr - 'UMB lock counter overflow (XMS)'
XMSUMBLockFailedErr - 'UMB cannot be locked (XMS)'
XMSUMBTooLargeErr - 'UMB too large (XMS)'
XMSOutOfUMBErr - 'No more UMB available (XMS)'
XMSUMBSegmentErr - 'Invalid UMB segment address (XMS)'
EMSNoDriverErr - 'Driver not installed (EMS)'
EMSInitDoneErr - 'Driver already installed (EMS)'
EMSDriverErr - 'Driver error (EMS)'
EMSHardwareErr - 'Hardware error (EMS)'
EMSHandleErr - 'Invalid handle (EMS)'
EMSFunctionCallErr - 'Unknown function call (EMS)'
EMSOutOfHandlesErr - 'No free handles available (EMS)'
EMSMapSaveRestErr - 'Error when saving/restoring mapping (EMS)'
EMSMoreAvailErr - 'More pages requested than are available (EMS)'
EMSMoreFreeErr - 'More pages requested than are free (EMS)'
EMSNoRequestErr - 'No pages requested (EMS)'
EMSLogicalPageErr - 'Logical page does not belong to handle (EMS)'
EMSPhysicalPageErr - 'Invalid physical page number (EMS)'
EMSMapRangeFullErr - 'Mapping memory range is full (EMS)'
EMSMapSaveDoneErr - 'Map save has already been done (EMS)'
EMSMapSaveFirstErr - 'Mapping must be saved first (EMS)'
(***************************************************************)