VirtualBox

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

Last change on this file since 58029 was 57432, checked in by vboxsync, 9 years ago

iprt/cdefs.h,*: Split RT_NO_THROW into prototype and definition macros named RT_NO_THROW_PROTO and RT_NO_THROW_DEF respecitively.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.6 KB
Line 
1/* $Id: alloc-win.cpp 57432 2015-08-18 14:57:46Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 <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 /*
49 * Allocate first.
50 */
51 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
52 cb = RT_ALIGN_Z(cb, 32);
53 void *pv = malloc(cb);
54 AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
55 if (pv)
56 {
57 memset(pv, 0xcc, cb);
58 void *pvProt = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
59 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
60 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
61 DWORD fFlags = 0;
62 if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
63 {
64 AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
65 free(pv);
66 pv = NULL;
67 }
68 }
69 return pv;
70}
71
72
73RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_DEF
74{
75 if (pv)
76 free(pv);
77}
78
79
80RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
81{
82#ifdef USE_VIRTUAL_ALLOC
83 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
84#else
85 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
86#endif
87 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
88 return pv;
89}
90
91
92RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
93{
94#ifdef USE_VIRTUAL_ALLOC
95 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
96#else
97 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
98#endif
99 if (pv)
100 {
101 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
102 return pv;
103 }
104 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
105 return NULL;
106}
107
108
109RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
110{
111 if (pv)
112 {
113#ifdef USE_VIRTUAL_ALLOC
114 if (!VirtualFree(pv, 0, MEM_RELEASE))
115 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
116#else
117 _aligned_free(pv);
118#endif
119 }
120}
121
122
123RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
124{
125 /*
126 * Validate input.
127 */
128 if (cb == 0)
129 {
130 AssertMsgFailed(("!cb\n"));
131 return VERR_INVALID_PARAMETER;
132 }
133 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
134 {
135 AssertMsgFailed(("fProtect=%#x\n", fProtect));
136 return VERR_INVALID_PARAMETER;
137 }
138
139 /*
140 * Convert the flags.
141 */
142 int fProt;
143 Assert(!RTMEM_PROT_NONE);
144 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
145 {
146 case RTMEM_PROT_NONE:
147 fProt = PAGE_NOACCESS;
148 break;
149
150 case RTMEM_PROT_READ:
151 fProt = PAGE_READONLY;
152 break;
153
154 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
155 fProt = PAGE_READWRITE;
156 break;
157
158 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
159 fProt = PAGE_EXECUTE_READWRITE;
160 break;
161
162 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
163 fProt = PAGE_EXECUTE_READWRITE;
164 break;
165
166 case RTMEM_PROT_WRITE:
167 fProt = PAGE_READWRITE;
168 break;
169
170 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
171 fProt = PAGE_EXECUTE_READWRITE;
172 break;
173
174 case RTMEM_PROT_EXEC:
175 fProt = PAGE_EXECUTE_READWRITE;
176 break;
177
178 /* If the compiler had any brains it would warn about this case. */
179 default:
180 AssertMsgFailed(("fProtect=%#x\n", fProtect));
181 return VERR_INTERNAL_ERROR;
182 }
183
184 /*
185 * Align the request.
186 */
187 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
188 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
189
190 /*
191 * Change the page attributes.
192 */
193 DWORD fFlags = 0;
194 if (VirtualProtect(pv, cb, fProt, &fFlags))
195 return VINF_SUCCESS;
196 return RTErrConvertFromWin32(GetLastError());
197}
198
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