VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/allocex.cpp@ 96507

Last change on this file since 96507 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1/* $Id: allocex.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Extended Alloc and Free Functions for Ring-3, posix.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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/err.h>
47#include <iprt/string.h>
48#include "internal/magics.h"
49#include "allocex.h"
50
51
52RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW_DEF
53{
54 RT_NOREF_PV(pszTag);
55
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
64 if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX)
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));
75 AssertMsgReturn(cbAligned >= cb && cbAligned <= ~(size_t)0, ("cbAligned=%#zx cb=%#zx", cbAligned, cb),
76 VERR_INVALID_PARAMETER);
77
78 /*
79 * Allocate the requested memory.
80 */
81 void *pv;
82 if (fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
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 }
92 else if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
93 {
94 pv = RTMemExecAlloc(cbAligned + sizeof(RTMEMHDRR3));
95 if ((fFlags & RTMEMALLOCEX_FLAGS_ZEROED) && pv)
96 RT_BZERO(pv, cbAligned + sizeof(RTMEMHDRR3));
97 }
98 else if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
99 pv = RTMemAllocZ(cbAligned + sizeof(RTMEMHDRR3));
100 else
101 pv = RTMemAlloc(cbAligned + sizeof(RTMEMHDRR3));
102 if (!pv)
103 return VERR_NO_MEMORY;
104
105 /*
106 * Fill in the header and return.
107 */
108 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv;
109 pHdr->u32Magic = RTMEMHDR_MAGIC;
110 pHdr->fFlags = fFlags;
111 pHdr->cb = (uint32_t)cbAligned;
112 pHdr->cbReq = (uint32_t)cb;
113
114 *ppv = pHdr + 1;
115 return VINF_SUCCESS;
116}
117RT_EXPORT_SYMBOL(RTMemAllocExTag);
118
119
120RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW_DEF
121{
122 if (!pv)
123 return;
124 AssertPtr(pv);
125
126 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv - 1;
127 AssertMsg(pHdr->u32Magic == RTMEMHDR_MAGIC, ("pHdr->u32Magic=%RX32 pv=%p cb=%#x\n", pHdr->u32Magic, pv, cb));
128 pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
129 Assert(pHdr->cbReq == cb); RT_NOREF_PV(cb);
130
131 if (pHdr->fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
132 rtMemFreeExYyBitReach(pHdr, pHdr->cb + sizeof(*pHdr), pHdr->fFlags);
133 else if (pHdr->fFlags & RTMEMALLOCEX_FLAGS_EXEC)
134 RTMemExecFree(pHdr, pHdr->cb + sizeof(*pHdr));
135 else
136 RTMemFree(pHdr);
137}
138RT_EXPORT_SYMBOL(RTMemFreeEx);
139
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