下面是内存映射的代码:
也很简单:
首先先通过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内存的过程是上面两个过程的逆过程,这里就不再多讲。