VirtualBox

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

Last change on this file since 28317 was 28317, checked in by vboxsync, 15 years ago

RTMemPageFree + all users: Added size parameter to RTMemPageFree so we can avoid tracking structures when using mmap/munmap.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: alloc-win.cpp 28317 2010-04-14 18:06:05Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Win32.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_MEM
36#include <Windows.h>
37
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/param.h>
41#include <iprt/string.h>
42#include <iprt/err.h>
43
44#ifndef USE_VIRTUAL_ALLOC
45# include <malloc.h>
46#endif
47
48
49/**
50 * Allocates memory which may contain code.
51 *
52 * @returns Pointer to the allocated memory.
53 * @returns NULL on failure.
54 * @param cb Size in bytes of the memory block to allocate.
55 */
56RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
57{
58 /*
59 * Allocate first.
60 */
61 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
62 cb = RT_ALIGN_Z(cb, 32);
63 void *pv = malloc(cb);
64 AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
65 if (pv)
66 {
67 /*
68 * Add PROT_EXEC flag to the page.
69 *
70 * This is in violation of the SuS where I think it saith that mprotect() shall
71 * only be used with mmap()'ed memory. Works on linux and OS/2 LIBC v0.6.
72 */
73 memset(pv, 0xcc, cb);
74 void *pvProt = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
75 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
76 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
77 DWORD fFlags = 0;
78 if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
79 {
80 AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
81 free(pv);
82 pv = NULL;
83 }
84 }
85 return pv;
86}
87
88
89/**
90 * Free executable/read/write memory allocated by RTMemExecAlloc().
91 *
92 * @param pv Pointer to memory block.
93 */
94RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
95{
96 if (pv)
97 free(pv);
98}
99
100
101/**
102 * Allocate page aligned memory.
103 *
104 * @returns Pointer to the allocated memory.
105 * @returns NULL if we're out of memory.
106 * @param cb Size of the memory block. Will be rounded up to page size.
107 */
108RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW
109{
110#ifdef USE_VIRTUAL_ALLOC
111 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
112#else
113 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
114#endif
115 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
116 return pv;
117}
118
119
120/**
121 * Allocate zero'ed page aligned memory.
122 *
123 * @returns Pointer to the allocated memory.
124 * @returns NULL if we're out of memory.
125 * @param cb Size of the memory block. Will be rounded up to page size.
126 */
127RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
128{
129#ifdef USE_VIRTUAL_ALLOC
130 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
131#else
132 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
133#endif
134 if (pv)
135 {
136 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
137 return pv;
138 }
139 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
140 return NULL;
141}
142
143
144/**
145 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
146 *
147 * @param pv Pointer to the block as it was returned by the allocation function.
148 * NULL will be ignored.
149 */
150RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
151{
152 if (pv)
153 {
154#ifdef USE_VIRTUAL_ALLOC
155 if (!VirtualFree(pv, 0, MEM_RELEASE))
156 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
157#else
158 _aligned_free(pv);
159#endif
160 }
161}
162
163
164/**
165 * Change the page level protection of a memory region.
166 *
167 * @returns iprt status code.
168 * @param pv Start of the region. Will be rounded down to nearest page boundary.
169 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
170 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
171 */
172RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
173{
174 /*
175 * Validate input.
176 */
177 if (cb == 0)
178 {
179 AssertMsgFailed(("!cb\n"));
180 return VERR_INVALID_PARAMETER;
181 }
182 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
183 {
184 AssertMsgFailed(("fProtect=%#x\n", fProtect));
185 return VERR_INVALID_PARAMETER;
186 }
187
188 /*
189 * Convert the flags.
190 */
191 int fProt;
192 Assert(!RTMEM_PROT_NONE);
193 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
194 {
195 case RTMEM_PROT_NONE:
196 fProt = PAGE_NOACCESS;
197 break;
198
199 case RTMEM_PROT_READ:
200 fProt = PAGE_READONLY;
201 break;
202
203 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
204 fProt = PAGE_READWRITE;
205 break;
206
207 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
208 fProt = PAGE_EXECUTE_READWRITE;
209 break;
210
211 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
212 fProt = PAGE_EXECUTE_READWRITE;
213 break;
214
215 case RTMEM_PROT_WRITE:
216 fProt = PAGE_READWRITE;
217 break;
218
219 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
220 fProt = PAGE_EXECUTE_READWRITE;
221 break;
222
223 case RTMEM_PROT_EXEC:
224 fProt = PAGE_EXECUTE_READWRITE;
225 break;
226
227 /* If the compiler had any brains it would warn about this case. */
228 default:
229 AssertMsgFailed(("fProtect=%#x\n", fProtect));
230 return VERR_INTERNAL_ERROR;
231 }
232
233 /*
234 * Align the request.
235 */
236 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
237 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
238
239 /*
240 * Change the page attributes.
241 */
242 DWORD fFlags = 0;
243 if (VirtualProtect(pv, cb, fProt, &fFlags))
244 return VINF_SUCCESS;
245 return RTErrConvertFromWin32(GetLastError());
246}
247
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