VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/freebsd/alloc-freebsd.cpp@ 5999

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

The Giant CDDL Dual-License Header Change.

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