VirtualBox

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

Last change on this file since 68119 was 62592, checked in by vboxsync, 8 years ago

IPRT: More unused parameters and undefined preprocessor macor warning (C4668) fixes/workarounds. The latter triggers in stdint.h from the compiler and in windows SDK/DDK headers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.8 KB
Line 
1/* $Id: alloc-win.cpp 62592 2016-07-27 13:24:48Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/err.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 *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
99{
100 RT_NOREF_PV(pszTag);
101
102#ifdef USE_VIRTUAL_ALLOC
103 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
104#else
105 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
106#endif
107 if (pv)
108 {
109 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
110 return pv;
111 }
112 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
113 return NULL;
114}
115
116
117RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
118{
119 RT_NOREF_PV(cb);
120
121 if (pv)
122 {
123#ifdef USE_VIRTUAL_ALLOC
124 if (!VirtualFree(pv, 0, MEM_RELEASE))
125 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
126#else
127 _aligned_free(pv);
128#endif
129 }
130}
131
132
133RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
134{
135 /*
136 * Validate input.
137 */
138 if (cb == 0)
139 {
140 AssertMsgFailed(("!cb\n"));
141 return VERR_INVALID_PARAMETER;
142 }
143 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
144 {
145 AssertMsgFailed(("fProtect=%#x\n", fProtect));
146 return VERR_INVALID_PARAMETER;
147 }
148
149 /*
150 * Convert the flags.
151 */
152 int fProt;
153 Assert(!RTMEM_PROT_NONE);
154 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
155 {
156 case RTMEM_PROT_NONE:
157 fProt = PAGE_NOACCESS;
158 break;
159
160 case RTMEM_PROT_READ:
161 fProt = PAGE_READONLY;
162 break;
163
164 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
165 fProt = PAGE_READWRITE;
166 break;
167
168 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
169 fProt = PAGE_EXECUTE_READWRITE;
170 break;
171
172 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
173 fProt = PAGE_EXECUTE_READWRITE;
174 break;
175
176 case RTMEM_PROT_WRITE:
177 fProt = PAGE_READWRITE;
178 break;
179
180 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
181 fProt = PAGE_EXECUTE_READWRITE;
182 break;
183
184 case RTMEM_PROT_EXEC:
185 fProt = PAGE_EXECUTE_READWRITE;
186 break;
187
188 /* If the compiler had any brains it would warn about this case. */
189 default:
190 AssertMsgFailed(("fProtect=%#x\n", fProtect));
191 return VERR_INTERNAL_ERROR;
192 }
193
194 /*
195 * Align the request.
196 */
197 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
198 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
199
200 /*
201 * Change the page attributes.
202 */
203 DWORD fFlags = 0;
204 if (VirtualProtect(pv, cb, fProt, &fFlags))
205 return VINF_SUCCESS;
206 return RTErrConvertFromWin32(GetLastError());
207}
208
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