VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: alloc-darwin.cpp 4071 2007-08-07 17:07:59Z 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
32
33#ifdef IN_RING3
34
35/**
36 * Allocates memory which may contain code.
37 *
38 * @returns Pointer to the allocated memory.
39 * @returns NULL on failure.
40 * @param cb Size in bytes of the memory block to allocate.
41 */
42RTDECL(void *) RTMemExecAlloc(size_t cb)
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/**
70 * Free executable/read/write memory allocated by RTMemExecAlloc().
71 *
72 * @param pv Pointer to memory block.
73 */
74RTDECL(void) RTMemExecFree(void *pv)
75{
76 if (pv)
77 free(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 */
88RTDECL(void *) RTMemPageAlloc(size_t cb)
89{
90 return valloc(RT_ALIGN_Z(cb, PAGE_SIZE));
91}
92
93
94/**
95 * Allocate zero'ed page aligned memory.
96 *
97 * @returns Pointer to the allocated memory.
98 * @returns NULL if we're out of memory.
99 * @param cb Size of the memory block. Will be rounded up to page size.
100 */
101RTDECL(void *) RTMemPageAllocZ(size_t cb)
102{
103 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
104 void *pv = valloc(cb);
105 if (pv)
106 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
107 return pv;
108}
109
110
111/**
112 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
113 *
114 * @param pv Pointer to the block as it was returned by the allocation function.
115 * NULL will be ignored.
116 */
117RTDECL(void) RTMemPageFree(void *pv)
118{
119 if (pv)
120 free(pv);
121}
122
123
124/**
125 * Change the page level protection of a memory region.
126 *
127 * @returns iprt status code.
128 * @param pv Start of the region. Will be rounded down to nearest page boundary.
129 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
130 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
131 */
132RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
133{
134 /*
135 * Validate input.
136 */
137 if (cb == 0)
138 {
139 AssertMsgFailed(("!cb\n"));
140 return VERR_INVALID_PARAMETER;
141 }
142 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
143 {
144 AssertMsgFailed(("fProtect=%#x\n", fProtect));
145 return VERR_INVALID_PARAMETER;
146 }
147
148 /*
149 * Convert the flags.
150 */
151 int fProt;
152#if RTMEM_PROT_NONE == PROT_NONE \
153 && RTMEM_PROT_READ == PROT_READ \
154 && RTMEM_PROT_WRITE == PROT_WRITE \
155 && RTMEM_PROT_EXEC == PROT_EXEC
156 fProt = fProtect;
157#else
158 Assert(!RTMEM_PROT_NONE);
159 if (!fProtect)
160 fProt = PROT_NONE;
161 else
162 {
163 fProt = 0;
164 if (fProtect & RTMEM_PROT_READ)
165 fProt |= PROT_READ;
166 if (fProtect & RTMEM_PROT_WRITE)
167 fProt |= PROT_WRITE;
168 if (fProtect & RTMEM_PROT_EXEC)
169 fProt |= PROT_EXEC;
170 }
171#endif
172
173 /*
174 * Align the request.
175 */
176 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
177 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
178
179 /*
180 * Change the page attributes.
181 */
182 int rc = mprotect(pv, cb, fProt);
183 if (!rc)
184 return rc;
185 return RTErrConvertFromErrno(errno);
186}
187
188#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