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

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.

下面的例子阐明了使用VirtualAllocVirtualFree函数为一个动态数组预留和调拨所需的内存。首先,调用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 tabindex="0" errorurl="../common/badjump.htm" keywords="_win32_structured_exception_handling"/>Structured Exception Handling.

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

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" );
}
weixin151云匹面粉直供微信小程序+springboot后端毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值