Показать сообщение отдельно

  #436  
Старый 16.11.2007, 10:44
iv.
Познавший АНТИЧАТ
Регистрация: 21.03.2007
Сообщений: 1,200
Провел на форуме:
7134052

Репутация: 1204


По умолчанию

Код:
#include <stdio.h>
#include <windows.h>
#define _WIN32_WINNT 0x0501
#define MEMORY_REQUESTED 1024*1024	// request a megabyte

void main(void)
{
	BOOL bResult;                   // generic Boolean value
	ULONG_PTR NumberOfPages;        // number of pages to request
	ULONG_PTR NumberOfPagesInitial; // initial number of pages requested
	ULONG_PTR *aPFNs;               // page info; holds opaque data
	PVOID lpMemReserved;            // AWE window
	SYSTEM_INFO sSysInfo;           // useful system information
	int PFNArraySize;               // memory to request for PFN array

	GetSystemInfo(&sSysInfo);		// fill the system information structure
	printf("This computer has page size %d.\n", sSysInfo.dwPageSize);
	NumberOfPages = MEMORY_REQUESTED/sSysInfo.dwPageSize;	// calculate the number of pages of memory to request
	printf("Requesting %d pages of memory.\n", NumberOfPages);
	PFNArraySize = NumberOfPages * sizeof (ULONG_PTR);		// calculate the size of the user PFN array
	printf ("Requesting a PFN array of %d bytes.\n", PFNArraySize);

	aPFNs = (ULONG_PTR *) HeapAlloc(GetProcessHeap(), 0, PFNArraySize);

	NumberOfPagesInitial = NumberOfPages;
	bResult = AllocateUserPhysicalPages(GetCurrentProcess(), &NumberOfPages, aPFNs); // allocate the physical memory
    
	if(bResult != TRUE) printf("Cannot allocate physical pages (%u)\n", GetLastError()); return;
	if(NumberOfPagesInitial != NumberOfPages) printf("Allocated only %p pages.\n", NumberOfPages); return;

	lpMemReserved = VirtualAlloc(NULL, MEMORY_REQUESTED, MEM_RESERVE | MEM_PHYSICAL, PAGE_READWRITE); // reserve the virtual memory
	if(lpMemReserved == NULL) printf("Cannot reserve memory.\n"); return;
    
	bResult = MapUserPhysicalPages(lpMemReserved, NumberOfPages, aPFNs);
	if(bResult != TRUE) printf("MapUserPhysicalPages failed (%u)\n", GetLastError()); return;

	bResult = MapUserPhysicalPages(lpMemReserved, NumberOfPages, NULL); // unmap
	if(bResult != TRUE) printf("MapUserPhysicalPages failed (%u)\n", GetLastError()); return;
  
	bResult = FreeUserPhysicalPages(GetCurrentProcess(), &NumberOfPages, aPFNs); // free the physical pages
	if(bResult != TRUE) printf("Cannot free physical pages, error %u.\n", GetLastError()); return;

	bResult = VirtualFree(lpMemReserved, 0, MEM_RELEASE); // free virtual memory
	bResult = HeapFree(GetProcessHeap(), 0, aPFNs); // release the aPFNs array
	if(bResult != TRUE) printf("Call to HeapFree has failed (%u)\n", GetLastError());

	return;
}
 
Ответить с цитированием