无法分配,例如3GB大页面

时间:2017-10-27 13:44:13

标签: c++ windows

当我使用VirtualAlloc提交内存时,我可以分配3,4 GB内存而不会出现问题。但是当我尝试将内存分配为大页面时,我就陷入了麻烦。

我得到LastError

  

87参数无效

     

     

1450没有足够的系统资源来完成所请求的服务。

在我的32 GB Win 10 Creators更新机器上,我仍有21 GB的可用内存。提交计数为17,1 GB,因此这也不用担心。什么限制了我分配大量大页面的能力?

如果我无法分配大量页面,那么大页面有用吗?

#include <windows.h>

int * LargePageAlloc(int64_t numberOfBytesToAllocate)
{
    wprintf(L"\nLarge Page allocator used.");

    HANDLE proc_h = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());

    HANDLE hToken;
    OpenProcessToken(proc_h, TOKEN_ADJUST_PRIVILEGES, &hToken);
    CloseHandle(proc_h);

    LUID luid;
    ::LookupPrivilegeValue(0, SE_LOCK_MEMORY_NAME, &luid);

    TOKEN_PRIVILEGES tp;

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    auto status = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), (PTOKEN_PRIVILEGES)NULL, 0);
    CloseHandle(hToken);

    if (status != TRUE)
    {
        wprintf(L"\nSeLockMemoryPrivilege could not be acquired. LastError: %d. Use secpol.msc to assign to your user account the SeLockMemoryPrivilege privilege.", ::GetLastError());
        return 0;
    }

    auto p = (int *)VirtualAlloc(nullptr, numberOfBytesToAllocate, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES , PAGE_READWRITE);

    if (::GetLastError() == 1314)
    {
        wprintf(L"\nNo Privilege held. Use secpol.msc to assign to your user account the SeLockMemoryPrivilege privilege.");
    }

    return p;
}


void _tmain(int argc, _TCHAR* argv[])
{
    int *pBuffer = LargePageAlloc(3000uL*1024*1024);
}

0 个答案:

没有答案
相关问题