home *** CD-ROM | disk | FTP | other *** search
- {$R-,S-,F-,O-,I-,B-}
-
- unit eco_swap;
-
- interface
-
- const
- swaploc : array[boolean] of string[7] = ('on disk', 'in EMS');
-
- useemsifavailable : boolean = true;
- bytesswapped : longint = 0;
- emsallocated : boolean = false;
- fileallocated : boolean = false;
-
-
-
- function execwithswap(path, cmdline: string): word;
- function initexecswap(lasttosave: pointer; swapfilename: string): boolean;
- procedure shutdownexecswap;
-
-
-
-
- implementation
-
-
-
- var
- emshandle : word; {handle of ems allocation block}
- frameseg : word; {segment of ems page frame}
- filehandle : word; {dos handle of swap file}
- swapname : string[80]; {asciiz name of swap file}
- saveexit : pointer; {exit chain pointer}
-
- {$L eco_swap}
- function execwithswap(path, cmdline : string) : word; external;
- procedure firsttosave; external;
- function allocateswapfile : boolean; external;
- procedure deallocateswapfile; external;
-
- {$F+} {These routines could be interfaced for general use}
- function emsinstalled : boolean; external;
- function emspageframe : word; external;
- function allocateemspages(numpages : word) : word; external;
- procedure deallocateemshandle(handle : word); external;
- function defaultdrive : char; external;
- function diskfree(drive : byte) : longint; external;
-
- procedure execswapexit;
- begin
- exitproc := saveexit;
- shutdownexecswap;
- end;
- {$F-}
-
- procedure shutdownexecswap;
- begin
- if emsallocated then begin
- deallocateemshandle(emshandle);
- emsallocated := false;
- end else if fileallocated then begin
- deallocateswapfile;
- fileallocated := false;
- end;
- end;
-
- function ptrdiff(h, l : pointer) : longint;
- type
- os = record o, s : word; end; {convenient typecast}
- begin
- ptrdiff := (longint(os(h).s) shl 4+os(h).o)-
- (longint(os(l).s) shl 4+os(l).o);
- end;
-
- function initexecswap(lasttosave : pointer;
- swapfilename : string) : boolean;
- const
- emspagesize = 16384; {bytes in a standard ems page}
- var
- pagesinems : word; {pages needed in ems}
- bytesfree : longint; {bytes free on swap file drive}
- drivechar : char; {drive letter for swap file}
- begin
- initexecswap := false;
-
- if emsallocated or fileallocated then
- exit;
- bytesswapped := ptrdiff(lasttosave, @firsttosave);
- if bytesswapped <= 0 then
- exit;
-
- if useemsifavailable and emsinstalled then begin
- pagesinems := (bytesswapped+emspagesize-1) div emspagesize;
- emshandle := allocateemspages(pagesinems);
- if emshandle <> $ffff then begin
- emsallocated := true;
- frameseg := emspageframe;
- if frameseg <> 0 then begin
- initexecswap := true;
- exit;
- end;
- end;
- end;
- if length(swapfilename) <> 0 then begin
- swapname := swapfilename+#0;
- if pos(':', swapfilename) = 2 then
- drivechar := upcase(swapfilename[1])
- else
- drivechar := defaultdrive;
- bytesfree := diskfree(byte(drivechar)-$40);
- fileallocated := (bytesfree > bytesswapped) and allocateswapfile;
- if fileallocated then
- initexecswap := true;
- end;
- end;
-
- begin
- saveexit := exitproc;
- exitproc := @execswapexit;
- end. { originally written by Dr. Dobbs Journal }
-