VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/alloc-solaris.cpp@ 31158

Last change on this file since 31158 was 31158, checked in by vboxsync, 14 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: 5.2 KB
Line 
1/* $Id: alloc-solaris.cpp 31158 2010-07-28 03:24:30Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Solaris.
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#include <strings.h>
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46#if 0
47# define RT_USE_MMAP_PAGE
48#endif
49
50
51RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
52{
53 /*
54 * Allocate first.
55 */
56 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
57 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
58 void *pv = valloc(cb);
59 AssertMsg(pv, ("posix_memalign(%d) failed!!! errno=%d\n", cb, errno));
60 if (RT_UNLIKELY((uintptr_t)pv + cb > _2G))
61 {
62 AssertMsgFailed(("%p %#zx\n", pv, cb));
63 free(pv);
64 return NULL;
65 }
66 if (pv)
67 {
68 /*
69 * Add PROT_EXEC flag to the page(s).
70 */
71 memset(pv, 0xcc, cb);
72 int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
73 if (rc)
74 {
75 AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
76 free(pv);
77 pv = NULL;
78 }
79 }
80 return pv;
81}
82
83
84RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
85{
86 if (pv)
87 free(pv);
88}
89
90
91RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
92{
93#ifdef RT_USE_MMAP_PAGE
94 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
95 void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
97 return pv;
98
99#else
100 return valloc(RT_ALIGN_Z(cb, PAGE_SIZE));
101#endif
102}
103
104
105RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
106{
107#ifdef RT_USE_MMAP_PAGE
108 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
109 void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
110 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
111 return pv;
112
113#else
114 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
115 void *pv = valloc(cb);
116 if (pv)
117 bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
118 return pv;
119#endif
120}
121
122
123RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
124{
125 if (pv)
126 {
127#ifdef RT_USE_MMAP_PAGE
128 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
129 int rc = munmap(pv, cbAligned);
130 AssertMsg(!rc, ("munmap(%p, %#zx) -> %d errno=%d\n", pv, cbAligned, rc, errno)); NOREF(rc);
131#else
132 free(pv);
133#endif
134 }
135}
136
137
138RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
139{
140 /*
141 * Validate input.
142 */
143 if (cb == 0)
144 {
145 AssertMsgFailed(("!cb\n"));
146 return VERR_INVALID_PARAMETER;
147 }
148 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
149 {
150 AssertMsgFailed(("fProtect=%#x\n", fProtect));
151 return VERR_INVALID_PARAMETER;
152 }
153
154 /*
155 * Convert the flags.
156 */
157 int fProt;
158#if RTMEM_PROT_NONE == PROT_NONE \
159 && RTMEM_PROT_READ == PROT_READ \
160 && RTMEM_PROT_WRITE == PROT_WRITE \
161 && RTMEM_PROT_EXEC == PROT_EXEC
162 fProt = fProtect;
163#else
164 Assert(!RTMEM_PROT_NONE);
165 if (!fProtect)
166 fProt = PROT_NONE;
167 else
168 {
169 fProt = 0;
170 if (fProtect & RTMEM_PROT_READ)
171 fProt |= PROT_READ;
172 if (fProtect & RTMEM_PROT_WRITE)
173 fProt |= PROT_WRITE;
174 if (fProtect & RTMEM_PROT_EXEC)
175 fProt |= PROT_EXEC;
176 }
177#endif
178
179 /*
180 * Align the request.
181 */
182 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
183 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
184
185 /*
186 * Change the page attributes.
187 */
188 int rc = mprotect(pv, cb, fProt);
189 if (!rc)
190 return rc;
191 return RTErrConvertFromErrno(errno);
192}
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