VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/allocex-win.cpp@ 95084

Last change on this file since 95084 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: allocex-win.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Extended Alloc Workers, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define RTMEM_NO_WRAP_TO_EF_APIS
32#include <iprt/mem.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/errcore.h>
37#include <iprt/string.h>
38#include <iprt/param.h>
39#include "../allocex.h"
40
41#include <iprt/win/windows.h>
42
43
44static int rtMemAllocExInRange(size_t cbAlloc, uint32_t fFlags, void **ppv, uintptr_t uAddr, uintptr_t uAddrLast)
45{
46 /*
47 * Try with every possible address hint since the possible range is very limited.
48 */
49 DWORD fPageProt = (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
50 while (uAddr <= uAddrLast)
51 {
52 MEMORY_BASIC_INFORMATION MemInfo;
53 SIZE_T cbRange = VirtualQuery((void *)uAddr, &MemInfo, sizeof(MemInfo));
54 AssertReturn(cbRange == sizeof(MemInfo), VERR_NOT_SUPPORTED);
55 Assert(MemInfo.RegionSize > 0);
56
57 if ( MemInfo.State == MEM_FREE
58 && MemInfo.RegionSize >= cbAlloc)
59 {
60 void *pv = VirtualAlloc((void *)uAddr, cbAlloc, MEM_RESERVE | MEM_COMMIT, fPageProt);
61 if ((uintptr_t)pv == uAddr)
62 {
63 *ppv = pv;
64 return VINF_SUCCESS;
65 }
66 AssertStmt(!pv, VirtualFree(pv, cbAlloc, MEM_RELEASE));
67 }
68
69 /* skip ahead */
70 uintptr_t uAddrNext = (uintptr_t)MemInfo.BaseAddress + MemInfo.RegionSize;
71 if (uAddrNext <= uAddr)
72 break;
73 uAddr += uAddrNext;
74 }
75
76 return VERR_NO_MEMORY;
77}
78
79
80DECLHIDDEN(int) rtMemAllocEx16BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
81{
82 cbAlloc = RT_ALIGN_Z(cbAlloc, PAGE_SIZE);
83 AssertReturn(cbAlloc <= _64K - PAGE_SIZE, VERR_NO_MEMORY);
84
85 /* Seems this doesn't work on W7/64... */
86 return rtMemAllocExInRange(cbAlloc, fFlags, ppv, PAGE_SIZE, _64K - cbAlloc);
87}
88
89
90DECLHIDDEN(int) rtMemAllocEx32BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
91{
92 cbAlloc = RT_ALIGN_Z(cbAlloc, PAGE_SIZE);
93 AssertReturn(cbAlloc <= _2G+_1G, VERR_NO_MEMORY);
94
95 /*
96 * Just try first.
97 */
98 DWORD fPageProt = (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
99 void *pv = VirtualAlloc(NULL, cbAlloc, MEM_RESERVE | MEM_COMMIT, fPageProt);
100 if (!pv)
101 return VERR_NO_MEMORY;
102 if ((uintptr_t)pv + cbAlloc - 1 < _4G)
103 {
104 *ppv = pv;
105 return VINF_SUCCESS;
106 }
107 VirtualFree(pv, cbAlloc, MEM_RELEASE);
108
109 /*
110 * No luck, do address scan based allocation.
111 */
112 return rtMemAllocExInRange(cbAlloc, fFlags, ppv, _64K, _4G - cbAlloc);
113}
114
115
116DECLHIDDEN(void) rtMemFreeExYyBitReach(void *pv, size_t cb, uint32_t fFlags)
117{
118 RT_NOREF_PV(fFlags);
119
120 BOOL fRc = VirtualFree(pv, cb, MEM_RELEASE);
121 Assert(fRc); RT_NOREF_PV(fRc);
122}
123
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette