allocex-win.cpp revision 83de81b6f22c80532a478e79afaee21a0887302a
83de81b6f22c80532a478e79afaee21a0887302avboxsync * IPRT - Memory Allocation, Extended Alloc Workers, Windows.
83de81b6f22c80532a478e79afaee21a0887302avboxsync * Copyright (C) 2006-2013 Oracle Corporation
83de81b6f22c80532a478e79afaee21a0887302avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
83de81b6f22c80532a478e79afaee21a0887302avboxsync * available from http://www.virtualbox.org. This file is free software;
83de81b6f22c80532a478e79afaee21a0887302avboxsync * you can redistribute it and/or modify it under the terms of the GNU
83de81b6f22c80532a478e79afaee21a0887302avboxsync * General Public License (GPL) as published by the Free Software
83de81b6f22c80532a478e79afaee21a0887302avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
83de81b6f22c80532a478e79afaee21a0887302avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
83de81b6f22c80532a478e79afaee21a0887302avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
83de81b6f22c80532a478e79afaee21a0887302avboxsync * The contents of this file may alternatively be used under the terms
83de81b6f22c80532a478e79afaee21a0887302avboxsync * of the Common Development and Distribution License Version 1.0
83de81b6f22c80532a478e79afaee21a0887302avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
83de81b6f22c80532a478e79afaee21a0887302avboxsync * VirtualBox OSE distribution, in which case the provisions of the
83de81b6f22c80532a478e79afaee21a0887302avboxsync * CDDL are applicable instead of those of the GPL.
83de81b6f22c80532a478e79afaee21a0887302avboxsync * You may elect to license modified versions of this file under the
83de81b6f22c80532a478e79afaee21a0887302avboxsync * terms and conditions of either the GPL or the CDDL or both.
83de81b6f22c80532a478e79afaee21a0887302avboxsync/*******************************************************************************
83de81b6f22c80532a478e79afaee21a0887302avboxsync* Header Files *
83de81b6f22c80532a478e79afaee21a0887302avboxsync*******************************************************************************/
83de81b6f22c80532a478e79afaee21a0887302avboxsyncstatic int rtMemAllocExInRange(size_t cbAlloc, uint32_t fFlags, void **ppv, uintptr_t uAddr, uintptr_t uAddrLast)
83de81b6f22c80532a478e79afaee21a0887302avboxsync * Try with every possible address hint since the possible range is very limited.
83de81b6f22c80532a478e79afaee21a0887302avboxsync DWORD fPageProt = (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
83de81b6f22c80532a478e79afaee21a0887302avboxsync SIZE_T cbRange = VirtualQuery((void *)uAddr, &MemInfo, sizeof(MemInfo));
83de81b6f22c80532a478e79afaee21a0887302avboxsync AssertReturn(cbRange == sizeof(MemInfo), VERR_NOT_SUPPORTED);
83de81b6f22c80532a478e79afaee21a0887302avboxsync void *pv = VirtualAlloc((void *)uAddr, cbAlloc, MEM_RESERVE | MEM_COMMIT, fPageProt);
83de81b6f22c80532a478e79afaee21a0887302avboxsync AssertStmt(!pv, VirtualFree(pv, cbAlloc, MEM_RELEASE));
83de81b6f22c80532a478e79afaee21a0887302avboxsync /* skip ahead */
83de81b6f22c80532a478e79afaee21a0887302avboxsync uintptr_t uAddrNext = (uintptr_t)MemInfo.BaseAddress + MemInfo.RegionSize;
83de81b6f22c80532a478e79afaee21a0887302avboxsyncDECLHIDDEN(int) rtMemAllocEx16BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
83de81b6f22c80532a478e79afaee21a0887302avboxsync AssertReturn(cbAlloc <= _64K - PAGE_SIZE, VERR_NO_MEMORY);
83de81b6f22c80532a478e79afaee21a0887302avboxsync /* Seems this doesn't work on W7/64... */
83de81b6f22c80532a478e79afaee21a0887302avboxsync return rtMemAllocExInRange(cbAlloc, fFlags, ppv, PAGE_SIZE, _64K - cbAlloc);
83de81b6f22c80532a478e79afaee21a0887302avboxsyncDECLHIDDEN(int) rtMemAllocEx32BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
83de81b6f22c80532a478e79afaee21a0887302avboxsync * Just try first.
83de81b6f22c80532a478e79afaee21a0887302avboxsync DWORD fPageProt = (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
83de81b6f22c80532a478e79afaee21a0887302avboxsync void *pv = VirtualAlloc(NULL, cbAlloc, MEM_RESERVE | MEM_COMMIT, fPageProt);
83de81b6f22c80532a478e79afaee21a0887302avboxsync * No luck, do address scan based allocation.
83de81b6f22c80532a478e79afaee21a0887302avboxsync return rtMemAllocExInRange(cbAlloc, fFlags, ppv, _64K, _4G - cbAlloc);