1 | /* $Id: alloc-freebsd.cpp 2981 2007-06-01 16:01:28Z 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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/alloc.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/param.h>
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 |
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <sys/mman.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Allocates memory which may contain code.
|
---|
39 | *
|
---|
40 | * @returns Pointer to the allocated memory.
|
---|
41 | * @returns NULL on failure.
|
---|
42 | * @param cb Size in bytes of the memory block to allocate.
|
---|
43 | */
|
---|
44 | RTDECL(void *) RTMemExecAlloc(size_t cb)
|
---|
45 | {
|
---|
46 | /*
|
---|
47 | * Allocate first.
|
---|
48 | */
|
---|
49 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
50 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
51 | void *pv = RTMemPageAlloc(cb);
|
---|
52 | if (pv)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Add PROT_EXEC flag to the page(s).
|
---|
56 | */
|
---|
57 | memset(pv, 0xcc, cb);
|
---|
58 | int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
|
---|
59 | if (rc)
|
---|
60 | {
|
---|
61 | AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
|
---|
62 | free(pv);
|
---|
63 | pv = NULL;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return pv;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
72 | *
|
---|
73 | * @param pv Pointer to memory block.
|
---|
74 | */
|
---|
75 | RTDECL(void) RTMemExecFree(void *pv)
|
---|
76 | {
|
---|
77 | return RTMemPageFree(pv);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Allocate page aligned memory.
|
---|
83 | *
|
---|
84 | * @returns Pointer to the allocated memory.
|
---|
85 | * @returns NULL if we're out of memory.
|
---|
86 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
87 | */
|
---|
88 | RTDECL(void *) RTMemPageAlloc(size_t cb)
|
---|
89 | {
|
---|
90 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
91 | void *pv = malloc(cb);
|
---|
92 | AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) != pv,
|
---|
93 | ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
|
---|
94 | NULL);
|
---|
95 | return pv;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Allocate zero'ed page aligned memory.
|
---|
101 | *
|
---|
102 | * @returns Pointer to the allocated memory.
|
---|
103 | * @returns NULL if we're out of memory.
|
---|
104 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
105 | */
|
---|
106 | RTDECL(void *) RTMemPageAllocZ(size_t cb)
|
---|
107 | {
|
---|
108 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
109 | void *pv = RTMemPageAlloc(cb);
|
---|
110 | if (pv)
|
---|
111 | bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
112 | return pv;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
118 | *
|
---|
119 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
120 | * NULL will be ignored.
|
---|
121 | */
|
---|
122 | RTDECL(void) RTMemPageFree(void *pv)
|
---|
123 | {
|
---|
124 | if (pv)
|
---|
125 | free(pv);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Change the page level protection of a memory region.
|
---|
131 | *
|
---|
132 | * @returns iprt status code.
|
---|
133 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
134 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
135 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
136 | */
|
---|
137 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
|
---|
138 | {
|
---|
139 | /*
|
---|
140 | * Validate input.
|
---|
141 | */
|
---|
142 | if (cb == 0)
|
---|
143 | {
|
---|
144 | AssertMsgFailed(("!cb\n"));
|
---|
145 | return VERR_INVALID_PARAMETER;
|
---|
146 | }
|
---|
147 | if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
|
---|
148 | {
|
---|
149 | AssertMsgFailed(("fProtect=%#x\n", fProtect));
|
---|
150 | return VERR_INVALID_PARAMETER;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Convert the flags.
|
---|
155 | */
|
---|
156 | int fProt;
|
---|
157 | #if RTMEM_PROT_NONE == PROT_NONE \
|
---|
158 | && RTMEM_PROT_READ == PROT_READ \
|
---|
159 | && RTMEM_PROT_WRITE == PROT_WRITE \
|
---|
160 | && RTMEM_PROT_EXEC == PROT_EXEC
|
---|
161 | fProt = fProtect;
|
---|
162 | #else
|
---|
163 | Assert(!RTMEM_PROT_NONE);
|
---|
164 | if (!fProtect)
|
---|
165 | fProt = PROT_NONE;
|
---|
166 | else
|
---|
167 | {
|
---|
168 | fProt = 0;
|
---|
169 | if (fProtect & RTMEM_PROT_READ)
|
---|
170 | fProt |= PROT_READ;
|
---|
171 | if (fProtect & RTMEM_PROT_WRITE)
|
---|
172 | fProt |= PROT_WRITE;
|
---|
173 | if (fProtect & RTMEM_PROT_EXEC)
|
---|
174 | fProt |= PROT_EXEC;
|
---|
175 | }
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * Align the request.
|
---|
180 | */
|
---|
181 | cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
|
---|
182 | pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Change the page attributes.
|
---|
186 | */
|
---|
187 | int rc = mprotect(pv, cb, fProt);
|
---|
188 | if (!rc)
|
---|
189 | return rc;
|
---|
190 | return RTErrConvertFromErrno(errno);
|
---|
191 | }
|
---|