开发者

How do I find the base address and size of the stack on MacOS X?

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开发者_如何学C 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:

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./


You could also get the stack's total size and length with the Darwin-specific functions:

    pthread_t self = pthread_self();
    void* addr = pthread_get_stackaddr_np(self);
    size_t size = pthread_get_stacksize_np(self);
    printf("addr=%p size=%zx\n", addr, size);


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜