chapt16、线程堆栈

来源:互联网 发布:淘宝首页自定义优惠券 编辑:程序博客网 时间:2024/05/29 04:37
这里的堆栈指的就是栈 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#endifvoid 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.}


原创粉丝点击