chapt16、线程堆栈

这里的堆栈指的就是栈 Stack

线程堆栈默认大小是保留1M,初始提交2个页面,如8KB,1读写,1具有PAGE_GUARD保护属性

堆栈扩展的时候,会因为保护属性触发异常,线程根据这个自动增长

2000的堆栈
在Win2000里,最后一个页面不会被用到,会被最终标记reserve,当提交到倒数第二个页面的时候,会触发EXCEPTION_STACK_ OVERFLOW,虽然这还不是最后一个page,但是这个时候就应该妥善处理这个问题了,否则容易发生严重的问题

98的堆栈--A Thread's Stack Under Windows 98
在Win98里,线程堆栈的前后是额外个增加了64KB的,即1MB+128KB,98没有PAGE_GUARD保护属性,使用PAGE_NOACCESS属性模拟
98的堆栈比较特殊,有一段为兼容16位程序而模拟特殊的16KB读写段。最高的16KB地址用来做underflow保护,最低的16KB用来做overflow检测,它可用用全1MB的堆栈空间,而2000的最低一个页面则不会被使用

线程堆栈检测函数--The C/C++ Run-Time Library's Stack-Checking Function
在必要的时候,编译器会加入合适的代码来时堆栈进行合适的扩展
如如下代码,
void SomeFunction () {
   int nValues[4000];

   // Do some processing with the array.
   nValues[0] = 0;  // Some assignment
}
会被类似的扩展为
// The C run-time library knows the page size for the target system.
#ifdef _M_ALPHA
#define PAGESIZE   (8 * 1024)   // 8-KB page
#else
#define PAGESIZE   (4 * 1024)   // 4-KB page
#endif

void StackCheck(int nBytesNeededFromStack) {
   // Get the stack pointer position.
   // At this point, the stack pointer has NOT been decremented
   // to account for the function's local variables.
   PBYTE pbStackPtr = (CPU's stack pointer);

   while (nBytesNeededFromStack >= PAGESIZE) {
      // Move down a page on the stack--should be a guard page.
      pbStackPtr -= PAGESIZE;

      // Access a byte on the guard page--forces new page to be
      // committed and guard page to move down a page.
      pbStackPtr[0] = 0;

      // Reduce the number of bytes needed from the stack.
      nBytesNeededFromStack -= PAGESIZE;
   }

   // Before returning, the StackCheck function sets the CPU's
   // stack pointer to the address below the function's
   // local variables.
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值