龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

.Net应用程序突破2G的内存访问限制方法(4)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
下面是内存映射的代码: 也很简单: 首先先通过VirtualAlloc函数申请一块32位虚拟内存区域 然后通过 MapUserPhysicalPages API 函数将AWE内存映射到这个虚拟内存

下面是内存映射的代码:

也很简单:

首先先通过VirtualAlloc函数申请一块32位虚拟内存区域

然后通过 MapUserPhysicalPages API 函数将AWE内存映射到这个虚拟内存地址区域。

以下为引用的内容:

public void Map(bool readOnly)
{
unsafe
{
if (IsMapped)
{
return;
}

if (readOnly)
{
_VirtualAddress = AweApi.VirtualAlloc(null, Capacity, AweApi.MEM_RESERVE | AweApi.MEM_PHYSICAL,
AweApi.PAGE_READONLY);
}
else
{
_VirtualAddress = AweApi.VirtualAlloc(null, Capacity, AweApi.MEM_RESERVE | AweApi.MEM_PHYSICAL,
AweApi.PAGE_READWRITE);
}

if (_VirtualAddress == null)
{
throw new AweStreamException("Cannot reserve memory.", AweStreamException.Reason.CannotReserveMemory);
}

if (!AweApi.MapUserPhysicalPages(_VirtualAddress, _NumberOfPages, _PFNArray))
{
AweApi.VirtualFree(_VirtualAddress, Capacity, AweApi.MEM_RELEASE);
_VirtualAddress = null;
throw new AweStreamException(string.Format("MapUserPhysicalPages failed ({0})", General.GetLastError()),
AweStreamException.Reason.MapUserPhysicalPagesFail);
}

_CanWrite = !readOnly;
}
}

去映射和归还AWE内存的过程是上面两个过程的逆过程,这里就不再多讲。


精彩图集

赞助商链接