VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/alloc-win.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.4 KB
Line 
1/* $Id: alloc-win.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#ifdef IPRT_NO_CRT
42# define USE_VIRTUAL_ALLOC
43#endif
44#define LOG_GROUP RTLOGGROUP_MEM
45#include <iprt/win/windows.h>
46
47#include <iprt/alloc.h>
48#include <iprt/assert.h>
49#include <iprt/param.h>
50#include <iprt/string.h>
51#include <iprt/errcore.h>
52
53#ifndef USE_VIRTUAL_ALLOC
54# include <malloc.h>
55#endif
56
57
58RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
59{
60 RT_NOREF_PV(pszTag);
61
62 /*
63 * Allocate first.
64 */
65 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
66#ifdef USE_VIRTUAL_ALLOC
67 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
68 void *pv = VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
69 AssertMsg(pv, ("VirtualAlloc(%zx) failed!!!\n", cb));
70#else
71 cb = RT_ALIGN_Z(cb, 32);
72 void *pv = malloc(cb);
73 AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
74 if (pv)
75 {
76 memset(pv, 0xcc, cb);
77 void *pvProt = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
78 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
79 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
80 DWORD fFlags = 0;
81 if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
82 {
83 AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
84 free(pv);
85 pv = NULL;
86 }
87 }
88#endif
89 return pv;
90}
91
92
93RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_DEF
94{
95 RT_NOREF_PV(cb);
96
97 if (pv)
98#ifdef USE_VIRTUAL_ALLOC
99 if (!VirtualFree(pv, 0, MEM_RELEASE))
100 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
101#else
102 free(pv);
103#endif
104}
105
106
107RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
108{
109 RT_NOREF_PV(pszTag);
110
111#ifdef USE_VIRTUAL_ALLOC
112 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
113#else
114 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
115#endif
116 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
117 return pv;
118}
119
120
121RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
122{
123 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
124 RT_NOREF_PV(pszTag);
125 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
126
127#ifdef USE_VIRTUAL_ALLOC
128 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE);
129#else
130 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE);
131#endif
132 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL);
133
134 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
135 {
136 /** @todo check why we get ERROR_WORKING_SET_QUOTA here. */
137 BOOL const fOkay = VirtualLock(pv, cbAligned);
138 AssertMsg(fOkay || GetLastError() == ERROR_WORKING_SET_QUOTA, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError()));
139 NOREF(fOkay);
140 }
141
142 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
143 RT_BZERO(pv, cbAligned);
144
145 return pv;
146}
147
148
149RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
150{
151 RT_NOREF_PV(pszTag);
152
153#ifdef USE_VIRTUAL_ALLOC
154 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
155#else
156 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
157#endif
158 if (pv)
159 {
160 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
161 return pv;
162 }
163 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
164 return NULL;
165}
166
167
168RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
169{
170 RT_NOREF_PV(cb);
171
172 if (pv)
173 {
174#ifdef USE_VIRTUAL_ALLOC
175 if (!VirtualFree(pv, 0, MEM_RELEASE))
176 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
177#else
178 _aligned_free(pv);
179#endif
180 }
181}
182
183
184RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
185{
186 /*
187 * Validate input.
188 */
189 if (cb == 0)
190 {
191 AssertMsgFailed(("!cb\n"));
192 return VERR_INVALID_PARAMETER;
193 }
194 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
195 {
196 AssertMsgFailed(("fProtect=%#x\n", fProtect));
197 return VERR_INVALID_PARAMETER;
198 }
199
200 /*
201 * Convert the flags.
202 */
203 int fProt;
204 Assert(!RTMEM_PROT_NONE);
205 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
206 {
207 case RTMEM_PROT_NONE:
208 fProt = PAGE_NOACCESS;
209 break;
210
211 case RTMEM_PROT_READ:
212 fProt = PAGE_READONLY;
213 break;
214
215 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
216 fProt = PAGE_READWRITE;
217 break;
218
219 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
220 fProt = PAGE_EXECUTE_READWRITE;
221 break;
222
223 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
224 fProt = PAGE_EXECUTE_READWRITE;
225 break;
226
227 case RTMEM_PROT_WRITE:
228 fProt = PAGE_READWRITE;
229 break;
230
231 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
232 fProt = PAGE_EXECUTE_READWRITE;
233 break;
234
235 case RTMEM_PROT_EXEC:
236 fProt = PAGE_EXECUTE_READWRITE;
237 break;
238
239 /* If the compiler had any brains it would warn about this case. */
240 default:
241 AssertMsgFailed(("fProtect=%#x\n", fProtect));
242 return VERR_INTERNAL_ERROR;
243 }
244
245 /*
246 * Align the request.
247 */
248 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
249 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
250
251 /*
252 * Change the page attributes.
253 */
254 DWORD fFlags = 0;
255 if (VirtualProtect(pv, cb, fProt, &fFlags))
256 return VINF_SUCCESS;
257 return RTErrConvertFromWin32(GetLastError());
258}
259
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use