c/c++运行期库的堆栈检查函数

//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的保留区域,每次提交一个页面的内存。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值