1 | /* $Id: alloc-darwin.cpp 31157 2010-07-28 03:15:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, POSIX.
|
---|
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 | #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 | RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
43 | {
|
---|
44 | /*
|
---|
45 | * Allocate first.
|
---|
46 | */
|
---|
47 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
48 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
49 | void *pv = valloc(cb);
|
---|
50 | AssertMsg(pv, ("posix_memalign(%d) failed!!! errno=%d\n", cb, errno));
|
---|
51 | if (pv)
|
---|
52 | {
|
---|
53 | /*
|
---|
54 | * Add PROT_EXEC flag to the page(s).
|
---|
55 | */
|
---|
56 | memset(pv, 0xcc, cb);
|
---|
57 | int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
|
---|
58 | if (rc)
|
---|
59 | {
|
---|
60 | AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
|
---|
61 | free(pv);
|
---|
62 | pv = NULL;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return pv;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
|
---|
70 | {
|
---|
71 | if (pv)
|
---|
72 | free(pv);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
77 | {
|
---|
78 | return valloc(RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
83 | {
|
---|
84 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
85 | void *pv = valloc(cb);
|
---|
86 | if (pv)
|
---|
87 | bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
88 | return pv;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
|
---|
93 | {
|
---|
94 | if (pv)
|
---|
95 | free(pv);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
|
---|
100 | {
|
---|
101 | /*
|
---|
102 | * Validate input.
|
---|
103 | */
|
---|
104 | if (cb == 0)
|
---|
105 | {
|
---|
106 | AssertMsgFailed(("!cb\n"));
|
---|
107 | return VERR_INVALID_PARAMETER;
|
---|
108 | }
|
---|
109 | if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
|
---|
110 | {
|
---|
111 | AssertMsgFailed(("fProtect=%#x\n", fProtect));
|
---|
112 | return VERR_INVALID_PARAMETER;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Convert the flags.
|
---|
117 | */
|
---|
118 | int fProt;
|
---|
119 | #if RTMEM_PROT_NONE == PROT_NONE \
|
---|
120 | && RTMEM_PROT_READ == PROT_READ \
|
---|
121 | && RTMEM_PROT_WRITE == PROT_WRITE \
|
---|
122 | && RTMEM_PROT_EXEC == PROT_EXEC
|
---|
123 | fProt = fProtect;
|
---|
124 | #else
|
---|
125 | Assert(!RTMEM_PROT_NONE);
|
---|
126 | if (!fProtect)
|
---|
127 | fProt = PROT_NONE;
|
---|
128 | else
|
---|
129 | {
|
---|
130 | fProt = 0;
|
---|
131 | if (fProtect & RTMEM_PROT_READ)
|
---|
132 | fProt |= PROT_READ;
|
---|
133 | if (fProtect & RTMEM_PROT_WRITE)
|
---|
134 | fProt |= PROT_WRITE;
|
---|
135 | if (fProtect & RTMEM_PROT_EXEC)
|
---|
136 | fProt |= PROT_EXEC;
|
---|
137 | }
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Align the request.
|
---|
142 | */
|
---|
143 | cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
|
---|
144 | pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Change the page attributes.
|
---|
148 | */
|
---|
149 | int rc = mprotect(pv, cb, fProt);
|
---|
150 | if (!rc)
|
---|
151 | return rc;
|
---|
152 | return RTErrConvertFromErrno(errno);
|
---|
153 | }
|
---|