VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/alloc-posix.cpp@ 25659

Last change on this file since 25659 was 11020, checked in by vboxsync, 16 years ago

iprt: Added a RT_NO_THROW macro for wrapping up the throw() stuff, applying it to mem.h and rand.h to try make RTMemAutoPtr work as efficiently as the pure C version of the stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
1/* $Id: alloc-posix.cpp 11020 2008-07-30 22:48:35Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, POSIX.
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#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/param.h>
38#include <iprt/err.h>
39#include <iprt/string.h>
40
41#include <stdlib.h>
42#include <malloc.h>
43#include <errno.h>
44#include <sys/mman.h>
45
46#if !defined(RT_USE_MMAP) && (defined(RT_OS_LINUX))
47# define RT_USE_MMAP
48#endif
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53#ifdef RT_USE_MMAP
54/**
55 * RTMemExecAlloc() header used when using mmap for allocating the memory.
56 */
57typedef struct RTMEMEXECHDR
58{
59 /** Magic number (RTMEMEXECHDR_MAGIC). */
60 size_t uMagic;
61 /** The size we requested from mmap. */
62 size_t cb;
63# if ARCH_BITS == 32
64 uint32_t Alignment[2];
65# endif
66} RTMEMEXECHDR, *PRTMEMEXECHDR;
67
68/** Magic for RTMEMEXECHDR. */
69#define RTMEMEXECHDR_MAGIC (~(size_t)0xfeedbabe)
70
71#endif /* RT_USE_MMAP */
72
73
74
75#ifdef IN_RING3
76
77/**
78 * Allocates memory which may contain code.
79 *
80 * @returns Pointer to the allocated memory.
81 * @returns NULL on failure.
82 * @param cb Size in bytes of the memory block to allocate.
83 */
84RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
85{
86 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
87
88#ifdef RT_USE_MMAP
89 /*
90 * Use mmap to get low memory.
91 */
92 size_t cbAlloc = RT_ALIGN_Z(cb + sizeof(RTMEMEXECHDR), PAGE_SIZE);
93 void *pv = mmap(NULL, cbAlloc, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS
94#if defined(RT_ARCH_AMD64) && defined(MAP_32BIT)
95 | MAP_32BIT
96#endif
97 , -1, 0);
98 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
99 PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv;
100 pHdr->uMagic = RTMEMEXECHDR_MAGIC;
101 pHdr->cb = cbAlloc;
102 pv = pHdr + 1;
103
104#else
105 /*
106 * Allocate first.
107 */
108 cb = RT_ALIGN_Z(cb, 32);
109 void *pv = NULL;
110 int rc = posix_memalign(&pv, 32, cb);
111 AssertMsg(!rc && pv, ("posix_memalign(%zd) failed!!! rc=%d\n", cb, rc));
112 if (pv && !rc)
113 {
114 /*
115 * Add PROT_EXEC flag to the page.
116 *
117 * This is in violation of the SuS where I think it saith that mprotect() shall
118 * only be used with mmap()'ed memory. Works on linux and OS/2 LIBC v0.6.
119 */
120 memset(pv, 0xcc, cb);
121 void *pvProt = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
122 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
123 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
124 rc = mprotect(pvProt, cbProt, PROT_READ | PROT_WRITE | PROT_EXEC);
125 if (rc)
126 {
127 AssertMsgFailed(("mprotect(%p, %#zx,,) -> rc=%d, errno=%d\n", pvProt, cbProt, rc, errno));
128 free(pv);
129 pv = NULL;
130 }
131 }
132#endif
133 return pv;
134}
135
136
137/**
138 * Free executable/read/write memory allocated by RTMemExecAlloc().
139 *
140 * @param pv Pointer to memory block.
141 */
142RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
143{
144 if (pv)
145 {
146#ifdef RT_USE_MMAP
147 PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv - 1;
148 AssertMsgReturnVoid(RT_ALIGN_P(pHdr, PAGE_SIZE) == pHdr, ("pHdr=%p pv=%p\n", pHdr, pv));
149 AssertMsgReturnVoid(pHdr->uMagic == RTMEMEXECHDR_MAGIC, ("pHdr=%p(uMagic=%#zx) pv=%p\n", pHdr, pHdr->uMagic, pv));
150 int rc = munmap(pHdr, pHdr->cb);
151 AssertMsg(!rc, ("munmap -> %d errno=%d\n", rc, errno)); NOREF(rc);
152#else
153 free(pv);
154#endif
155 }
156}
157
158
159/**
160 * Allocate page aligned memory.
161 *
162 * @returns Pointer to the allocated memory.
163 * @returns NULL if we're out of memory.
164 * @param cb Size of the memory block. Will be rounded up to page size.
165 */
166RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW
167{
168#if 0 /** @todo huh? we're using posix_memalign in the next function... */
169 void *pv;
170 int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
171 if (!rc)
172 return pv;
173 return NULL;
174#else
175 return memalign(PAGE_SIZE, cb);
176#endif
177}
178
179
180/**
181 * Allocate zero'ed page aligned memory.
182 *
183 * @returns Pointer to the allocated memory.
184 * @returns NULL if we're out of memory.
185 * @param cb Size of the memory block. Will be rounded up to page size.
186 */
187RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
188{
189 void *pv;
190 int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
191 if (!rc)
192 {
193 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
194 return pv;
195 }
196 return NULL;
197}
198
199
200/**
201 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
202 *
203 * @param pv Pointer to the block as it was returned by the allocation function.
204 * NULL will be ignored.
205 */
206RTDECL(void) RTMemPageFree(void *pv) RT_NO_THROW
207{
208 if (pv)
209 free(pv);
210}
211
212
213/**
214 * Change the page level protection of a memory region.
215 *
216 * @returns iprt status code.
217 * @param pv Start of the region. Will be rounded down to nearest page boundary.
218 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
219 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
220 */
221RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
222{
223 /*
224 * Validate input.
225 */
226 if (cb == 0)
227 {
228 AssertMsgFailed(("!cb\n"));
229 return VERR_INVALID_PARAMETER;
230 }
231 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
232 {
233 AssertMsgFailed(("fProtect=%#x\n", fProtect));
234 return VERR_INVALID_PARAMETER;
235 }
236
237 /*
238 * Convert the flags.
239 */
240 int fProt;
241#if RTMEM_PROT_NONE == PROT_NONE \
242 && RTMEM_PROT_READ == PROT_READ \
243 && RTMEM_PROT_WRITE == PROT_WRITE \
244 && RTMEM_PROT_EXEC == PROT_EXEC
245 fProt = fProtect;
246#else
247 Assert(!RTMEM_PROT_NONE);
248 if (!fProtect)
249 fProt = PROT_NONE;
250 else
251 {
252 fProt = 0;
253 if (fProtect & RTMEM_PROT_READ)
254 fProt |= PROT_READ;
255 if (fProtect & RTMEM_PROT_WRITE)
256 fProt |= PROT_WRITE;
257 if (fProtect & RTMEM_PROT_EXEC)
258 fProt |= PROT_EXEC;
259 }
260#endif
261
262 /*
263 * Align the request.
264 */
265 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
266 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
267
268 /*
269 * Change the page attributes.
270 */
271 int rc = mprotect(pv, cb, fProt);
272 if (!rc)
273 return rc;
274 return RTErrConvertFromErrno(errno);
275}
276
277#endif
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