VirtualBox

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

Last change on this file since 99828 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.0 KB
Line 
1/* $Id: alloc-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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 *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
59{
60 RT_NOREF_PV(pszTag);
61
62#ifdef USE_VIRTUAL_ALLOC
63 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
64#else
65 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
66#endif
67 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
68 return pv;
69}
70
71
72RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
73{
74 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
75 RT_NOREF_PV(pszTag);
76 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
77
78#ifdef USE_VIRTUAL_ALLOC
79 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE);
80#else
81 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE);
82#endif
83 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL);
84
85 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
86 {
87 /** @todo check why we get ERROR_WORKING_SET_QUOTA here. */
88 BOOL const fOkay = VirtualLock(pv, cbAligned);
89 AssertMsg(fOkay || GetLastError() == ERROR_WORKING_SET_QUOTA, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError()));
90 NOREF(fOkay);
91 }
92
93 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
94 RT_BZERO(pv, cbAligned);
95
96 return pv;
97}
98
99
100RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
101{
102 RT_NOREF_PV(pszTag);
103
104#ifdef USE_VIRTUAL_ALLOC
105 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
106#else
107 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
108#endif
109 if (pv)
110 {
111 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
112 return pv;
113 }
114 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
115 return NULL;
116}
117
118
119RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
120{
121 RT_NOREF_PV(cb);
122
123 if (pv)
124 {
125#ifdef USE_VIRTUAL_ALLOC
126 if (!VirtualFree(pv, 0, MEM_RELEASE))
127 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
128#else
129 _aligned_free(pv);
130#endif
131 }
132}
133
134
135RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
136{
137 /*
138 * Validate input.
139 */
140 if (cb == 0)
141 {
142 AssertMsgFailed(("!cb\n"));
143 return VERR_INVALID_PARAMETER;
144 }
145 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
146 {
147 AssertMsgFailed(("fProtect=%#x\n", fProtect));
148 return VERR_INVALID_PARAMETER;
149 }
150
151 /*
152 * Convert the flags.
153 */
154 int fProt;
155 Assert(!RTMEM_PROT_NONE);
156 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
157 {
158 case RTMEM_PROT_NONE:
159 fProt = PAGE_NOACCESS;
160 break;
161
162 case RTMEM_PROT_READ:
163 fProt = PAGE_READONLY;
164 break;
165
166 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
167 fProt = PAGE_READWRITE;
168 break;
169
170 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
171 fProt = PAGE_EXECUTE_READWRITE;
172 break;
173
174 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
175 fProt = PAGE_EXECUTE_READWRITE;
176 break;
177
178 case RTMEM_PROT_WRITE:
179 fProt = PAGE_READWRITE;
180 break;
181
182 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
183 fProt = PAGE_EXECUTE_READWRITE;
184 break;
185
186 case RTMEM_PROT_EXEC:
187 fProt = PAGE_EXECUTE_READWRITE;
188 break;
189
190 /* If the compiler had any brains it would warn about this case. */
191 default:
192 AssertMsgFailed(("fProtect=%#x\n", fProtect));
193 return VERR_INTERNAL_ERROR;
194 }
195
196 /*
197 * Align the request.
198 */
199 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
200 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
201
202 /*
203 * Change the page attributes.
204 */
205 DWORD fFlags = 0;
206 if (VirtualProtect(pv, cb, fProt, &fFlags))
207 return VINF_SUCCESS;
208 return RTErrConvertFromWin32(GetLastError());
209}
210
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use