[comp.sys.mac.programmer] Terminate and Stay Resident?

gja@etive.ed.ac.uk (A Cunningham) (06/15/89)

	I'm leaving my current job to programme Macs for a living (Yay!). One
of my first jobs will be porting a programme from IBM (spit!) PCs (blechh!)
to the Mac. However there is a fly in the ointment. Without giving too much
away about what I'll be doing I'll try and describe what's going on. In the
PC (ugh!) version of the program a terminate-and-stay-resident block of code
is loaded and then the main application runs and uses services provided by
the first program by executing software interupts.
	Is it possible to do something similar on a Mac without too much 
trouble? One solution might be to use the interprocess communications
facilities provided by System 7 but the software is intended for academic
use and there are a lot of people with one meg macs who will not be happy
with this as a solution. Can a CDEV provide a similar service in a one
meg machine? (I'm really fishing here!) Any help as usual is gratefully
received.

	Tony Cunningham

"I think of Sarah. The rest is easy."

stuart@tartarus.uchicago.edu (Stuart A. Kurtz) (06/16/89)

gjr@etive.ed.ac.uk asks whether or not it makes sense to use an init
to aid in porting an MS-DOS program that relies on tsr code.

I can only see three reasons for the terminate-and-stay-resident kludge:

1) to evade the limit on process size under MS-DOS.  Your program is either
too big or requires too much data to fit.

2) for some reason, the original program wants to keep data around across
runs within a single boot session.  E.g., it might want to know how many
times its executed since the last reboot.

3) the tsr code is patching around something in MS-DOS, and the interrupts
are replacing BIOS interrupts.

All these have easy solutions:

1) the MacOS doesn't have a disgusting limit on process size (well, there
is at present a limit of 8M, but let's hope that's not a problem).  If this
is the problem, just combine the tsr code with the main program and call it
directly.

2) There are two solutions to this problem.  The lazy solution is to put an
unpurgeable resource in the system heap.  This will give you trouble under
a lot of circumstances.  The robust alternative is to store the information
in the resource fork of the file, along with the time of last reboot.  When
you start up, load the resource and check the embedded time: if it's old,
revert to defaults.

3) If it's doing this, there is real kludgery present.  Either the PC
application is (a) fixing something that is broke in a way that won't break
other programs; (b) checks to sees who calls it, and then calls the default
trap if it's not your application [this is almost too gross to imagine], or
(c) breaks other programs.  You might find that whatever is broken in
MS-DOS isn't broken on the Mac; and even if it is broken, it is probably
broken in a different way.  In any event, there is a well-defined, safe,
and supported mechanism for patching Mac traps.

I *really dislike* the idea of an init-dependent application.  I almost
always run off a hard disk.  The init-dependent solution requires that I
either:

a) always pay the price in memory for the resident code, even when I'm not
going to run your application.  (I have a 5 meg machine, but I also run
Mathematica.  Space is too dear to give it up for no good cause.) Or,

b) I have to reboot off a special disk whenever I want to run your program.

Believe me, I wouldn't accept either solution.

Don't do it.  Trust me.

Stu

-------------------------------------------------------------------------
Stuart A. Kurtz                         Telephone: (312)-702-2493
Department of Computer Science          EMail:     stuart@cs.uchicago.edu
University of Chicago
1100 E. 58th Street
Chicago, Illinois  60637


-------------------------------------------------------------------------
Stuart A. Kurtz                         Telephone: (312)-702-2493
Department of Computer Science          EMail:     stuart@cs.uchicago.edu
University of Chicago

mjm@eleazar.dartmouth.edu (Michael McClemen) (06/21/89)

> PC (ugh!) version of the program a terminate-and-stay-resident block of code
> is loaded and then the main application runs and uses services provided by
> the first program by executing software interupts.
>        Is it possible to do something similar on a Mac without too much

You may want to try writing the "terminate-and-stay-resident" code as a driver.
A Macintosh driver is a resource of type 'DRVR' containing executable code and
a specialized header that enables it to receive various types of control calls
and events.  Your application can open the driver (it gets called to
initialize itself) and then make various control calls to it.  A driver can
also be set up to own windows and receive events associated with them, or (I
think) to receive network and disk interrupts.  Just to make things
confusing, a desk accessory is a sort of driver.  So is the disk interface code,
the serial port interface code, the Appletalk interface code...  The details,
as usual, are in Inside Macintosh and the tech notes.

Michael McClennen