[comp.lang.c] function pointers and Mac segments

peter@neccan.oz (Peter Alexander Miller) (05/04/90)

in article <3545@dogie.macc.wisc.edu>, yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) says:
> 
> I'm a little confused about this. I have a Macintosh program
> I'm doing in Think C. In one part I make heavy use of function
> pointers where I init a heap based array with the address's of
> a bunch of different functions. 
> 
> The init code, all of the functions themselves and the routine
> that actually does the referencing (jumping) to the functions
> are all in one segment, but not the main one. 
> 
> Do I ever have to worry about the Mac memory manager purging and
> then reloading the segment thereby possibly causing the address's
> to become invalid? If the init code or the functions or the 
> jumping routine were in different segment would I ever have to
> worry?
The Mac uses a jump table for functions.
All function calls go through this table.
This table is relative to A5, and is never purged.
The table contains either (a) a trap to load in the code segment
or (b) a jump into the code segment (if it is loaded).
Your array of pointers to functions is being filled
with addresses of entries in the jump table.
If you call through one of these function pointers it will
load the segment into memory if necessary first.

In short: no, you don't have to worry about the addresses becoming invalid
or the function not being in memory.

BTW I use Think C 4.0 myself.

Hope this helps.

Regards
Peter Miller       UUCP    {uunet,mcvax,ukc}!munnari!neccan.oz!pmiller
                   ARPA    pmiller%neccan.oz@uunet.uu.net
/\/\*              CSNET   pmiller%neccan.oz@australia
                   ACSnet  pmiller@neccan.oz
Disclaimer?  These guys have no idea what I am on about!
D

eifrig@crabcake.cs.jhu.edu (Jonathan Eifrig) (05/05/90)

In <707@neccan.oz>, peter@neccan.oz (Peter Alexander Miller) writes:
-> in article <3545@dogie.macc.wisc.edu>, yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) says:
-> > 
-> > I'm a little confused about this. I have a Macintosh program
-> > I'm doing in Think C. In one part I make heavy use of function
-> > pointers where I init a heap based array with the address's of
-> > a bunch of different functions. 
-> > 
-> > The init code, all of the functions themselves and the routine
-> > that actually does the referencing (jumping) to the functions
-> > are all in one segment, but not the main one. 
-> > 
-> > Do I ever have to worry about the Mac memory manager purging and
-> > then reloading the segment thereby possibly causing the address's
-> > to become invalid? If the init code or the functions or the 
-> > jumping routine were in different segment would I ever have to
-> > worry?
-> The Mac uses a jump table for functions.
-> All function calls go through this table.
-> This table is relative to A5, and is never purged.
-> The table contains either (a) a trap to load in the code segment
-> or (b) a jump into the code segment (if it is loaded).
-> Your array of pointers to functions is being filled
-> with addresses of entries in the jump table.
-> If you call through one of these function pointers it will
-> load the segment into memory if necessary first.
-> 
-> In short: no, you don't have to worry about the addresses becoming invalid
-> or the function not being in memory.

	I'm not so sure about this.  Use of the Segment Loader and code seg-
ment 0 is only _required_ when jumping/calling functions outside the current
segment.  I've seen code that only used the segment table for "non-local"
function calls, and did intra-segment calling the normal UNIX way.  Blindly
assuming that your function pointers are actually pointers into CODE 0 is
probably a bad idea.  Glancing through my LSC 4.0 manual doesn't tell me
that they are definitely using CODE 0, so I would either Nosy some code or
call Tech. Support.  (Sigh!  I still have a delusion that those _helpful_
people at Think Technologies produce LSC.  Oh well ....)

-> 
-> BTW I use Think C 4.0 myself.
-> 

	No no NO!!!  It is, and always will be, Lightspeed C, regardless of
what Symantec says!  :-}

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

	"One of the chief services which mathematics has rendered the human
race in the past century is to put "common sense" where it belongs, on the
topmost shelf next to the dusty canister labeled "discarded nonsense.""
	-Eric Temple Bell, "Mathematics - Queen of the Sciences"

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=