VirtualBox

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

Last change on this file since 34801 was 33269, checked in by vboxsync, 14 years ago

IPRT: A quick replacement of the RTMemPage* and RTMemExec* APIs on posix. (Turned out to be a bit more work than expected because of the electric fence heap and init dependencies.)

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