VirtualBox

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

Last change on this file since 85561 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.7 KB
Line 
1/* $Id: alloc-win.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 USE_VIRTUAL_ALLOC*/
32#define LOG_GROUP RTLOGGROUP_MEM
33#include <iprt/win/windows.h>
34
35#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/param.h>
38#include <iprt/string.h>
39#include <iprt/errcore.h>
40
41#ifndef USE_VIRTUAL_ALLOC
42# include <malloc.h>
43#endif
44
45
46RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
47{
48 RT_NOREF_PV(pszTag);
49
50 /*
51 * Allocate first.
52 */
53 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
54 cb = RT_ALIGN_Z(cb, 32);
55 void *pv = malloc(cb);
56 AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
57 if (pv)
58 {
59 memset(pv, 0xcc, cb);
60 void *pvProt = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
61 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
62 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
63 DWORD fFlags = 0;
64 if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
65 {
66 AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
67 free(pv);
68 pv = NULL;
69 }
70 }
71 return pv;
72}
73
74
75RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_DEF
76{
77 RT_NOREF_PV(cb);
78
79 if (pv)
80 free(pv);
81}
82
83
84RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
85{
86 RT_NOREF_PV(pszTag);
87
88#ifdef USE_VIRTUAL_ALLOC
89 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
90#else
91 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
92#endif
93 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
94 return pv;
95}
96
97
98RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
99{
100 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
101 RT_NOREF_PV(pszTag);
102 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
103
104#ifdef USE_VIRTUAL_ALLOC
105 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE);
106#else
107 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE);
108#endif
109 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL);
110
111 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
112 {
113 /** @todo check why we get ERROR_WORKING_SET_QUOTA here. */
114 BOOL const fOkay = VirtualLock(pv, cbAligned);
115 AssertMsg(fOkay || GetLastError() == ERROR_WORKING_SET_QUOTA, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError()));
116 NOREF(fOkay);
117 }
118
119 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
120 RT_BZERO(pv, cbAligned);
121
122 return pv;
123}
124
125
126RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
127{
128 RT_NOREF_PV(pszTag);
129
130#ifdef USE_VIRTUAL_ALLOC
131 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
132#else
133 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
134#endif
135 if (pv)
136 {
137 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
138 return pv;
139 }
140 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
141 return NULL;
142}
143
144
145RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
146{
147 RT_NOREF_PV(cb);
148
149 if (pv)
150 {
151#ifdef USE_VIRTUAL_ALLOC
152 if (!VirtualFree(pv, 0, MEM_RELEASE))
153 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
154#else
155 _aligned_free(pv);
156#endif
157 }
158}
159
160
161RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
162{
163 /*
164 * Validate input.
165 */
166 if (cb == 0)
167 {
168 AssertMsgFailed(("!cb\n"));
169 return VERR_INVALID_PARAMETER;
170 }
171 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
172 {
173 AssertMsgFailed(("fProtect=%#x\n", fProtect));
174 return VERR_INVALID_PARAMETER;
175 }
176
177 /*
178 * Convert the flags.
179 */
180 int fProt;
181 Assert(!RTMEM_PROT_NONE);
182 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
183 {
184 case RTMEM_PROT_NONE:
185 fProt = PAGE_NOACCESS;
186 break;
187
188 case RTMEM_PROT_READ:
189 fProt = PAGE_READONLY;
190 break;
191
192 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
193 fProt = PAGE_READWRITE;
194 break;
195
196 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
197 fProt = PAGE_EXECUTE_READWRITE;
198 break;
199
200 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
201 fProt = PAGE_EXECUTE_READWRITE;
202 break;
203
204 case RTMEM_PROT_WRITE:
205 fProt = PAGE_READWRITE;
206 break;
207
208 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
209 fProt = PAGE_EXECUTE_READWRITE;
210 break;
211
212 case RTMEM_PROT_EXEC:
213 fProt = PAGE_EXECUTE_READWRITE;
214 break;
215
216 /* If the compiler had any brains it would warn about this case. */
217 default:
218 AssertMsgFailed(("fProtect=%#x\n", fProtect));
219 return VERR_INTERNAL_ERROR;
220 }
221
222 /*
223 * Align the request.
224 */
225 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
226 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
227
228 /*
229 * Change the page attributes.
230 */
231 DWORD fFlags = 0;
232 if (VirtualProtect(pv, cb, fProt, &fFlags))
233 return VINF_SUCCESS;
234 return RTErrConvertFromWin32(GetLastError());
235}
236
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