VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/allocex-r3-posix.cpp@ 103795

Last change on this file since 103795 was 100191, checked in by vboxsync, 18 months ago

*: Some of the easy build fixes for linux.arm64 not warranting their own commit, bugref:10457

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* $Id: allocex-r3-posix.cpp 100191 2023-06-16 08:04:11Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Extended Alloc Workers, posix.
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#define RTMEM_NO_WRAP_TO_EF_APIS
42#include <iprt/mem.h>
43#include "internal/iprt.h"
44
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include <iprt/string.h>
48#include "../allocex.h"
49
50#include <sys/mman.h>
51
52
53DECLHIDDEN(int) rtMemAllocEx16BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
54{
55 AssertReturn(cbAlloc < _64K, VERR_NO_MEMORY);
56
57 /*
58 * Try with every possible address hint since the possible range is very limited.
59 */
60 int fProt = PROT_READ | PROT_WRITE | (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PROT_EXEC : 0);
61 uintptr_t uAddr = 0x1000;
62 uintptr_t uAddrLast = _64K - uAddr - cbAlloc;
63 while (uAddr <= uAddrLast)
64 {
65 void *pv = mmap((void *)uAddr, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
66 if (pv && (uintptr_t)pv <= uAddrLast)
67 {
68 *ppv = pv;
69 return VINF_SUCCESS;
70 }
71
72 if (pv)
73 {
74 munmap(pv, cbAlloc);
75 pv = NULL;
76 }
77 uAddr += _4K;
78 }
79
80 return VERR_NO_MEMORY;
81}
82
83
84DECLHIDDEN(int) rtMemAllocEx32BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
85{
86 int fProt = PROT_READ | PROT_WRITE | (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PROT_EXEC : 0);
87#if ARCH_BITS == 32
88 void *pv = mmap(NULL, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
89 if (pv)
90 {
91 *ppv = pv;
92 return VINF_SUCCESS;
93 }
94 return VERR_NO_MEMORY;
95
96#elif defined(RT_OS_LINUX)
97# ifdef MAP_32BIT
98 void *pv = mmap(NULL, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
99 if (pv)
100 {
101 *ppv = pv;
102 return VINF_SUCCESS;
103 }
104# else
105 RT_NOREF(fProt, cbAlloc, ppv);
106# endif
107
108 /** @todo On linux, we need an accurate hint. Since I don't need this branch of
109 * the code right now, I won't bother starting to parse
110 * /proc/curproc/mmap right now... */
111#else
112 RT_NOREF(fProt, cbAlloc, ppv);
113#endif
114 return VERR_NOT_SUPPORTED;
115}
116
117
118DECLHIDDEN(void) rtMemFreeExYyBitReach(void *pv, size_t cb, uint32_t fFlags)
119{
120 RT_NOREF_PV(fFlags);
121 munmap(pv, cb);
122}
123
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