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