[46567] | 1 | /* $Id: allocex.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[46570] | 3 | * IPRT - Memory Allocation, Extended Alloc and Free Functions for Ring-3, posix.
|
---|
[46567] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[46567] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[46567] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[46567] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[46567] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[46567] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[46567] | 41 | #define RTMEM_NO_WRAP_TO_EF_APIS
|
---|
| 42 | #include <iprt/mem.h>
|
---|
| 43 | #include "internal/iprt.h"
|
---|
| 44 |
|
---|
| 45 | #include <iprt/assert.h>
|
---|
[76346] | 46 | #include <iprt/err.h>
|
---|
[46567] | 47 | #include <iprt/string.h>
|
---|
| 48 | #include "internal/magics.h"
|
---|
[46739] | 49 | #include "allocex.h"
|
---|
[46567] | 50 |
|
---|
| 51 |
|
---|
[57432] | 52 | RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW_DEF
|
---|
[46567] | 53 | {
|
---|
[62564] | 54 | RT_NOREF_PV(pszTag);
|
---|
| 55 |
|
---|
[46567] | 56 | /*
|
---|
| 57 | * Validate and adjust input.
|
---|
| 58 | */
|
---|
| 59 | AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
| 60 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
| 61 | AssertReturn(RT_IS_POWER_OF_TWO(cbAlignment), VERR_INVALID_PARAMETER);
|
---|
| 62 | AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
|
---|
| 63 |
|
---|
[46570] | 64 | if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX)
|
---|
[46567] | 65 | return VERR_NOT_SUPPORTED;
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
| 68 | * Align the request.
|
---|
| 69 | */
|
---|
| 70 | size_t cbAligned = cb;
|
---|
| 71 | if (cbAlignment)
|
---|
| 72 | cbAligned = RT_ALIGN_Z(cb, cbAlignment);
|
---|
| 73 | else
|
---|
| 74 | cbAligned = RT_ALIGN_Z(cb, sizeof(uint64_t));
|
---|
[46570] | 75 | AssertMsgReturn(cbAligned >= cb && cbAligned <= ~(size_t)0, ("cbAligned=%#zx cb=%#zx", cbAligned, cb),
|
---|
[46567] | 76 | VERR_INVALID_PARAMETER);
|
---|
| 77 |
|
---|
| 78 | /*
|
---|
| 79 | * Allocate the requested memory.
|
---|
| 80 | */
|
---|
| 81 | void *pv;
|
---|
[46570] | 82 | if (fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
|
---|
[46739] | 83 | {
|
---|
| 84 | int rc;
|
---|
| 85 | if (fFlags & RTMEMALLOCEX_FLAGS_16BIT_REACH)
|
---|
| 86 | rc = rtMemAllocEx16BitReach(cbAligned + sizeof(RTMEMHDRR3), fFlags, &pv);
|
---|
| 87 | else
|
---|
| 88 | rc = rtMemAllocEx32BitReach(cbAligned + sizeof(RTMEMHDRR3), fFlags, &pv);
|
---|
| 89 | if (RT_FAILURE(rc))
|
---|
| 90 | return rc;
|
---|
| 91 | }
|
---|
[46570] | 92 | else if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
|
---|
[46567] | 93 | {
|
---|
[97904] | 94 | pv = RTMemPageAlloc(cbAligned + sizeof(RTMEMHDRR3));
|
---|
| 95 | if (pv)
|
---|
| 96 | {
|
---|
| 97 | if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
|
---|
| 98 | RT_BZERO(pv, cbAligned + sizeof(RTMEMHDRR3));
|
---|
| 99 |
|
---|
| 100 | int rc = RTMemProtect(pv, cbAligned + sizeof(RTMEMHDRR3), RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE);
|
---|
| 101 | if (RT_FAILURE(rc))
|
---|
| 102 | {
|
---|
| 103 | RTMemPageFree(pv, cbAligned + sizeof(RTMEMHDRR3));
|
---|
| 104 | return rc;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
[46567] | 107 | }
|
---|
| 108 | else if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
|
---|
| 109 | pv = RTMemAllocZ(cbAligned + sizeof(RTMEMHDRR3));
|
---|
| 110 | else
|
---|
| 111 | pv = RTMemAlloc(cbAligned + sizeof(RTMEMHDRR3));
|
---|
| 112 | if (!pv)
|
---|
| 113 | return VERR_NO_MEMORY;
|
---|
| 114 |
|
---|
| 115 | /*
|
---|
| 116 | * Fill in the header and return.
|
---|
| 117 | */
|
---|
| 118 | PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv;
|
---|
| 119 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
| 120 | pHdr->fFlags = fFlags;
|
---|
[46739] | 121 | pHdr->cb = (uint32_t)cbAligned;
|
---|
| 122 | pHdr->cbReq = (uint32_t)cb;
|
---|
[46567] | 123 |
|
---|
| 124 | *ppv = pHdr + 1;
|
---|
| 125 | return VINF_SUCCESS;
|
---|
| 126 | }
|
---|
| 127 | RT_EXPORT_SYMBOL(RTMemAllocExTag);
|
---|
| 128 |
|
---|
| 129 |
|
---|
[57432] | 130 | RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
[46567] | 131 | {
|
---|
| 132 | if (!pv)
|
---|
| 133 | return;
|
---|
| 134 | AssertPtr(pv);
|
---|
| 135 |
|
---|
| 136 | PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv - 1;
|
---|
| 137 | AssertMsg(pHdr->u32Magic == RTMEMHDR_MAGIC, ("pHdr->u32Magic=%RX32 pv=%p cb=%#x\n", pHdr->u32Magic, pv, cb));
|
---|
| 138 | pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
|
---|
[62564] | 139 | Assert(pHdr->cbReq == cb); RT_NOREF_PV(cb);
|
---|
[46567] | 140 |
|
---|
[46570] | 141 | if (pHdr->fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
|
---|
[46739] | 142 | rtMemFreeExYyBitReach(pHdr, pHdr->cb + sizeof(*pHdr), pHdr->fFlags);
|
---|
[46570] | 143 | else if (pHdr->fFlags & RTMEMALLOCEX_FLAGS_EXEC)
|
---|
[97904] | 144 | {
|
---|
| 145 | RTMemProtect(pHdr, pHdr->cb + sizeof(*pHdr), RTMEM_PROT_READ | RTMEM_PROT_WRITE);
|
---|
| 146 | RTMemPageFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
| 147 | }
|
---|
[46567] | 148 | else
|
---|
| 149 | RTMemFree(pHdr);
|
---|
| 150 | }
|
---|
| 151 | RT_EXPORT_SYMBOL(RTMemFreeEx);
|
---|
| 152 |
|
---|