VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/alloc-solaris.cpp@ 27787

Last change on this file since 27787 was 27492, checked in by vboxsync, 15 years ago

superflous IN_RING3

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