home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / pascal / swapunit.zip / SWAPUNIT.DOC < prev    next >
Text File  |  1992-01-20  |  17KB  |  357 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.                          SWAPUNIT: Turbo Pascal Unit
  7.  
  8.                          Copyright 1992  Kevin Kwast
  9.  
  10.  
  11.                               January 20, 1992
  12.  
  13.  
  14.  
  15.  
  16.               SWAPUNIT allows you to execute a program (like the Turbo
  17.         Pascal Exec procedure) with the maximum of available memory. Just
  18.         add SWAPUNIT to Uses clause and use SwapExec instead of the Turbo
  19.         Pascal Exec.  Memory previously used by your program will be
  20.         swapped to XMS, EMS, or the hard disk, freeing up more available
  21.         memory for the executed program or DOS shell.
  22.  
  23.               SWAPUNIT requires Turbo Pascal version 4 or above.  Units
  24.         are included which were compiled under versions 4.0, 5.5, and 6.0
  25.         for your convenience.  They are named SWAPUNIT.TP4 and so on.
  26.         Rename your choice to SWAPUNIT.TPU and use it.
  27.  
  28.               The results can be astounding, particularly with programs
  29.         that use a lot of stack or heap space.  The SwapExec procedure
  30.         is hand-coded in assembly language for optimal speed and minimum
  31.         overhead.  A child program or DOS shell can be spawned with as
  32.         little as 3K overhead.  Sound unbelievable? Try it!  You have 30
  33.         days to examine SWAPUNIT and see if it meets your needs; if you
  34.         decide to use SWAPUNIT, please register your usage with the
  35.         author.  See the end of this documentation for information.
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.               Here is the interface section of SWAPUNIT, which includes
  43.         comments describing the unit's usage and results:
  44.  
  45.         Unit SwapUnit;                  { Kevin A. Kwast }
  46.         (**********************************************************************)
  47.         (** This unit provides Turbo Pascal programs with an ability to swap **)
  48.         (** its memory blocks to XMS, EMS, or the hard disk (in that order)  **)
  49.         (** and EXEC another program.  Put this unit last in the Uses list   **)
  50.         (** for best performance (Turbo Pascal links in reverse).  The Try   **)
  51.         (** parameter allows the caller to specify which methods of swapping **)
  52.         (** will be attempted.  They are additive, so SwapToEMS+SwapToXMS    **)
  53.         (** will try swapping to EMS and XMS, but not to the disk.  The word **)
  54.         (** result of the SwapExec function is the result code, with the Hi  **)
  55.         (** and Lo bytes having the following meanings:                      **)
  56.         (**                                                                  **)
  57.         (**  Hi(Result) = 0:   Success, the swapping and execution was OK.   **)
  58.         (**                     The DOS errorlevel result is in Lo(Result).  **)
  59.         (**             = 1:   An error in the DOS memory chain prevented    **)
  60.         (**                     swapping.  This is unlikely.                 **)
  61.         (**             = 2:   Unable to swap the program to any of the      **)
  62.         (**                     methods allowed.                             **)
  63.         (**             = 3:   Swapping was alright, but the program name    **)
  64.         (**                     specified could not be executed.  The low    **)
  65.         (**                     byte describes the execution problem.        **)
  66.         (**                                                                  **)
  67.         (**  Lo(Result) = 2:   Program name could not be found to execute.   **)
  68.         (**             = 5:   Couldn't open the program name.               **)
  69.         (**             = 8:   Insufficient memory to run the program.       **)
  70.         (**                                                                  **)
  71.         (**********************************************************************)
  72.  
  73.         Interface
  74.  
  75.           Const
  76.               SwapToDisk = 4;
  77.               SwapToEMS = 2;
  78.               SwapToXMS = 1;
  79.               SwapToAny = 7;
  80.  
  81.           Function SwapExec(Program_Name, Command_Line, Swap_FName: String;
  82.                             Try: Byte) : Word;
  83.  
  84.           Function CheckEMS : Boolean;
  85.           Function CheckXMS : Boolean;
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.         -------------------
  93.         Function  SwapExec
  94.         -------------------
  95.           Parameter:  Program_Name     This is a Pascal string of any type
  96.                                        containing the program which will be
  97.                                        executed.  This string is a DOS file
  98.                                        specification which MUST include a
  99.                                        file name and extension, and may
  100.                                        include a drive and path.  Any drive
  101.                                        accessible by DOS is valid here.
  102.  
  103.           Parameter:  Command_Line     This command line will be passed to
  104.                                        the executed program.
  105.  
  106.           Parameter:  Swap_FName       This is a DOS file specification to
  107.                                        be used if it is necessary to swap
  108.                                        to disk.  This should be specified
  109.                                        if SwapExec will try to swap to disk.
  110.  
  111.           Parameter:  Try              This byte describes which storage
  112.                                        forms will be attempted in swapping.
  113.                                        The constants SwapToXMS, SwapToEMS,
  114.                                        SwapToDisk, and SwapToAny have been
  115.                                        provided for user convenience.
  116.  
  117.           Result                       The word returned by the function
  118.                                        describes the results of the swap
  119.                                        and program execution.  Check the
  120.                                        high and low bytes for individual
  121.                                        status codes documented above.
  122.  
  123.         -------------------
  124.         Function  CheckEMS
  125.         -------------------
  126.  
  127.           Result                       The boolean result returns TRUE if
  128.                                        LIM EMS 4.0 or compatible expanded
  129.                                        memory is available.  This is the
  130.                                        form of expanded memory required
  131.                                        in order to swap to EMS.
  132.  
  133.         -------------------
  134.         Function  CheckXMS
  135.         -------------------
  136.  
  137.           Result                       The boolean result returns TRUE if
  138.                                        a Microsoft XMS or compatible driver
  139.                                        provides extended memory support.
  140.                                        A driver like this is necessary to
  141.                                        provide XMS support to SWAPUNIT.
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.               In order to get the optimum performance from SWAPUNIT, you
  149.         need to understand how SWAPUNIT works, how Turbo Pascal allocates
  150.         memory, and how you can use Turbo Pascal's memory allocation
  151.         characteristics to your advantage.
  152.  
  153.               First of all, program memory is allocated as one large block
  154.         including the program code, unit code, data, stack, and heap space.
  155.         SWAPUNIT can save everything after itself in the memory block to
  156.         XMS, EMS, or the hard disk.  SwapExec will try XMS, then EMS, and
  157.         then try to save to disk (you can allow swapping to only some of
  158.         those forms with the Try parameter).  If swapping is successful,
  159.         the memory block is reduced in size and the child program is
  160.         executed.  After the child program returns, SWAPUNIT (which was
  161.         not swapped out) will reload the rest of the memory block and
  162.         restore the DOS memory block to its former status.  Graphically,
  163.         an example program being swapped looks like this:
  164.  
  165.           Before calling SwapExec:       After SWAPUNIT Swaps:       
  166.  
  167.             -------------------           -------------------
  168.             / 40K Free Memory \           /                 \
  169.             |=================|           /  All Of This Is \
  170.             | 500K Heap       |           /   Swapped Out   \
  171.             |=================|           /  And Available! \
  172.             | 16K Stack       |           /                 \
  173.             |=================|           /   574K Free     \
  174.             | 10K Data        |           /    Memory!      \
  175.             |=================|           |=================|
  176.             | 10K Units Code  |           | 2K SWAPUNIT     |
  177.             |=================|           |=================|
  178.             | 5K Program Code |           | 5K Program Code |
  179.             -------------------           -------------------
  180.  
  181.  
  182.               Since SWAPUNIT can only swap out what appears after itself
  183.         in memory, you want SWAPUNIT to be the first unit in memory, and
  184.         you want the program code (all the code in the main Pascal file)
  185.         segment to be as small as possible.  For those using Turbo Pascal
  186.         version 5 or above, compile your program with map file generation
  187.         (this is the /GS parameter in TPC, the command line compiler).
  188.         Take a look at the .MAP file to see how memory will be used by
  189.         the compiled program.  You will see your program's name appearing
  190.         as the first area in the .MAP file, with areas appearing later
  191.         (or higher) in memory further on in the MAP file.  Note that the
  192.         SYSTEM unit (which Turbo Pascal always includes) is automatically
  193.         linked as the last unit in memory; this is not a concern.
  194.  
  195.               SwapExec will swap out everything after the SWAPUNIT area
  196.         and restore it when SwapExec returns.  Turbo Pascal will always
  197.         put the data, stack, and heap where they can be swapped out, but
  198.         maximizing the unit and program code space which can be swapped
  199.         out requires a little inside knowledge.
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.               Thus, your goal is to have a small main program area with
  207.         SWAPUNIT immediately after that.  Here are a few tips and facts
  208.         to make this feat easier to accomplish:
  209.  
  210.         *  String constants will be in the code segment.  This means that
  211.            if your main program contains a great deal of literal strings
  212.            [such as things like: WriteLn('Hello there.');], your code
  213.            segment can get very large.  The best solution to this problem
  214.            is declaring your strings in the Const section (which are
  215.            allocated in the data segment) and then use the constant.
  216.            [For example: Const Str1 = 'Hello there.'; WriteLn(Str1);]
  217.            You will see this technique in the SWAPTEST example program.
  218.  
  219.         *  Each unit occupies its own segment.  These are linked in the
  220.            reverse order of their appearance on the Uses line, so that
  221.            the last unit on the line will be the earliest in memory.
  222.  
  223.         *  The program's memory block will be loaded in order (from first
  224.            to last in memory):  the main program code, the unit segments
  225.            (where each unit has its own code and data), the main program
  226.            data, the stack, and the heap.
  227.  
  228.         *  In order to keep the main program code as small as possible
  229.            (since this is unavoidable overhead), put as much as you can
  230.            in seperate units.  A trick which I use goes like this:  Put
  231.            the basis of the program in a unit, which is then included
  232.            in a "shell" whose sole purpose is to call the program in its
  233.            unit.  In this way, you can link SWAPUNIT before the large
  234.            unit and have large applications use SwapExec with about 3
  235.            kilobytes of memory overhead.  In order to allow the large
  236.            unit to call SwapExec (which is in the SWAPUNIT), you will
  237.            need to have a symbiotic dependency (so that the large unit
  238.            containing the bulk of the program uses SwapUnit and SwapUnit
  239.            uses the large unit).  In order to do this trick, you will
  240.            need the SWAPUNIT source code, which is given to registered
  241.            users.  See the end of the documentation for details.
  242.  
  243.  
  244.         See the enclosed SWAPTEST example Turbo Pascal program and the
  245.         commented SWAPTEST.MAP file (produced by Turbo Pascal 6.0) for
  246.         an example of SwapExec usage and results.
  247.  
  248.         Please note that the SwapExec "swap to disk" function requires
  249.         the user to have at least DOS 3.0, while everything else needs
  250.         DOS 2.0 (as does anything, really).  If the user doesn't have
  251.         DOS 3.0 or above, swapping to disk will automatically fail.
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.         -----------------------------------------------------------------
  259.                                D I S C L A I M E R
  260.         -----------------------------------------------------------------
  261.  
  262.           Every effort has been made to insure that SWAPUNIT works as
  263.           described, and runs completely error-free.  No guarantee is
  264.           made as to the suitability of SWAPUNIT to any purpose, and I
  265.           cannot be responsible for any damages alledged to have been
  266.           caused by the use of SWAPUNIT.  If you find a problem with
  267.           the usage of SWAPUNIT, please alert the author at the address
  268.           below.  SWAPUNIT will continue to go through testing and
  269.           modification to ensure the best possible product.
  270.  
  271.  
  272.         -----------------------------------------------------------------
  273.                        A B O U T   T H I S   P R O G R A M
  274.         -----------------------------------------------------------------
  275.  
  276.           SWAPUNIT is Copyright 1992 by Kevin A. Kwast, and protected
  277.           by copyright laws under U.S. and International law.  The user
  278.           is granted the right to copy and distribute this package in
  279.           its complete, unaltered form.  Usage of SWAPUNIT requires a
  280.           Shareware registration fee be paid to the author.  In this
  281.           way, you can examine SWAPUNIT and see if it meets your needs
  282.           before you purchase the program.  Take it for a spin; drive
  283.           it around the block a few times.  Try it out for 30 days, but
  284.           please note that you may not use beyond this time (or use it
  285.           in any program) until you register your usage.  When you do,
  286.           you will receive the Turbo Pascal source to SWAPUNIT (but not
  287.           the assembly language source to the SwapExec routine), so you
  288.           can remove the registration notice from the start of the unit
  289.           and create a more specialized unit which will eliminate
  290.           overhead (through each unit using the other, as I mentioned
  291.           above).  Specify disk type when you register, and you will
  292.           receive the source code and additional examples so that you
  293.           can begin producing the best programs possible.
  294.  
  295.           Thank you in advance for your support of Shareware products.
  296.           Months of effort were put into this product, and your support
  297.           shows that you appreciate products like these.  Is $35 worth
  298.           the effort you would put into designing something like this
  299.           yourself?  You can use the form on the next page (which is
  300.           also found in the file REGISTER.ME) -- Thank you again!
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.         -----------------------------------------------------------------
  308.                         R E G I S T R A T I O N   F O R M
  309.         -----------------------------------------------------------------
  310.  
  311.          Print and complete this form to send with check or money order
  312.          in the amount of $35 (US dollars only) payable to KEVIN KWAST.
  313.          Mail to the address below, and thank you for your support; your
  314.          registration makes quality Shareware possible.
  315.  
  316.          Any of this information is optional.
  317.  
  318.  
  319.            Name: ____________________________________________________
  320.  
  321.            Company: _________________________________________________
  322.  
  323.            Address: _________________________________________________
  324.  
  325.            City: ____________________  State: _______  Zip: _________
  326.  
  327.            Area Code: _________  Phone: _____________________________
  328.  
  329.  
  330.  
  331.            Computer Type: ___________________________________________
  332.  
  333.            Turbo Pascal Version: ___________________  Modem? ________
  334.  
  335.            Other Languages You Use: _________________________________
  336.  
  337.            Disk Size Preferred:   [___] 5.25 inch     [____] 3.5 inch
  338.  
  339.  
  340.  
  341.            Where Did You Get SWAPUNIT: ______________________________
  342.  
  343.            What Do You Think: _______________________________________
  344.  
  345.            What Will It Be Used For: ________________________________
  346.  
  347.  
  348.  
  349.  
  350.          You can contact me for registration or support:
  351.  
  352.                       Kevin A. Kwast
  353.                       P. O. Box 1397
  354.                       Coppell, Texas
  355.                               75019
  356.  
  357.