[1] | 1 | /* $Id: alloc-ef-cpp.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Memory Allocation, C++ electric fence.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 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 | *
|
---|
[5999] | 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
|
---|
[5999] | 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
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #include "alloc-ef.h"
|
---|
| 42 |
|
---|
[28298] | 43 | #include <iprt/asm.h>
|
---|
[1] | 44 | #include <new>
|
---|
| 45 |
|
---|
| 46 |
|
---|
[57358] | 47 | /*********************************************************************************************************************************
|
---|
| 48 | * Defined Constants And Macros *
|
---|
| 49 | *********************************************************************************************************************************/
|
---|
[1] | 50 | /** @todo test this on MSC */
|
---|
| 51 |
|
---|
[39088] | 52 | /** MSC declares the operators as cdecl it seems. */
|
---|
[1] | 53 | #ifdef _MSC_VER
|
---|
| 54 | # define RT_EF_CDECL __cdecl
|
---|
| 55 | #else
|
---|
| 56 | # define RT_EF_CDECL
|
---|
| 57 | #endif
|
---|
| 58 |
|
---|
[39088] | 59 | /** MSC doesn't use the standard namespace. */
|
---|
[1] | 60 | #ifdef _MSC_VER
|
---|
| 61 | # define RT_EF_SIZE_T size_t
|
---|
| 62 | #else
|
---|
| 63 | # define RT_EF_SIZE_T std::size_t
|
---|
| 64 | #endif
|
---|
| 65 |
|
---|
[39088] | 66 | /** The hint that we're throwing std::bad_alloc is not apprecitated by MSC. */
|
---|
[39089] | 67 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
| 68 | # ifdef _MSC_VER
|
---|
[39092] | 69 | # define RT_EF_THROWS_BAD_ALLOC
|
---|
[57434] | 70 | # define RT_EF_NOTHROW RT_NO_THROW_DEF
|
---|
[39088] | 71 | # else
|
---|
[54372] | 72 | # ifdef _GLIBCXX_THROW
|
---|
| 73 | # define RT_EF_THROWS_BAD_ALLOC _GLIBCXX_THROW(std::bad_alloc)
|
---|
[94877] | 74 | # elif defined(__cplusplus) && (__cplusplus + 0) < 201700
|
---|
| 75 | # define RT_EF_THROWS_BAD_ALLOC throw(std::bad_alloc)
|
---|
[94876] | 76 | # else
|
---|
[94877] | 77 | # define RT_EF_THROWS_BAD_ALLOC noexcept(false)
|
---|
[54372] | 78 | # endif
|
---|
[57434] | 79 | # define RT_EF_NOTHROW throw()
|
---|
[39088] | 80 | # endif
|
---|
[39089] | 81 | #else /* !RT_EXCEPTIONS_ENABLED */
|
---|
| 82 | # define RT_EF_THROWS_BAD_ALLOC
|
---|
[57434] | 83 | # define RT_EF_NOTHROW
|
---|
[39089] | 84 | #endif /* !RT_EXCEPTIONS_ENABLED */
|
---|
[1] | 85 |
|
---|
[39088] | 86 |
|
---|
| 87 | void *RT_EF_CDECL operator new(RT_EF_SIZE_T cb) RT_EF_THROWS_BAD_ALLOC
|
---|
[1] | 88 | {
|
---|
[31157] | 89 | void *pv = rtR3MemAlloc("new", RTMEMTYPE_NEW, cb, cb, NULL, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 90 | if (!pv)
|
---|
| 91 | throw std::bad_alloc();
|
---|
| 92 | return pv;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
[57434] | 96 | void *RT_EF_CDECL operator new(RT_EF_SIZE_T cb, const std::nothrow_t &) RT_EF_NOTHROW
|
---|
[1] | 97 | {
|
---|
[31157] | 98 | void *pv = rtR3MemAlloc("new nothrow", RTMEMTYPE_NEW, cb, cb, NULL, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 99 | return pv;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[57434] | 103 | void RT_EF_CDECL operator delete(void *pv) RT_EF_NOTHROW
|
---|
[1] | 104 | {
|
---|
[83546] | 105 | rtR3MemFree("delete", RTMEMTYPE_DELETE, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|
[59273] | 109 | #ifdef __cpp_sized_deallocation
|
---|
| 110 | void RT_EF_CDECL operator delete(void *pv, RT_EF_SIZE_T cb) RT_EF_NOTHROW
|
---|
| 111 | {
|
---|
| 112 | NOREF(cb);
|
---|
| 113 | AssertMsgFailed(("cb ignored!\n"));
|
---|
[83546] | 114 | rtR3MemFree("delete", RTMEMTYPE_DELETE, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[59273] | 115 | }
|
---|
| 116 | #endif
|
---|
| 117 |
|
---|
| 118 |
|
---|
[57434] | 119 | void RT_EF_CDECL operator delete(void * pv, const std::nothrow_t &) RT_EF_NOTHROW
|
---|
[1] | 120 | {
|
---|
[83546] | 121 | rtR3MemFree("delete nothrow", RTMEMTYPE_DELETE, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | /*
|
---|
| 126 | *
|
---|
| 127 | * Array object form.
|
---|
| 128 | * Array object form.
|
---|
| 129 | * Array object form.
|
---|
| 130 | *
|
---|
| 131 | */
|
---|
| 132 |
|
---|
[39088] | 133 | void *RT_EF_CDECL operator new[](RT_EF_SIZE_T cb) RT_EF_THROWS_BAD_ALLOC
|
---|
[1] | 134 | {
|
---|
[31157] | 135 | void *pv = rtR3MemAlloc("new[]", RTMEMTYPE_NEW_ARRAY, cb, cb, NULL, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 136 | if (!pv)
|
---|
| 137 | throw std::bad_alloc();
|
---|
| 138 | return pv;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
[57434] | 142 | void * RT_EF_CDECL operator new[](RT_EF_SIZE_T cb, const std::nothrow_t &) RT_EF_NOTHROW
|
---|
[1] | 143 | {
|
---|
[31157] | 144 | void *pv = rtR3MemAlloc("new[] nothrow", RTMEMTYPE_NEW_ARRAY, cb, cb, NULL, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 145 | return pv;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
[57434] | 149 | void RT_EF_CDECL operator delete[](void * pv) RT_EF_NOTHROW
|
---|
[1] | 150 | {
|
---|
[83546] | 151 | rtR3MemFree("delete[]", RTMEMTYPE_DELETE_ARRAY, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
[59273] | 155 | #ifdef __cpp_sized_deallocation
|
---|
| 156 | void RT_EF_CDECL operator delete[](void * pv, RT_EF_SIZE_T cb) RT_EF_NOTHROW
|
---|
| 157 | {
|
---|
| 158 | NOREF(cb);
|
---|
| 159 | AssertMsgFailed(("cb ignored!\n"));
|
---|
[83546] | 160 | rtR3MemFree("delete[]", RTMEMTYPE_DELETE_ARRAY, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[59273] | 161 | }
|
---|
| 162 | #endif
|
---|
| 163 |
|
---|
| 164 |
|
---|
[57434] | 165 | void RT_EF_CDECL operator delete[](void *pv, const std::nothrow_t &) RT_EF_NOTHROW
|
---|
[1] | 166 | {
|
---|
[83546] | 167 | rtR3MemFree("delete[] nothrow", RTMEMTYPE_DELETE_ARRAY, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[1] | 168 | }
|
---|
| 169 |
|
---|