VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-posix.cpp

Last change on this file was 101142, checked in by vboxsync, 8 months ago

IPRT/mem: Added a RTMEMPAGEALLOC_F_EXECUTABLE to RTMemPageAllocEx. Untested on windows. bugref:10370

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: rtmempage-exec-mmap-posix.cpp 101142 2023-09-18 11:12:16Z vboxsync $ */
2/** @file
3 * IPRT - RTMemPage*, POSIX with mmap only.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/mem.h>
43
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include <iprt/param.h>
48#include <iprt/string.h>
49#include <iprt/system.h>
50
51#include <stdlib.h>
52#include <errno.h>
53#include <sys/mman.h>
54#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
55# define MAP_ANONYMOUS MAP_ANON
56#endif
57
58
59/**
60 * Applies flags to an allocation.
61 *
62 * @param pv The allocation.
63 * @param cb The size of the allocation (page aligned).
64 * @param fFlags RTMEMPAGEALLOC_F_XXX.
65 */
66DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags)
67{
68#ifndef RT_OS_OS2
69 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
70 {
71 int rc = mlock(pv, cb);
72# ifndef RT_OS_SOLARIS /* mlock(3C) on Solaris requires the priv_lock_memory privilege */
73 AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
74# endif
75 NOREF(rc);
76 }
77
78# ifdef MADV_DONTDUMP
79 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
80 {
81 int rc = madvise(pv, cb, MADV_DONTDUMP);
82 AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno));
83 NOREF(rc);
84 }
85# endif
86#endif
87
88 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
89 RT_BZERO(pv, cb);
90}
91
92
93/**
94 * Allocates memory from the specified heap.
95 *
96 * @returns Address of the allocated memory.
97 * @param cb The number of bytes to allocate.
98 * @param pszTag The tag.
99 * @param fFlags RTMEMPAGEALLOC_F_XXX.
100 * @param fProtExec PROT_EXEC or 0.
101 */
102static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, int fProtExec)
103{
104 /*
105 * Validate & adjust the input.
106 */
107 Assert(cb > 0);
108 NOREF(pszTag);
109 cb = RTSystemPageAlignSize(cb);
110
111 /*
112 * Do the allocation.
113 */
114 void *pv = mmap(NULL, cb,
115 PROT_READ | PROT_WRITE | fProtExec,
116 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
117 if (pv != MAP_FAILED)
118 {
119 AssertPtr(pv);
120
121 if (fFlags)
122 rtMemPagePosixApplyFlags(pv, cb, fFlags);
123 }
124 else
125 pv = NULL;
126
127 return pv;
128}
129
130
131/**
132 * Free memory allocated by rtMemPagePosixAlloc.
133 *
134 * @param pv The address of the memory to free.
135 * @param cb The size.
136 */
137static void rtMemPagePosixFree(void *pv, size_t cb)
138{
139 /*
140 * Validate & adjust the input.
141 */
142 if (!pv)
143 return;
144 AssertPtr(pv);
145 Assert(cb > 0);
146 Assert(!((uintptr_t)pv & RTSystemGetPageOffsetMask()));
147 cb = RTSystemPageAlignSize(cb);
148
149 /*
150 * Free the memory.
151 */
152 int rc = munmap(pv, cb);
153 AssertMsg(rc == 0, ("rc=%d pv=%p cb=%#zx\n", rc, pv, cb)); NOREF(rc);
154}
155
156
157
158
159
160RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
161{
162 return rtMemPagePosixAlloc(cb, pszTag, 0, 0);
163}
164
165
166RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
167{
168 return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, 0);
169}
170
171
172RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
173{
174 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
175 return rtMemPagePosixAlloc(cb, pszTag, fFlags, !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? 0 : PROT_EXEC);
176}
177
178
179RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
180{
181 return rtMemPagePosixFree(pv, cb);
182}
183
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use