Path: blob/master/src/hotspot/share/memory/operator_new.cpp
40949 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "utilities/debug.hpp"2627#include <new>2829//--------------------------------------------------------------------------------------30// Non-product code3132#ifndef PRODUCT33// The global operator new should never be called since it will usually indicate34// a memory leak. Use CHeapObj as the base class of such objects to make it explicit35// that they're allocated on the C heap.36// Commented out in product version to avoid conflicts with third-party C++ native code.37//38// In C++98/03 the throwing new operators are defined with the following signature:39//40// void* operator new(std::size_tsize) throw(std::bad_alloc);41// void* operator new[](std::size_tsize) throw(std::bad_alloc);42//43// while all the other (non-throwing) new and delete operators are defined with an empty44// throw clause (i.e. "operator delete(void* p) throw()") which means that they do not45// throw any exceptions (see section 18.4 of the C++ standard).46//47// In the new C++11/14 standard, the signature of the throwing new operators was changed48// by completely omitting the throw clause (which effectively means they could throw any49// exception) while all the other new/delete operators where changed to have a 'nothrow'50// clause instead of an empty throw clause.51//52// Unfortunately, the support for exception specifications among C++ compilers is still53// very fragile. While some more strict compilers like AIX xlC or HP aCC reject to54// override the default throwing new operator with a user operator with an empty throw()55// clause, the MS Visual C++ compiler warns for every non-empty throw clause like56// throw(std::bad_alloc) that it will ignore the exception specification. The following57// operator definitions have been checked to correctly work with all currently supported58// compilers and they should be upwards compatible with C++11/14. Therefore59// PLEASE BE CAREFUL if you change the signature of the following operators!6061static void * zero = (void *) 0;6263void* operator new(size_t size) /* throw(std::bad_alloc) */ {64fatal("Should not call global operator new");65return zero;66}6768void* operator new [](size_t size) /* throw(std::bad_alloc) */ {69fatal("Should not call global operator new[]");70return zero;71}7273void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {74fatal("Should not call global operator new");75return 0;76}7778void* operator new [](size_t size, std::nothrow_t& nothrow_constant) throw() {79fatal("Should not call global operator new[]");80return 0;81}8283void operator delete(void* p) throw() {84fatal("Should not call global delete");85}8687void operator delete [](void* p) throw() {88fatal("Should not call global delete []");89}9091#ifdef __GNUG__92// Warning disabled for gcc 5.493PRAGMA_DIAG_PUSH94PRAGMA_DISABLE_GCC_WARNING("-Wc++14-compat")95#endif // __GNUG__9697void operator delete(void* p, size_t size) throw() {98fatal("Should not call global sized delete");99}100101void operator delete [](void* p, size_t size) throw() {102fatal("Should not call global sized delete []");103}104105#ifdef __GNUG__106PRAGMA_DIAG_POP107#endif // __GNUG__108109#endif // Non-product110111112