views:

37

answers:

1

I'm porting an imprecise garbage collector from Windows to MacOS X. In it, it has to scan the stack to identify potential pointers into the heap, and then use those as GC roots. To do this, I need the stack's base as well as it's length. In Windows, this code uses an algorithm similar to what's described here:

http://stackoverflow.com/questions/3171475/stack-and-stack-base-address

How do I do that on Mac OS X? Note that, for now, I only care about the main thread. The interpreter that uses this GC is single threaded and I can guarantee that no references exist on other threads./

A: 

Hans Boehm's conservative GC for C runs on MacOS X, and is open-source. So you could conceivably have a look at the source code of that GC to see how it locates the stack.

Alternatively, depending on how much you control the calling code, you may simply take the address of a local variable somewhere "high" (e.g. in the main() function or its MacOS X equivalent, or in the starting function for the relevant thread). Possibly, you might be able to simply choose the stack address and size upon thread creation (with Posix threads, this is done with pthread_attr_setstack() -- Posix threads can be used with MacOS X).

Thomas Pornin