[comp.sys.mac.programmer] LSC & Calling CODE segments as functions

leeke@glacier.STANFORD.EDU (Steven D. Leeke) (09/24/88)

Let's suppose I want to load a CODE segment from some other file and
call it as a C function - making sure it is declared as a C main
and pass it the correct parameters - one of which is a table of
entry points into the program that is loading it - the "main" program.
The entrypoints are setup as an array of Ptr's which are created dynamically
just before the call. e.g.


	entryPoints	=	NewPtr(sizeof(ProcPtr)*NoOfEntries);
	
	if (entryPoints != NULL) {
		entryPoints[0]	=	<function name>;
		...
	};


LSC reqires me to use their RememberA4/SetupA4 stuff on entry in the
CODE segment and RestoreA4 on exit to get at the globals (if any) in
the CODE segment - so I do. e.g. the CODE segment's main looks like:


void main(p1,p2,p3,...,entryPoints)
{
	RememberA4();
	SetupA4();

	/*
		Intervening code...including jumps back into the
		"main" program - see below.
	*/

	RestoreA4();
};


What I want to know is if A5 is getting bashed when I call the CODE
segment?  I am able to load the segment, lock it, dereference it and
call it successfully e.g.:


	code	=	GetResource(zzzz,yyy);
	
	if (code != NULL) {
		HLock(code);

		(**code)(p1,p2,p3,...,entrypoints);

		HUnlock(code);
	};


Then, I use the array of entrypoints to call various fctns (C) in the
"main" program, e.g.:


	(*entryPoints[0])();


This seems to work well until I try to draw something from within the
"main" program (I don't draw in the code segment) - usually activating
or updating a window when a dialog the CODE segment loaded is being drawn.

So what is it?  Is A5 getting bashed in the CODE segment?  I am using a
project with only the MacTraps library for the CODE segment.  This makes me
think that as soon as I call the code segment the QuickDraw globals are
getting bashed since MacTraps defines them.  e.g. A5 is corrupted.  This
is my prime culprit since crashing only occurs when something is drawn.

I've tried to use SetupA5 right after SetupA4 when I start my CODE segment,
for what it's worth, but it doesn't seem to make a difference.  I'm not
an experienced ASM hacker period - especially on the Mac.

Any help would be appreciated,

Steve Leeke