VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1/* $Id: alloc-freebsd.cpp 28800 2010-04-27 08:22:32Z 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.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) RT_NO_THROW
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) RT_NO_THROW
81{
82 if (pv)
83 free(pv);
84}
85
86
87/**
88 * Allocate page aligned memory.
89 *
90 * @returns Pointer to the allocated memory.
91 * @returns NULL if we're out of memory.
92 * @param cb Size of the memory block. Will be rounded up to page size.
93 */
94RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW
95{
96 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
97 void *pv = malloc(cb);
98 AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) == pv,
99 ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
100 NULL);
101 return pv;
102}
103
104
105/**
106 * Allocate zero'ed page aligned memory.
107 *
108 * @returns Pointer to the allocated memory.
109 * @returns NULL if we're out of memory.
110 * @param cb Size of the memory block. Will be rounded up to page size.
111 */
112RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
113{
114 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
115 void *pv = RTMemPageAlloc(cb);
116 if (pv)
117 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
118 return pv;
119}
120
121
122/**
123 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
124 *
125 * @param pv Pointer to the block as it was returned by the allocation function.
126 * NULL will be ignored.
127 */
128RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
129{
130 if (pv)
131 free(pv);
132}
133
134
135/**
136 * Change the page level protection of a memory region.
137 *
138 * @returns iprt status code.
139 * @param pv Start of the region. Will be rounded down to nearest page boundary.
140 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
141 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
142 */
143RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
144{
145 /*
146 * Validate input.
147 */
148 if (cb == 0)
149 {
150 AssertMsgFailed(("!cb\n"));
151 return VERR_INVALID_PARAMETER;
152 }
153 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
154 {
155 AssertMsgFailed(("fProtect=%#x\n", fProtect));
156 return VERR_INVALID_PARAMETER;
157 }
158
159 /*
160 * Convert the flags.
161 */
162 int fProt;
163#if RTMEM_PROT_NONE == PROT_NONE \
164 && RTMEM_PROT_READ == PROT_READ \
165 && RTMEM_PROT_WRITE == PROT_WRITE \
166 && RTMEM_PROT_EXEC == PROT_EXEC
167 fProt = fProtect;
168#else
169 Assert(!RTMEM_PROT_NONE);
170 if (!fProtect)
171 fProt = PROT_NONE;
172 else
173 {
174 fProt = 0;
175 if (fProtect & RTMEM_PROT_READ)
176 fProt |= PROT_READ;
177 if (fProtect & RTMEM_PROT_WRITE)
178 fProt |= PROT_WRITE;
179 if (fProtect & RTMEM_PROT_EXEC)
180 fProt |= PROT_EXEC;
181 }
182#endif
183
184 /*
185 * Align the request.
186 */
187 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
188 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
189
190 /*
191 * Change the page attributes.
192 */
193 int rc = mprotect(pv, cb, fProt);
194 if (!rc)
195 return rc;
196 return RTErrConvertFromErrno(errno);
197}
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