[comp.sys.mac.programmer] Passing pointers to functions

dan@lclark.UUCP (Dan Revel) (12/22/89)

I have been following the HLock thread and would like to get
some clarification.  Is it safe to pass pointers to functions
across segment boundaries?  (I think this question might be
rephrased: 'are my code segments going to be relocated while
my program is running?', is that right?)

I have tried doing this running the heapscramble (hs) mode of
Macsbugs with no obvious ill-effects, but still, I wonder...

Thanks,
	Dan

-- 
dan@lclark
tektronix!reed!lclark!dan			Dylsexics untie! (-|

tim@hoptoad.uucp (Tim Maroney) (12/24/89)

In article <859@lclark.UUCP> dan@lclark.UUCP (Dan Revel) writes:
>I have been following the HLock thread and would like to get
>some clarification.  Is it safe to pass pointers to functions
>across segment boundaries?  (I think this question might be
>rephrased: 'are my code segments going to be relocated while
>my program is running?', is that right?)
>
>I have tried doing this running the heapscramble (hs) mode of
>Macsbugs with no obvious ill-effects, but still, I wonder...

Your code segments may be relocated if you do UnloadSeg on them,
as many people recommend.  However, MPW at least will generate
function pointers into the jump table rather than to the function
itself in the case of cross-segment references.  Within a single
segment, it will produce pointers to the functions.  I would
hope that THINK C does the same.

This means that if you take a function pointer while you're in a
different segment from the function, what you actually get is the
jump table entry for the functions, so even if the segment is
relocated, your pointer will still be valid.
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com

"I've been called an evil genius by cities of assholes...  but I know who
 these people are!  And they're on my list!" -- Robert Crumb

zben@umd5.umd.edu (Ben Cranston) (12/29/89)

In article <859@lclark.UUCP> dan@lclark.UUCP (Dan Revel) writes:

> Is it safe to pass pointers to functions
> across segment boundaries?  (I think this question might be
> rephrased: 'are my code segments going to be relocated while
> my program is running?', is that right?)

In article <9389@hoptoad.uucp> tim@hoptoad.UUCP (Tim Maroney) replies:

> Your code segments may be relocated if you do UnloadSeg on them,
> as many people recommend.  However, MPW at least will generate
> function pointers into the jump table rather than to the function
> itself in the case of cross-segment references.  Within a single
> segment, it will produce pointers to the functions.  I would
> hope that THINK C does the same.
> This means that if you take a function pointer while you're in a
> different segment from the function, what you actually get is the
> jump table entry for the functions, so even if the segment is
> relocated, your pointer will still be valid.

The other case is also worth considering.  Suppose you take a pointer to
a local function (so the pointer generated is to the real function, not to
a jump table entry).  You then pass this pointer to a function in another
segment.  It then uses the pointer, and does not get the benefit of going
through a jump table entry.

In order for this to fail, though, the original segment must have been
unloaded.  While this could happen in the case of an initialization segment
that will never be returned to, in the usual case of heirarchical segment
organization the upper level segment cannot be unloaded for fear of
invalidating return addresses and other pointers that might be saved in the
stack frame for the calling routine in that segment.

-- 
Sig     DS.L    ('ZBen')       ; Ben Cranston <zben@Trantor.UMD.EDU>
* Network Infrastructures Group, Computer Science Center
* University of Maryland at College Park
* of Ulm

siegel@endor.harvard.edu (Rich Siegel) (12/29/89)

In article <5832@umd5.umd.edu> zben@umd5.umd.edu (Ben Cranston) writes:
>In article <859@lclark.UUCP> dan@lclark.UUCP (Dan Revel) writes:
>
>> Is it safe to pass pointers to functions
>> across segment boundaries?  (I think this question might be
>
>The other case is also worth considering.  Suppose you take a pointer to
>a local function (so the pointer generated is to the real function, not to
>a jump table entry).  You then pass this pointer to a function in another

	I don't know about other development systems, but if you take the
address of a function in THINK C or THINK Pascal, a jump table entry
gets generated for that function if one does not already exist. This is
to avoid exactly this sort of thing.

	Of course, you can take the absolute address of a local function
in assembly language, but that's your funeral...

R.




~~~~~~~~~~~~~~~
 Rich Siegel
 Staff Software Developer
 Symantec Corporation, Language Products Group
 Internet: siegel@endor.harvard.edu
 UUCP: ..harvard!endor!siegel

"When someone who makes four hundred and fifty dollars an hour wants to
tell you something for free, it's a good idea to listen."

~~~~~~~~~~~~~~~