|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef SHARE_CPPSTDLIB_NEW_HPP |
| 26 | +#define SHARE_CPPSTDLIB_NEW_HPP |
| 27 | + |
| 28 | +#include "utilities/compilerWarnings.hpp" |
| 29 | + |
| 30 | +// HotSpot usage: |
| 31 | +// Only the following may be used: |
| 32 | +// * std::nothrow_t, std::nothrow |
| 33 | +// * std::align_val_t |
| 34 | +// * The non-allocating forms of `operator new` and `operator new[]` are |
| 35 | +// implicitly used by the corresponding `new` and `new[]` expressions. |
| 36 | +// - operator new(size_t, void*) noexcept |
| 37 | +// - operator new[](size_t, void*) noexcept |
| 38 | +// Note that the non-allocating forms of `operator delete` and `operator |
| 39 | +// delete[]` are not used, since they are only invoked by a placement new |
| 40 | +// expression that fails by throwing an exception. But they might still |
| 41 | +// end up being referenced in such a situation. |
| 42 | + |
| 43 | +BEGIN_ALLOW_FORBIDDEN_FUNCTIONS |
| 44 | +#include "utilities/vmassert_uninstall.hpp" |
| 45 | + |
| 46 | +#include <new> |
| 47 | + |
| 48 | +#include "utilities/vmassert_reinstall.hpp" // don't reorder |
| 49 | +END_ALLOW_FORBIDDEN_FUNCTIONS |
| 50 | + |
| 51 | +// Deprecation declarations to forbid use of the default global allocator. |
| 52 | +// See C++17 21.6.1 Header <new> synopsis. |
| 53 | + |
| 54 | +namespace std { |
| 55 | + |
| 56 | +#if 0 |
| 57 | +// We could deprecate exception types, for completeness, but don't bother. We |
| 58 | +// already have exceptions disabled, and run into compiler bugs when we try. |
| 59 | +// |
| 60 | +// gcc -Wattributes => type attributes ignored after type is already defined |
| 61 | +// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122167 |
| 62 | +// |
| 63 | +// clang -Wignored-attributes => attribute declaration must precede definition |
| 64 | +// The clang warning is https://github.com/llvm/llvm-project/issues/135481, |
| 65 | +// which should be fixed in clang 21. |
| 66 | +class [[deprecated]] bad_alloc; |
| 67 | +class [[deprecated]] bad_array_new_length; |
| 68 | +#endif // #if 0 |
| 69 | + |
| 70 | +// Forbid new_handler manipulation by HotSpot code, leaving it untouched for |
| 71 | +// use by application code. |
| 72 | +[[deprecated]] new_handler get_new_handler() noexcept; |
| 73 | +[[deprecated]] new_handler set_new_handler(new_handler) noexcept; |
| 74 | + |
| 75 | +// Prefer HotSpot mechanisms for padding. |
| 76 | +// |
| 77 | +// The syntax for redeclaring these for deprecation is tricky, and not |
| 78 | +// supported by some versions of some compilers. Dispatch on compiler and |
| 79 | +// version to decide whether to redeclare deprecated. |
| 80 | + |
| 81 | +#if defined(__clang__) |
| 82 | +#if __clang_major__ >= 19 |
| 83 | +// clang18 and earlier may accept the declaration but go wrong with uses. |
| 84 | +// Different warnings and link-time failures are both possible. |
| 85 | +#define CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES 1 |
| 86 | +#endif // restrict clang version |
| 87 | + |
| 88 | +#elif defined(__GNUC__) |
| 89 | +#if (__GNUC__ > 13) || (__GNUC__ == 13 && __GNUC_MINOR__ >= 2) |
| 90 | +// g++11.5 accepts the declaration and reports deprecation for uses, but also |
| 91 | +// has link-time failure for uses. Haven't tested intermediate versions. |
| 92 | +#define CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES 1 |
| 93 | +#endif // restrict gcc version |
| 94 | + |
| 95 | +#elif defined(_MSVC) |
| 96 | +// VS2022-17.13.2 => error C2370: '...': redefinition; different storage class |
| 97 | + |
| 98 | +#endif // Compiler dispatch |
| 99 | + |
| 100 | +// Redeclare deprecated if such is supported. |
| 101 | +#ifdef CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES |
| 102 | +[[deprecated]] extern const size_t hardware_destructive_interference_size; |
| 103 | +[[deprecated]] extern const size_t hardware_constructive_interference_size; |
| 104 | +#undef CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES |
| 105 | +#endif // CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES |
| 106 | + |
| 107 | +} // namespace std |
| 108 | + |
| 109 | +// Forbid using the global allocator by HotSpot code. |
| 110 | +// This doesn't provide complete coverage. Some global allocation and |
| 111 | +// deallocation functions are implicitly declared in all translation units, |
| 112 | +// without needing to include <new>; see C++17 6.7.4. So this doesn't remove |
| 113 | +// the need for the link-time verification that these functions aren't used. |
| 114 | +// |
| 115 | +// But don't poison them when compiling gtests. The gtest framework, the |
| 116 | +// HotSpot wrapper around it (gtestMain.cpp), and even some tests, all have |
| 117 | +// new/new[] and delete/delete[] expressions that use the default global |
| 118 | +// allocator. We also don't apply the link-time check for gtests, for the |
| 119 | +// same reason. |
| 120 | +#ifndef HOTSPOT_GTEST |
| 121 | + |
| 122 | +[[deprecated]] void* operator new(std::size_t); |
| 123 | +[[deprecated]] void* operator new(std::size_t, std::align_val_t); |
| 124 | +[[deprecated]] void* operator new(std::size_t, const std::nothrow_t&) noexcept; |
| 125 | +[[deprecated]] void* operator new(std::size_t, std::align_val_t, |
| 126 | + const std::nothrow_t&) noexcept; |
| 127 | + |
| 128 | +[[deprecated]] void operator delete(void*) noexcept; |
| 129 | +[[deprecated]] void operator delete(void*, std::size_t) noexcept; |
| 130 | +[[deprecated]] void operator delete(void*, std::align_val_t) noexcept; |
| 131 | +[[deprecated]] void operator delete(void*, std::size_t, std::align_val_t) noexcept; |
| 132 | +[[deprecated]] void operator delete(void*, const std::nothrow_t&) noexcept; |
| 133 | +[[deprecated]] void operator delete(void*, std::align_val_t, |
| 134 | + const std::nothrow_t&) noexcept; |
| 135 | + |
| 136 | +[[deprecated]] void* operator new[](std::size_t); |
| 137 | +[[deprecated]] void* operator new[](std::size_t, std::align_val_t); |
| 138 | +[[deprecated]] void* operator new[](std::size_t, const std::nothrow_t&) noexcept; |
| 139 | +[[deprecated]] void* operator new[](std::size_t, std::align_val_t, |
| 140 | + const std::nothrow_t&) noexcept; |
| 141 | + |
| 142 | +[[deprecated]] void operator delete[](void*) noexcept; |
| 143 | +[[deprecated]] void operator delete[](void*, std::size_t) noexcept; |
| 144 | +[[deprecated]] void operator delete[](void*, std::align_val_t) noexcept; |
| 145 | +[[deprecated]] void operator delete[](void*, std::size_t, std::align_val_t) noexcept; |
| 146 | +[[deprecated]] void operator delete[](void*, const std::nothrow_t&) noexcept; |
| 147 | +[[deprecated]] void operator delete[](void*, std::align_val_t, |
| 148 | + const std::nothrow_t&) noexcept; |
| 149 | + |
| 150 | +#endif // HOTSPOT_GTEST |
| 151 | + |
| 152 | +// Allow (don't poison) the non-allocating forms from [new.delete.placement]. |
| 153 | + |
| 154 | +#endif // SHARE_CPPSTDLIB_NEW_HPP |
0 commit comments