VirtualBox

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

Last change on this file since 4071 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.8 KB
Line 
1/* $Id: alloc-freebsd.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/**
34 * Allocates memory which may contain code.
35 *
36 * @returns Pointer to the allocated memory.
37 * @returns NULL on failure.
38 * @param cb Size in bytes of the memory block to allocate.
39 */
40RTDECL(void *) RTMemExecAlloc(size_t cb)
41{
42 /*
43 * Allocate first.
44 */
45 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
46 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
47 void *pv = RTMemPageAlloc(cb);
48 if (pv)
49 {
50 /*
51 * Add PROT_EXEC flag to the page(s).
52 */
53 memset(pv, 0xcc, cb);
54 int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
55 if (rc)
56 {
57 AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
58 free(pv);
59 pv = NULL;
60 }
61 }
62 return pv;
63}
64
65
66/**
67 * Free executable/read/write memory allocated by RTMemExecAlloc().
68 *
69 * @param pv Pointer to memory block.
70 */
71RTDECL(void) RTMemExecFree(void *pv)
72{
73 return RTMemPageFree(pv);
74}
75
76
77/**
78 * Allocate page aligned memory.
79 *
80 * @returns Pointer to the allocated memory.
81 * @returns NULL if we're out of memory.
82 * @param cb Size of the memory block. Will be rounded up to page size.
83 */
84RTDECL(void *) RTMemPageAlloc(size_t cb)
85{
86 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
87 void *pv = malloc(cb);
88 AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) != pv,
89 ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
90 NULL);
91 return pv;
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 = RTMemPageAlloc(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}
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