我已经看到至少三种不同的方法来设置sigaltstack()的替代堆栈。我想知道哪个是最好的方法:
方法#1
stack_t sigstk;
sigstk.ss_size = 0;
sigstk.ss_flags = 0;
sigstk.ss_sp = mmap (NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (sigstk.ss_sp != MAP_FAILED) {
sigstk.ss_size = SIGSTKSZ;
if (sigaltstack (&sigstk, 0) < 0) {
sigstk.ss_size = 0;
printf ("sigaltstack errno=%d\n", errno);
}
} else {
printf ("malloc (SIGSTKSZ) failed!\n");
}
方法#2(我们已经使用了一段时间,但是这里分配的内存显示在泄漏检测(leaks
命令)中)
stack_t sigstk;
sigstk.ss_size = 0;
sigstk.ss_flags = 0;
sigstk.ss_sp = malloc (SIGSTKSZ);
if (sigstk.ss_sp != NULL) {
sigstk.ss_size = SIGSTKSZ;
if (sigaltstack (&sigstk, 0) < 0) {
sigstk.ss_size = 0;
free (sigstk.ss_sp);
printf ("sigaltstack errno=%d\n", errno);
}
} else {
printf ("malloc (SIGSTKSZ) failed!\n");
}
方法#3
stack_t sigstk;
static char ssp[SIGSTKSZ];
sigstk.ss_size = SIGSTKSZ;
sigstk.ss_flags = 0;
sigstk.ss_sp = ssp;
sigstk.ss_size = SIGSTKSZ;
if (sigaltstack (&sigstk, 0) < 0) {
sigstk.ss_size = 0;
free (sigstk.ss_sp);
printf ("sigaltstack errno=%d\n", errno);
}
谢谢,Ákos (Mac OS X 10.8.2)