[comp.lang.pascal] SHRINK BEFORE RUNNING A CHILD PROCESS

zhou@brazil.psych.purdue.edu (Albert Q.Zhou) (06/11/91)

In Turbo Pascal, the dynamic memory to be used in a program has to be
specified during the compilation by $M. However, when you run a child
program with exec, you can not use any part of the memory already 
allocated for the heap even if it is free. Is it a way to get around this?
 

callawaycj@EA.USL.EDU (C.JamesCallaway) (06/11/91)

In article <11940@j.cc.purdue.edu>, zhou@brazil.psych.purdue.edu (Albert Q.Zhou) writes:
>In Turbo Pascal, the dynamic memory to be used in a program has to be
>specified during the compilation by $M. However, when you run a child
>program with exec, you can not use any part of the memory already 
>allocated for the heap even if it is free. Is it a way to get around this?
> 


Maybe This Will Help!

_TURBO_PASCAL_INTERNALS_  By Tischer Pub. ABACUS

Contains Source For A Unit That Will Free Up Almost All Memory Used By Your 
Program. When The Child Process Is Executed Your Program Is Saved To Disk
(Or Memory If Using VDISK, RAMDISK). After Termination Of The Child A Small
Block Of Code Added To The Child Will Install The Saved Program And Execution
Picks Up Just Like Normal. Sounds Pretty Useful, I'll Never Have A Need For It!

Also In The Book Are Units For Mouse Support, Managing Windows, Writing TSR's,
Understanding Interrupts, Multitasking In TP, Using EMS And Extended Memory.

--

###############################################################################
#  C.JamesCallaway!  # CALLAWAYCJ@EA.USL.EDU |     FUTURE iMAGE SoftWorks!    #
# USL P.O. Box 41353 #--------------------------------------------------------#
#Lafayette, La 70504 # A Man Can Only Find Peace Within Himself...If He Looks!#
###############################################################################

berto@and.nl (Bert van Oortmarssen) (06/14/91)

In article <11940@j.cc.purdue.edu> zhou@brazil.psych.purdue.edu (Albert Q.Zhou) writes:
>In Turbo Pascal, the dynamic memory to be used in a program has to be
>specified during the compilation by $M. However, when you run a child
>program with exec, you can not use any part of the memory already 
>allocated for the heap even if it is free. Is it a way to get around this?
> 

Try this ....



     Bert van Oortmarssen
     
unit ushell;

(*****************************************************************************
 ***                 EXEC                                                  ***
 ***  Now you can allways use the {$M xxxxx,xxxxx,655360 } compile options ***
 ***  works with TP4.0 & 5.0, other versions not tested.                   ***
 ***  see chapter "Inside Turbo Details" of your manual                    ***
 *****************************************************************************)

interface

uses crt,dos;

    PROCEDURE SHELL (path, CmdLine : string; minimumavail : longint);

implementation

    PROCEDURE SHELL (path, CmdLine : string; minimumavail : longint);
    var   regs : registers;
          FreelistSize, PargrInUse : word;
          SaveFreelist : pointer;
    begin
       if (maxavail<minimumavail) then EXIT;
            
      if Ofs(FreePtr^)=0 then         { save the free-list !!! }
         FreelistSize := 0    { Ofs(freeptr^)=0 means 65536 < Freelist is empty> }
      else
         begin             { be careful with word-arithmetic ! }
         FreelistSize := $FFFF- (Ofs(FreePtr^)-1);
                     { save the Freelist, allocate room for it }
         GetMem(SaveFreelist, FreelistSize);
                                      { Copy the Freelist ...  }
         Move (FreePtr^, SaveFreelist^, FreelistSize);
         end;

                           { how many paragraphs are in use ?? }
      PargrInUse := Seg(HeapPtr^) - PreFixSeg;
      regs.AH := $4A;            { see any good DOS-book  ...  }
      regs.ES := PreFixSeg;
      regs.BX := PargrInUse+2;   { one pargr. spare            }
      MsDos(regs);                     { shrink !!!!!!!!!!!!!! }
      Exec(path, CmdLine);    { do what you want to do ...     }
            { add code that checks doserror variable           }

      regs.AH := $4A;                         { reclaim memory }
      regs.ES := PreFixSeg;
      regs.BX := Seg(FreePtr^) + $1000 - PreFixSeg;
         { $1000 because of the word arithmetic, $1000 pargr.  }
         { is 65536 bytes                                      }
      MsDos(regs);                     { expand !!!!!!!!!!!!!! }
             { Restore the Freelist and destroy the copy of it }
      if FreelistSize>0 then
         begin
         Move (SaveFreeList^, FreePtr^, FreelistSize);
         FreeMem(SaveFreelist, FreelistSize);
         end;
   end;

BEGIN
END.

bns@ersys.edmonton.ab.ca (Chris Dollmont) (06/17/91)

callawaycj@EA.USL.EDU (C.JamesCallaway) writes:

> In article <11940@j.cc.purdue.edu>, zhou@brazil.psych.purdue.edu (Albert Q.Zh
> >In Turbo Pascal, the dynamic memory to be used in a program has to be
> >specified during the compilation by $M. However, when you run a child
> >program with exec, you can not use any part of the memory already 
> >allocated for the heap even if it is free. Is it a way to get around this?
> > 
> 
> 
> Maybe This Will Help!
> 
> _TURBO_PASCAL_INTERNALS_  By Tischer Pub. ABACUS
> 
> Contains Source For A Unit That Will Free Up Almost All Memory Used By Your 
> Program. When The Child Process Is Executed Your Program Is Saved To Disk
> (Or Memory If Using VDISK, RAMDISK). After Termination Of The Child A Small
> Block Of Code Added To The Child Will Install The Saved Program And Execution
> Picks Up Just Like Normal. Sounds Pretty Useful, I'll Never Have A Need For I
> 
> Also In The Book Are Units For Mouse Support, Managing Windows, Writing TSR's
> Understanding Interrupts, Multitasking In TP, Using EMS And Extended Memory.

Additionally, there is a unit by Kim Kokkenen (TurboPower) that does the 
same thing in a slightly more elegant fashion.  The unit is available on 
Compuserve (and most local BBS's) and is usually bundled with a variety 
of tools for memory management.  If only I could remember what it was 
called!
 
Chris

|  Chris Dollmont                   | USENET:  bns@ersys.edmonton.ab.ca
|  Blue North Software              |  Mail:    16936D 85th Ave.
|                                   |           Edmonton, AB T5R 4A3
|  These opinions remain valid only as long as they are visible...

alcocer@parc.xerox.com (Dario Alcocer) (06/18/91)

bns@ersys.edmonton.ab.ca (Chris Dollmont) writes:

>callawaycj@EA.USL.EDU (C.JamesCallaway) writes:
>Additionally, there is a unit by Kim Kokkenen (TurboPower) that does the 
>same thing in a slightly more elegant fashion.  The unit is available on 
>Compuserve (and most local BBS's) and is usually bundled with a variety 
>of tools for memory management.  If only I could remember what it was 
>called!


I believe it's called ExecSwap.  It was featured in an issue of Dr. Dobbs Journal
two (i think) years ago.  The version I saw included MASM source code.  I adapted
a version for MSC v5.1 for a project I was working on at the time.

--------------------
Dario Alcocer
alcocer.PARC@xerox.com

derek@sun4dts.dts.ine.philips.nl (derek) (06/18/91)

bns@ersys.edmonton.ab.ca (Chris Dollmont) writes:

[input from callawaycj@EA.USL.EDU (C.JamesCallaway) deleted]

>Additionally, there is a unit by Kim Kokkenen (TurboPower) that does the 
>same thing in a slightly more elegant fashion.  The unit is available on 
>Compuserve (and most local BBS's) and is usually bundled with a variety 
>of tools for memory management.  If only I could remember what it was 
>called!

You will find it in Simtel20 in an excellent collection of routines:

BONUS507.ARC  B  150435  900205  Misc. TP 4/5 utilities/pgms from TurboPower

(obviously in the pascal PD1:<MSDOS.TURBOPAS> directory)

Looking at what I just wrote, I see we are talking about an old version -
perhaps I ought to upload the latest disk to Simtel (the one that came 
with Turbo Objects) - I'll check the small print.

Best Regards, Derek Carr
DEREK@DTS.INE.PHILIPS.NL           Philips IE TQV-5 Eindhoven, The Netherlands 
Standard Disclaimers apply.

CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) (06/19/91)

In article <913@baby.and.nl> berto@and.nl (Bert van Oortmarssen)
  wrote:

>In article <11940@j.cc.purdue.edu> zhou@brazil.psych.purdue.edu
> (Albert Q.Zhou) writes:
>>In Turbo Pascal, the dynamic memory to be used in a program has to be
>>specified during the compilation by $M. However, when you run a child
>>program with exec, you can not use any part of the memory already
>>allocated for the heap even if it is free. Is it a way to get around this?

[...deleted...]

       if (maxavail<minimumavail) then EXIT;

[...deleted...]

         GetMem(SaveFreelist, FreelistSize);

[...deleted...]

                           { how many paragraphs are in use ?? }
      PargrInUse := Seg(HeapPtr^) - PreFixSeg;

[...deleted to end...]

Perhaps I'm wrong, but it seems to me that the test MaxAvail <
MinimumAvail (which perhaps we should call MinimumRequired) does not
guarantee that the shell will be provided the specified memory,
since there is no guarantee that the block referenced by MaxAvail
lies at the top of the heap. A better scheme might be to
GetMem(SaveFreeList,...) _first_, then compare MinimumAvail to sum
of the size of the block between HeapPtr^ and FreePtr^ plus the size
of the free list.

Cheers--                        --Karl

+====================================================================+
| Karl Brendel                           Centers for Disease Control |
| Internet: CDCKAB@EMUVM1.BITNET         Epidemiology Program Office |
| Bitnet: CDCKAB@EMUVM1                  Atlanta GA  30333       USA |
|                        Home of Epi Info 5.0                        |
+====================================================================+

speedy@vax.oxford.ac.uk (06/19/91)

In article <11940@j.cc.purdue.edu>, zhou@brazil.psych.purdue.edu (Albert Q.Zhou) writes:
> In Turbo Pascal, the dynamic memory to be used in a program has to be
> specified during the compilation by $M. However, when you run a child
> program with exec, you can not use any part of the memory already 
> allocated for the heap even if it is free. Is it a way to get around this?
>  

There are a few routinesabout from a wide variety of sources that can swap
out the whole heap to Disk or EMS along with much of the program-they are
available for both C and Pascal. I have one such routine which a) I have never
used and b) is quite old but it does have all the source code with it. If you 
are interested email me and I will send an XX-encoded version (Janet mucks up
UUencoded files according to BITFTP)

bns@ersys.edmonton.ab.ca (Chris Dollmont) (06/19/91)

alcocer@parc.xerox.com (Dario Alcocer) writes:
> I believe it's called ExecSwap.  It was featured in an issue of Dr. Dobbs Jou
> two (i think) years ago.  The version I saw included MASM source code.  I ada
> a version for MSC v5.1 for a project I was working on at the time.
> 
> --------------------
> Dario Alcocer
> alcocer.PARC@xerox.com

ExecSwap it is...thanks for aiding a failing memory.

Chris

|  Chris Dollmont                   | USENET:  bns@ersys.edmonton.ab.ca
|  Blue North Software              |  Mail:    16936D 85th Ave.
|                                   |           Edmonton, AB T5R 4A3
|  These opinions remain valid only as long as they are visible...