VirtualBox

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

Last change on this file since 5285 was 4362, checked in by vboxsync, 17 years ago

Solaris

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: alloc-solaris.cpp 4362 2007-08-24 17:20:40Z 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/alloc.h>
23#include <iprt/assert.h>
24#include <iprt/param.h>
25#include <iprt/err.h>
26#include <iprt/string.h>
27
28#include <stdlib.h>
29#include <errno.h>
30#include <sys/mman.h>
31#include <strings.h>
32
33
34#ifdef IN_RING3
35
36/**
37 * Allocates memory which may contain code.
38 *
39 * @returns Pointer to the allocated memory.
40 * @returns NULL on failure.
41 * @param cb Size in bytes of the memory block to allocate.
42 */
43RTDECL(void *) RTMemExecAlloc(size_t cb)
44{
45 /*
46 * Allocate first.
47 */
48 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
49 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
50 void *pv = valloc(cb);
51 AssertMsg(pv, ("posix_memalign(%d) failed!!! errno=%d\n", cb, errno));
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 */
75RTDECL(void) RTMemExecFree(void *pv)
76{
77 if (pv)
78 free(pv);
79}
80
81
82/**
83 * Allocate page aligned memory.
84 *
85 * @returns Pointer to the allocated memory.
86 * @returns NULL if we're out of memory.
87 * @param cb Size of the memory block. Will be rounded up to page size.
88 */
89RTDECL(void *) RTMemPageAlloc(size_t cb)
90{
91 return valloc(RT_ALIGN_Z(cb, PAGE_SIZE));
92}
93
94
95/**
96 * Allocate zero'ed page aligned memory.
97 *
98 * @returns Pointer to the allocated memory.
99 * @returns NULL if we're out of memory.
100 * @param cb Size of the memory block. Will be rounded up to page size.
101 */
102RTDECL(void *) RTMemPageAllocZ(size_t cb)
103{
104 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
105 void *pv = valloc(cb);
106 if (pv)
107 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
108 return pv;
109}
110
111
112/**
113 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
114 *
115 * @param pv Pointer to the block as it was returned by the allocation function.
116 * NULL will be ignored.
117 */
118RTDECL(void) RTMemPageFree(void *pv)
119{
120 if (pv)
121 free(pv);
122}
123
124
125/**
126 * Change the page level protection of a memory region.
127 *
128 * @returns iprt status code.
129 * @param pv Start of the region. Will be rounded down to nearest page boundary.
130 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
131 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
132 */
133RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
134{
135 /*
136 * Validate input.
137 */
138 if (cb == 0)
139 {
140 AssertMsgFailed(("!cb\n"));
141 return VERR_INVALID_PARAMETER;
142 }
143 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
144 {
145 AssertMsgFailed(("fProtect=%#x\n", fProtect));
146 return VERR_INVALID_PARAMETER;
147 }
148
149 /*
150 * Convert the flags.
151 */
152 int fProt;
153#if RTMEM_PROT_NONE == PROT_NONE \
154 && RTMEM_PROT_READ == PROT_READ \
155 && RTMEM_PROT_WRITE == PROT_WRITE \
156 && RTMEM_PROT_EXEC == PROT_EXEC
157 fProt = fProtect;
158#else
159 Assert(!RTMEM_PROT_NONE);
160 if (!fProtect)
161 fProt = PROT_NONE;
162 else
163 {
164 fProt = 0;
165 if (fProtect & RTMEM_PROT_READ)
166 fProt |= PROT_READ;
167 if (fProtect & RTMEM_PROT_WRITE)
168 fProt |= PROT_WRITE;
169 if (fProtect & RTMEM_PROT_EXEC)
170 fProt |= PROT_EXEC;
171 }
172#endif
173
174 /*
175 * Align the request.
176 */
177 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
178 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
179
180 /*
181 * Change the page attributes.
182 */
183 int rc = mprotect(pv, cb, fProt);
184 if (!rc)
185 return rc;
186 return RTErrConvertFromErrno(errno);
187}
188
189#endif
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