1 /*
   2  * Copyright (c) 2016, 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 #include "precompiled.hpp"
  26 #include "utilities/debug.hpp"
  27 
  28 #include <new>
  29 
  30 //--------------------------------------------------------------------------------------
  31 // Non-product code
  32 
  33 #ifndef PRODUCT
  34 // The global operator new should never be called since it will usually indicate
  35 // a memory leak.  Use CHeapObj as the base class of such objects to make it explicit
  36 // that they're allocated on the C heap.
  37 // Commented out in product version to avoid conflicts with third-party C++ native code.
  38 //
  39 // In C++98/03 the throwing new operators are defined with the following signature:
  40 //
  41 // void* operator new(std::size_tsize) throw(std::bad_alloc);
  42 // void* operator new[](std::size_tsize) throw(std::bad_alloc);
  43 //
  44 // while all the other (non-throwing) new and delete operators are defined with an empty
  45 // throw clause (i.e. "operator delete(void* p) throw()") which means that they do not
  46 // throw any exceptions (see section 18.4 of the C++ standard).
  47 //
  48 // In the new C++11/14 standard, the signature of the throwing new operators was changed
  49 // by completely omitting the throw clause (which effectively means they could throw any
  50 // exception) while all the other new/delete operators where changed to have a 'nothrow'
  51 // clause instead of an empty throw clause.
  52 //
  53 // Unfortunately, the support for exception specifications among C++ compilers is still
  54 // very fragile. While some more strict compilers like AIX xlC or HP aCC reject to
  55 // override the default throwing new operator with a user operator with an empty throw()
  56 // clause, the MS Visual C++ compiler warns for every non-empty throw clause like
  57 // throw(std::bad_alloc) that it will ignore the exception specification. The following
  58 // operator definitions have been checked to correctly work with all currently supported
  59 // compilers and they should be upwards compatible with C++11/14. Therefore
  60 // PLEASE BE CAREFUL if you change the signature of the following operators!
  61 
  62 static void * zero = (void *) 0;
  63 
  64 void* operator new(size_t size) /* throw(std::bad_alloc) */ {
  65   fatal("Should not call global operator new");
  66   return zero;
  67 }
  68 
  69 void* operator new [](size_t size) /* throw(std::bad_alloc) */ {
  70   fatal("Should not call global operator new[]");
  71   return zero;
  72 }
  73 
  74 void* operator new(size_t size, const std::nothrow_t&  nothrow_constant) throw() {
  75   fatal("Should not call global operator new");
  76   return 0;
  77 }
  78 
  79 void* operator new [](size_t size, std::nothrow_t&  nothrow_constant) throw() {
  80   fatal("Should not call global operator new[]");
  81   return 0;
  82 }
  83 
  84 void operator delete(void* p) throw() {
  85   fatal("Should not call global delete");
  86 }
  87 
  88 void operator delete [](void* p) throw() {
  89   fatal("Should not call global delete []");
  90 }
  91 
  92 #endif // Non-product