如何在MacOS X上找到堆栈的基地址和大小? [英] How do I find the base address and size of the stack on MacOS X?

查看:187
本文介绍了如何在MacOS X上找到堆栈的基地址和大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将一个不精确的垃圾收集器从Windows移植到MacOSX.在其中,它必须扫描堆栈以识别潜在的指针进入堆,然后将其用作GC根.为此,我需要堆栈的基数和长度.在Windows中,此代码使用类似于此处描述的算法:

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:

堆栈和堆栈基地址

如何在Mac OS X上执行此操作?请注意,目前,我只关心主线程.使用此GC的解释器是单线程的,我可以保证在其他线程上不存在引用.

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

推荐答案

您还可以使用特定于Darwin的函数来获取堆栈的总大小和长度:

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

这篇关于如何在MacOS X上找到堆栈的基地址和大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆