VirtualBox

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

最後變更 在這個檔案從30976是 29277,由 vboxsync 提交於 15 年 前

alloc-solaris.cpp: MMAP mode in case it comes in handy later.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 6.4 KB
 
1/* $Id: alloc-solaris.cpp 29277 2010-05-09 23:25:51Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#include <strings.h>
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46#if 0
47# define RT_USE_MMAP_PAGE
48#endif
49
50
51/**
52 * Allocates memory which may contain code.
53 *
54 * @returns Pointer to the allocated memory.
55 * @returns NULL on failure.
56 * @param cb Size in bytes of the memory block to allocate.
57 */
58RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
59{
60 /*
61 * Allocate first.
62 */
63 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
64 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
65 void *pv = valloc(cb);
66 AssertMsg(pv, ("posix_memalign(%d) failed!!! errno=%d\n", cb, errno));
67 if (RT_UNLIKELY((uintptr_t)pv + cb > _2G))
68 {
69 AssertMsgFailed(("%p %#zx\n", pv, cb));
70 free(pv);
71 return NULL;
72 }
73 if (pv)
74 {
75 /*
76 * Add PROT_EXEC flag to the page(s).
77 */
78 memset(pv, 0xcc, cb);
79 int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
80 if (rc)
81 {
82 AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
83 free(pv);
84 pv = NULL;
85 }
86 }
87 return pv;
88}
89
90
91/**
92 * Free executable/read/write memory allocated by RTMemExecAlloc().
93 *
94 * @param pv Pointer to memory block.
95 */
96RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
97{
98 if (pv)
99 free(pv);
100}
101
102
103/**
104 * Allocate 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 *) RTMemPageAlloc(size_t cb) RT_NO_THROW
111{
112#ifdef RT_USE_MMAP_PAGE
113 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
114 void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
115 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
116 return pv;
117
118#else
119 return valloc(RT_ALIGN_Z(cb, PAGE_SIZE));
120#endif
121}
122
123
124/**
125 * Allocate zero'ed page aligned memory.
126 *
127 * @returns Pointer to the allocated memory.
128 * @returns NULL if we're out of memory.
129 * @param cb Size of the memory block. Will be rounded up to page size.
130 */
131RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
132{
133#ifdef RT_USE_MMAP_PAGE
134 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
135 void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
136 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
137 return pv;
138
139#else
140 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
141 void *pv = valloc(cb);
142 if (pv)
143 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
144 return pv;
145#endif
146}
147
148
149/**
150 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
151 *
152 * @param pv Pointer to the block as it was returned by the allocation function.
153 * NULL will be ignored.
154 */
155RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
156{
157 if (pv)
158 {
159#ifdef RT_USE_MMAP_PAGE
160 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
161 int rc = munmap(pv, cbAligned);
162 AssertMsg(!rc, ("munmap(%p, %#zx) -> %d errno=%d\n", pv, cbAligned, rc, errno)); NOREF(rc);
163#else
164 free(pv);
165#endif
166 }
167}
168
169
170/**
171 * Change the page level protection of a memory region.
172 *
173 * @returns iprt status code.
174 * @param pv Start of the region. Will be rounded down to nearest page boundary.
175 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
176 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
177 */
178RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
179{
180 /*
181 * Validate input.
182 */
183 if (cb == 0)
184 {
185 AssertMsgFailed(("!cb\n"));
186 return VERR_INVALID_PARAMETER;
187 }
188 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
189 {
190 AssertMsgFailed(("fProtect=%#x\n", fProtect));
191 return VERR_INVALID_PARAMETER;
192 }
193
194 /*
195 * Convert the flags.
196 */
197 int fProt;
198#if RTMEM_PROT_NONE == PROT_NONE \
199 && RTMEM_PROT_READ == PROT_READ \
200 && RTMEM_PROT_WRITE == PROT_WRITE \
201 && RTMEM_PROT_EXEC == PROT_EXEC
202 fProt = fProtect;
203#else
204 Assert(!RTMEM_PROT_NONE);
205 if (!fProtect)
206 fProt = PROT_NONE;
207 else
208 {
209 fProt = 0;
210 if (fProtect & RTMEM_PROT_READ)
211 fProt |= PROT_READ;
212 if (fProtect & RTMEM_PROT_WRITE)
213 fProt |= PROT_WRITE;
214 if (fProtect & RTMEM_PROT_EXEC)
215 fProt |= PROT_EXEC;
216 }
217#endif
218
219 /*
220 * Align the request.
221 */
222 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
223 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
224
225 /*
226 * Change the page attributes.
227 */
228 int rc = mprotect(pv, cb, fProt);
229 if (!rc)
230 return rc;
231 return RTErrConvertFromErrno(errno);
232}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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