分享
 
 
 

[翻译]Reserving and Committing Memory(预约和调拨内存)

王朝other·作者佚名  2006-05-27
窄屏简体版  字體: |||超大  

Reserving and Committing Memory

The following example illustrates the use of the VirtualAlloc and VirtualFree functions in reserving and committing memory as needed for a dynamic array. First, VirtualAlloc is called to reserve a block of pages with NULL specified as the base address parameter, forcing the system to determine the location of the block. Later, VirtualAlloc is called whenever it is necessary to commit a page from this reserved region, and the base address of the next page to be committed is specified.

下面的例子阐明了在需要一个动态数组时,使用VirtualAlloc和 VirtualFree函数预约和调拨内存。首先,调用VirtualAlloc预约一块页,指定基址参数为NULL,由系统确定块的位置。然后,VirtualAlloc在任何必要的时候从这个预约的区域内调拨一个页面,同时下一个要被调拨的页面的基址也被指定了。

The example uses structured exception-handling syntax to commit pages from the reserved region. Whenever a page fault exception occurs during the execution of the __try block, the filter function in the expression preceding the __except block is executed. If the filter function can allocate another page, execution continues in the __try block at the point where the exception occurred. Otherwise, the exception handler in the __except block is executed. For more information, see Structured Exception Handling.

本例用structured exception-handling syntax 从预约区域调配页面。只要执行__try块过程中发生一个页面错误例外,表达式中先于__except 块的过滤函数会被执行。如果这个过滤函数能够重新分配一个页面,将从__try块中发生异常的地方继续执行。否则__except 块中的例外处理被执行。更多信息,见Structured Exception Handling。

As an alternative to dynamic allocation, the process can simply commit the entire region instead of only reserving it. However, committing the region consumes physical storage that might not be needed, making it unavailable for use by other processes.

对于一个动态分配事件,进程可以简单的直接把整个区域调配而不是预约。然而,调配这个区域不必要地消耗了物理容量,使其很难再被其他进程使用。

The example uses VirtualFree to free the reserved and committed pages when it is finished with them. The function uses MEM_RELEASE to decommit and release the entire region of reserved and committed pages.

本例使用VirtualFree 来释放被预约和调配结束的页面。这个函数使用MEM_RELEASE 取消调配和释放整个被预约和调配的页面区域。

// A short program to demonstrate dynamic memory allocation using

// a structured exception handler.

#include <windows.h>

#include <stdio.h> // for printf

#include <stdlib.h> // for exit

#define PAGELIMIT 80 // ask for this many pages

LPTSTR lpNxtPage; // address of the next page to ask for

DWORD dwPages = 0; // count of pages gotten so far

DWORD dwPageSize; // the page size on this computer

INT PageFaultExceptionFilter(DWORD dwCode)

{

LPVOID lpvResult;

// If the exception is not a page fault, exit.

if (dwCode != EXCEPTION_ACCESS_VIOLATION)

{

printf("Exception code = %d\n", dwCode);

return EXCEPTION_EXECUTE_HANDLER;

}

printf("Exception is a page fault\n");

// If the reserved pages are used up, exit.

if (dwPages >= PAGELIMIT)

{

printf("Exception: out of pages\n");

return EXCEPTION_EXECUTE_HANDLER;

}

// Otherwise, commit another page.

lpvResult = VirtualAlloc(

(LPVOID) lpNxtPage, // next page to commit

dwPageSize, // page size, in bytes

MEM_COMMIT, // allocate a committed page

PAGE_READWRITE); // read/write access

if (lpvResult == NULL )

{

printf("VirtualAlloc failed\n");

return EXCEPTION_EXECUTE_HANDLER;

} else {

printf ("Allocating another page.\n");

}

// Increment the page count, and advance lpNxtPage to the next page.

dwPages++;

lpNxtPage += dwPageSize;

// Continue execution where the page fault occurred.

return EXCEPTION_CONTINUE_EXECUTION;

}

VOID ErrorExit(LPTSTR oops)

{

printf ("Error! %s with error code of %ld\n",

oops, GetLastError ());

exit (0);

}

VOID main(VOID)

{

LPVOID lpvBase; // base address of the test memory

LPTSTR lpPtr; // generic character pointer

BOOL bSuccess; // flag

DWORD i; // generic counter

SYSTEM_INFO sSysInfo; // useful information about the system

GetSystemInfo(&sSysInfo); // populate the system information structure

printf ("This computer has a page size of %d.\n", sSysInfo.dwPageSize);

dwPageSize = sSysInfo.dwPageSize;

// Reserve pages in the process's virtual address space.

lpvBase = VirtualAlloc(

NULL, // system selects address

PAGELIMIT*dwPageSize, // size of allocation

MEM_RESERVE, // allocate reserved pages

PAGE_NOACCESS); // protection = no access

if (lpvBase == NULL )

ErrorExit("VirtualAlloc reserve failed");

lpPtr = lpNxtPage = (LPTSTR) lpvBase;

// Use structured exception handling when accessing the pages.

// If a page fault occurs, the exception filter is executed to

// commit another page from the reserved block of pages.

for (i=0; i < PAGELIMIT*dwPageSize; i++)

{

__try

{

// Write to memory.

lpPtr[i] = 'a';

}

// If there's a page fault, commit another page and try again.

__except ( PageFaultExceptionFilter( GetExceptionCode() ) )

{

// This is executed only if the filter function is unsuccessful

// in committing the next page.

ExitProcess( GetLastError() );

}

}

// Release the block of pages when you are finished using them.

bSuccess = VirtualFree(

lpvBase, // base address of block

0, // bytes of committed pages

MEM_RELEASE); // decommit the pages

printf ("Release was %s.\n", bSuccess ? "successful" : "unsuccessful" );

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有