//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*1028) //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 funtion'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 gurd 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.
}
当编译程序时,编译器知道你针对的CPU系统的页面大小。X86编译器知道页面大小是4KB,Alpha编译器知道页面大小是8KB。当编译器遇到程序中的第个函数时,它能确定该函数需要的椎栈空间的数量。如果该函数需要的堆栈空间大于目标系统的页面大小,编译器将自动插入对堆栈检查函数的调用。
上面这个伪代码显示了堆栈检查函数执行什么操作。之所以称它是伪代码,是因为这个函数通常是由编译器供应商用汇编语言来实现的。
每当创建一个线程时,系统就会为线程的堆栈(每个线程都有它自己的堆栈)保留一个堆栈空间区域。并将一些物理存储器提交给这个已保留的区域。按照默认设置,系统保留1MB的地址空间并提交两个页面的内存。但是,这些默认值是可以修改的,方法是在链接应用程序时设定Microsoft的链接程序的/STACK选项:/STACK:reservet[.commit]。
上面的代码假定的是使用默认的堆栈大小值,即1MB的保留区域,每次提交一个页面的内存。