|
Using the Virtual Memory Functions
This section explains how to use the virtual memory functions for dynamic
allocation.
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 kernel 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.
The example uses try-except 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 about structured exception handling,
see 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 is called twice: first to decommit the committed pages and again to
release the entire region of reserved pages.
#define PAGELIMIT 80
#define PAGESIZE 0x1000
INT PageFaultExceptionFilter(DWORD);
VOID MyErrorExit(LPTSTR);
LPTSTR lpNxtPage;
DWORD dwPages = 0;
VOID UseDynamicVirtualAlloc(VOID) {
LPVOID lpvBase;
LPTSTR lpPtr;
BOOL bSuccess;
DWORD i;
/* Reserve pages in the process's virtual address space. */
lpvBase = VirtualAlloc(
NULL, /* system selects address */
PAGELIMIT*PAGESIZE, /* size of allocation */
MEM_RESERVE, /* allocates reserved pages */
PAGE_NOACCESS); /* protection = no access */
if (lpvBase == NULL )
MyErrorExit("VirtualAlloc reserve");
lpPtr = lpNxtPage = (LPTSTR) lpvBase;
/*
* Use try-except 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*PAGESIZE; i++) {
try {
/* Write to memory. */
lpPtr[i] = 'a';
}
/*
* If there is 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. */
/* First, decommit the committed pages. */
bSuccess = VirtualFree(
lpvBase, /* base address of block */
dwPages*PAGESIZE, /* bytes of committed pages */
MEM_DECOMMIT); /* decommit the pages */
/* Release the entire block. */
if (bSuccess)
bSuccess = VirtualFree(
lpvBase, /* base address of block */
0, /* releases the entire block */
MEM_RELEASE); /* releases the pages */
}
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("page fault\n");
/* If the reserved pages are used up, exit. */
if (dwPages >= PAGELIMIT) {
printf("out of pages\n");
return EXCEPTION_EXECUTE_HANDLER;
}
/* Otherwise, commit another page. */
lpvResult = VirtualAlloc(
(LPVOID) lpNxtPage, /* next page to commit */
PAGESIZE, /* page size, in bytes */
MEM_COMMIT, /* alloc committed page */
PAGE_READWRITE); /* read-write access */
if (lpvResult == NULL ) {
printf("VirtualAlloc failed\n");
return EXCEPTION_EXECUTE_HANDLER;
}
/*
* Increment the page count, and advance lpNxtPage
* to the next page.
*/
dwPages++;
lpNxtPage += PAGESIZE;
/* Continue execution where the page fault occurred. */
return EXCEPTION_CONTINUE_EXECUTION;
}
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|