[comp.sys.mac.programmer] How do I expand system heap from an INIT?

mrr@mrsoft.Newport.RI.US (Mark Rinfret) (08/10/90)

I've  written a device driver which supports inter-application messages and
symbol  management.   It  works  great  under MultiFinder and it also works
great  under  Finder  when  TMON is running :-).  When run with Finder sans
TMON,  the  system  usually  crashes  on  my  first  attempt  to  start  an
application.

The  problem,  I'm  certain,  is  that it requires more system heap than is
available  from  the  standard  system.   I  don't  want to modify the boot
blocks,  so I need a way for the INIT (which installs the driver) to expand
the system heap.

I  gather (from some snooping through TMON Startup) that the way to do this
is roughly as follows:

    - Copy the INIT resource onto the stack. I believe this is necessary
      since the current application zone will be trashed when the heap
      is expanded (right?).
    - adjust the stack pointer (saving original)
    - jump into relocated code at next instruction (_ReleaseResource)
    - call SetAppBase with (ApplZone + additional size)
    - perform other chores (install driver, etc.)
    - restore stack pointer
    - return

Everything  works  fine  (or appears to) until I restore the stack pointer.
It  appears  to  be off by a longword (no simple explanation - I've done my
"stack arithmetic") and I end up back at the top of the INIT code.  Forcing
the stack pointer to the correct value (tried it once) caused a crash.

I've spent a fair amount of time tracking this problem.  Before I pursue it
further,  I'd  like to know if I'm on the right track with regard to system
heap  expansion.   If anyone has some sample code for "doing it right", I'd
very much appreciate it if you'd send me a copy.  Thanks very much.

Mark

--
#################################################################
# Mark R. Rinfret, MRSoft               Home: 401-846-7639      #
# mrr@mrsoft, galaxia!mrsoft!mrr        Work: 401-849-9930 x301 #
#################################################################

mneerach@a.inf.ethz.ch (Matthias Ulrich Neeracher) (08/13/90)

In article <mrr.3494@mrsoft.Newport.RI.US> mrr@mrsoft.Newport.RI.US (Mark Rinfret) writes:
>The  problem,  I'm  certain,  is  that it requires more system heap than is
>available  from  the  standard  system.   I  don't  want to modify the boot
>blocks,  so I need a way for the INIT (which installs the driver) to expand
>the system heap.
>
>I  gather (from some snooping through TMON Startup) that the way to do this
>is roughly as follows:
>
> [real horrorshow way to do it omitted :-]

The new & improved way to do this is to insert a resource of type 'sysz'
into your INIT file. This resource contains, I think, a longword, containing
the amount by which to grow the system heap. The easiest way to get a correct
'sysz' resource is to steal one. Most bigger INITs have one.

>Mark

Matthias

-----
Matthias Neeracher                                   mneerach@c.inf.ethz.ch
  "I wouldn't recommend sex, drugs or insanity for everyone, 
   but they've always worked for me" -- Hunter S. Thompson

stevec@Apple.COM (Steve Christensen) (08/14/90)

In article <mrr.3494@mrsoft.Newport.RI.US> mrr@mrsoft.Newport.RI.US (Mark Rinfret) writes:
>I've  written a device driver which supports inter-application messages and
>symbol  management.   It  works  great  under MultiFinder and it also works
>great  under  Finder  when  TMON is running :-).  When run with Finder sans
>TMON,  the  system  usually  crashes  on  my  first  attempt  to  start  an
>application.
>
>The  problem,  I'm  certain,  is  that it requires more system heap than is
>available  from  the  standard  system.   I  don't  want to modify the boot
>blocks,  so I need a way for the INIT (which installs the driver) to expand
>the system heap.
>
>I  gather (from some snooping through TMON Startup) that the way to do this
>is roughly as follows:
>
>    - Copy the INIT resource onto the stack. I believe this is necessary
>      since the current application zone will be trashed when the heap
>      is expanded (right?).
>    - adjust the stack pointer (saving original)
>    - jump into relocated code at next instruction (_ReleaseResource)
>    - call SetAppBase with (ApplZone + additional size)
>    - perform other chores (install driver, etc.)
>    - restore stack pointer
>    - return
>
>Everything  works  fine  (or appears to) until I restore the stack pointer.
>It  appears  to  be off by a longword (no simple explanation - I've done my
>"stack arithmetic") and I end up back at the top of the INIT code.  Forcing
>the stack pointer to the correct value (tried it once) caused a crash.
>
>I've spent a fair amount of time tracking this problem.  Before I pursue it
>further,  I'd  like to know if I'm on the right track with regard to system
>heap  expansion.   If anyone has some sample code for "doing it right", I'd
>very much appreciate it if you'd send me a copy.  Thanks very much.

Another way that doesn't require programming gymnastics is to include a 'sysz'
resource ID=0 in your INIT file.  The resource contains a single longint that
tells how much to grow the system heap if there isn't already enough space
available.  INIT 31 will then handle this for you before your INIT starts
executing...

steve

-- 
____________________________________________________________________

  Steve Christensen             Internet:   stevec@goofy.apple.com
  Apple Computer, Inc.          AppleLink:  STEVEC
  20525 Mariani Ave, MS 81-CS   CompuServe: 76174,1712
  Cupertino, CA  95014

  "You just contradicted me."  "No I didn't."
____________________________________________________________________

mrr@mrsoft.Newport.RI.US (Mark Rinfret) (08/15/90)

>In article <1717@neptune.inf.ethz.ch> mneerach@a.inf.ethz.ch (Matthias Ulrich Neeracher) writes:
>
>The new & improved way to do this is to insert a resource of type 'sysz'
>into your INIT file. This resource contains, I think, a longword, containing
>the amount by which to grow the system heap. The easiest way to get a correct
>'sysz' resource is to steal one. Most bigger INITs have one.
>

I did, in fact, attempt this prior to using the "horror show" approach. As
I understand it, "sysz" consists of a single longword containing the amount
of additional heap required. I created one (after examining another) and
it didn't have any lasting effect. Do you have explicit documentation for 
the correct use of sysz?

Mark

--
#################################################################
# Mark R. Rinfret, MRSoft               Home: 401-846-7639      #
# mrr@mrsoft, galaxia!mrsoft!mrr        Work: 401-849-9930 x301 #
#################################################################