VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/alloc-darwin.cpp@ 8141

Last change on this file since 8141 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/* $Id: alloc-darwin.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Memory Allocation, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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#include <iprt/alloc.h>
32#include <iprt/assert.h>
33#include <iprt/param.h>
34#include <iprt/err.h>
35#include <iprt/string.h>
36
37#include <stdlib.h>
38#include <errno.h>
39#include <sys/mman.h>
40
41
42#ifdef IN_RING3
43
44/**
45 * Allocates memory which may contain code.
46 *
47 * @returns Pointer to the allocated memory.
48 * @returns NULL on failure.
49 * @param cb Size in bytes of the memory block to allocate.
50 */
51RTDECL(void *) RTMemExecAlloc(size_t cb)
52{
53 /*
54 * Allocate first.
55 */
56 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
57 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
58 void *pv = valloc(cb);
59 AssertMsg(pv, ("posix_memalign(%d) failed!!! errno=%d\n", cb, errno));
60 if (pv)
61 {
62 /*
63 * Add PROT_EXEC flag to the page(s).
64 */
65 memset(pv, 0xcc, cb);
66 int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
67 if (rc)
68 {
69 AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
70 free(pv);
71 pv = NULL;
72 }
73 }
74 return pv;
75}
76
77
78/**
79 * Free executable/read/write memory allocated by RTMemExecAlloc().
80 *
81 * @param pv Pointer to memory block.
82 */
83RTDECL(void) RTMemExecFree(void *pv)
84{
85 if (pv)
86 free(pv);
87}
88
89
90/**
91 * Allocate page aligned memory.
92 *
93 * @returns Pointer to the allocated memory.
94 * @returns NULL if we're out of memory.
95 * @param cb Size of the memory block. Will be rounded up to page size.
96 */
97RTDECL(void *) RTMemPageAlloc(size_t cb)
98{
99 return valloc(RT_ALIGN_Z(cb, PAGE_SIZE));
100}
101
102
103/**
104 * Allocate zero'ed page aligned memory.
105 *
106 * @returns Pointer to the allocated memory.
107 * @returns NULL if we're out of memory.
108 * @param cb Size of the memory block. Will be rounded up to page size.
109 */
110RTDECL(void *) RTMemPageAllocZ(size_t cb)
111{
112 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
113 void *pv = valloc(cb);
114 if (pv)
115 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
116 return pv;
117}
118
119
120/**
121 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
122 *
123 * @param pv Pointer to the block as it was returned by the allocation function.
124 * NULL will be ignored.
125 */
126RTDECL(void) RTMemPageFree(void *pv)
127{
128 if (pv)
129 free(pv);
130}
131
132
133/**
134 * Change the page level protection of a memory region.
135 *
136 * @returns iprt status code.
137 * @param pv Start of the region. Will be rounded down to nearest page boundary.
138 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
139 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
140 */
141RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
142{
143 /*
144 * Validate input.
145 */
146 if (cb == 0)
147 {
148 AssertMsgFailed(("!cb\n"));
149 return VERR_INVALID_PARAMETER;
150 }
151 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
152 {
153 AssertMsgFailed(("fProtect=%#x\n", fProtect));
154 return VERR_INVALID_PARAMETER;
155 }
156
157 /*
158 * Convert the flags.
159 */
160 int fProt;
161#if RTMEM_PROT_NONE == PROT_NONE \
162 && RTMEM_PROT_READ == PROT_READ \
163 && RTMEM_PROT_WRITE == PROT_WRITE \
164 && RTMEM_PROT_EXEC == PROT_EXEC
165 fProt = fProtect;
166#else
167 Assert(!RTMEM_PROT_NONE);
168 if (!fProtect)
169 fProt = PROT_NONE;
170 else
171 {
172 fProt = 0;
173 if (fProtect & RTMEM_PROT_READ)
174 fProt |= PROT_READ;
175 if (fProtect & RTMEM_PROT_WRITE)
176 fProt |= PROT_WRITE;
177 if (fProtect & RTMEM_PROT_EXEC)
178 fProt |= PROT_EXEC;
179 }
180#endif
181
182 /*
183 * Align the request.
184 */
185 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
186 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
187
188 /*
189 * Change the page attributes.
190 */
191 int rc = mprotect(pv, cb, fProt);
192 if (!rc)
193 return rc;
194 return RTErrConvertFromErrno(errno);
195}
196
197#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette