VirtualBox

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

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

iprt,++: Tag allocation in all builds with a string, defaulting to FILE.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/* $Id: alloc-freebsd.cpp 31158 2010-07-28 03:24:30Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, FreeBSD.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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
42RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
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 = RTMemPageAlloc(cb);
50 if (pv)
51 {
52 /*
53 * Add PROT_EXEC flag to the page(s).
54 */
55 memset(pv, 0xcc, cb);
56 int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
57 if (rc)
58 {
59 AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
60 free(pv);
61 pv = NULL;
62 }
63 }
64 return pv;
65}
66
67
68RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
69{
70 if (pv)
71 free(pv);
72}
73
74
75RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
76{
77 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
78 void *pv = malloc(cb);
79 AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) == pv,
80 ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
81 NULL);
82 return pv;
83}
84
85
86RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
87{
88 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
89 void *pv = RTMemPageAlloc(cb);
90 if (pv)
91 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
92 return pv;
93}
94
95
96RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
97{
98 if (pv)
99 free(pv);
100}
101
102
103RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
104{
105 /*
106 * Validate input.
107 */
108 if (cb == 0)
109 {
110 AssertMsgFailed(("!cb\n"));
111 return VERR_INVALID_PARAMETER;
112 }
113 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
114 {
115 AssertMsgFailed(("fProtect=%#x\n", fProtect));
116 return VERR_INVALID_PARAMETER;
117 }
118
119 /*
120 * Convert the flags.
121 */
122 int fProt;
123#if RTMEM_PROT_NONE == PROT_NONE \
124 && RTMEM_PROT_READ == PROT_READ \
125 && RTMEM_PROT_WRITE == PROT_WRITE \
126 && RTMEM_PROT_EXEC == PROT_EXEC
127 fProt = fProtect;
128#else
129 Assert(!RTMEM_PROT_NONE);
130 if (!fProtect)
131 fProt = PROT_NONE;
132 else
133 {
134 fProt = 0;
135 if (fProtect & RTMEM_PROT_READ)
136 fProt |= PROT_READ;
137 if (fProtect & RTMEM_PROT_WRITE)
138 fProt |= PROT_WRITE;
139 if (fProtect & RTMEM_PROT_EXEC)
140 fProt |= PROT_EXEC;
141 }
142#endif
143
144 /*
145 * Align the request.
146 */
147 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
148 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
149
150 /*
151 * Change the page attributes.
152 */
153 int rc = mprotect(pv, cb, fProt);
154 if (!rc)
155 return rc;
156 return RTErrConvertFromErrno(errno);
157}
158
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