1 | /* $Id: allocex-r3-posix.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Extended Alloc Workers, posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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 | #define RTMEM_NO_WRAP_TO_EF_APIS
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include "../allocex.h"
|
---|
39 |
|
---|
40 | #include <sys/mman.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | DECLHIDDEN(int) rtMemAllocEx16BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
|
---|
44 | {
|
---|
45 | AssertReturn(cbAlloc < _64K, VERR_NO_MEMORY);
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Try with every possible address hint since the possible range is very limited.
|
---|
49 | */
|
---|
50 | int fProt = PROT_READ | PROT_WRITE | (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PROT_EXEC : 0);
|
---|
51 | uintptr_t uAddr = 0x1000;
|
---|
52 | uintptr_t uAddrLast = _64K - uAddr - cbAlloc;
|
---|
53 | while (uAddr <= uAddrLast)
|
---|
54 | {
|
---|
55 | void *pv = mmap((void *)uAddr, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
56 | if (pv && (uintptr_t)pv <= uAddrLast)
|
---|
57 | {
|
---|
58 | *ppv = pv;
|
---|
59 | return VINF_SUCCESS;
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (pv)
|
---|
63 | {
|
---|
64 | munmap(pv, cbAlloc);
|
---|
65 | pv = NULL;
|
---|
66 | }
|
---|
67 | uAddr += _4K;
|
---|
68 | }
|
---|
69 |
|
---|
70 | return VERR_NO_MEMORY;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | DECLHIDDEN(int) rtMemAllocEx32BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
|
---|
75 | {
|
---|
76 | int fProt = PROT_READ | PROT_WRITE | (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PROT_EXEC : 0);
|
---|
77 | #if ARCH_BITS == 32
|
---|
78 | void *pv = mmap(NULL, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
79 | if (pv)
|
---|
80 | {
|
---|
81 | *ppv = pv;
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | }
|
---|
84 | return VERR_NO_MEMORY;
|
---|
85 |
|
---|
86 | #elif defined(RT_OS_LINUX)
|
---|
87 | # ifdef MAP_32BIT
|
---|
88 | void *pv = mmap(NULL, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
|
---|
89 | if (pv)
|
---|
90 | {
|
---|
91 | *ppv = pv;
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 | # endif
|
---|
95 |
|
---|
96 | /** @todo On linux, we need an accurate hint. Since I don't need this branch of
|
---|
97 | * the code right now, I won't bother starting to parse
|
---|
98 | * /proc/curproc/mmap right now... */
|
---|
99 | #else
|
---|
100 | #endif
|
---|
101 | return VERR_NOT_SUPPORTED;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | DECLHIDDEN(void) rtMemFreeExYyBitReach(void *pv, size_t cb, uint32_t fFlags)
|
---|
106 | {
|
---|
107 | munmap(pv, cb);
|
---|
108 | }
|
---|
109 |
|
---|