# new

Go to the documentation of this file.

    00001 // The -*- C++ -*- dynamic memory management header.
    00002
    00003 // Copyright (C) 1994, 1996, 1997, 1998, 2000, 2001, 2002
    00004 // Free Software Foundation
    00005
    00006 // This file is part of GCC.
    00007 //
    00008 // GCC is free software; you can redistribute it and/or modify
    00009 // it under the terms of the GNU General Public License as published by
    00010 // the Free Software Foundation; either version 2, or (at your option)
    00011 // any later version.
    00012 //
    00013 // GCC is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016 // GNU General Public License for more details.
    00017 //
    00018 // You should have received a copy of the GNU General Public License
    00019 // along with GCC; see the file COPYING.  If not, write to
    00020 // the Free Software Foundation, 59 Temple Place - Suite 330,
    00021 // Boston, MA 02111-1307, USA.
    00022
    00023 // As a special exception, you may use this file as part of a free software
    00024 // library without restriction.  Specifically, if other files instantiate
    00025 // templates or use macros or inline functions from this file, or you compile
    00026 // this file and link it with other files to produce an executable, this
    00027 // file does not by itself cause the resulting executable to be covered by
    00028 // the GNU General Public License.  This exception does not however
    00029 // invalidate any other reasons why the executable file might be covered by
    00030 // the GNU General Public License.
    00031
    00032 /** @file new
    00033  *  The header @c new defines several functions to manage dynamic memory and
    00034  *  handling memory allocation errors; see
    00035  *  http://gcc.gnu.org/onlinedocs/libstdc++/18_support/howto.html#4 for more.
    00036  */
    00037
    00038 #ifndef _NEW
    00039 #define _NEW
    00040
    00041 #include <cstddef>
    00042 #include <exception>
    00043
    00044 #pragma GCC visibility push(default)
    00045
    00046 extern "C++" {
    00047
    00048 namespace std
    00049 {
    00050   /**
    00051    *  @brief  Exception possibly thrown by @c new.
    00052    *
    00053    *  @c bad_alloc (or classes derived from it) is used to report allocation
    00054    *  errors from the throwing forms of @c new.  */
    00055   class bad_alloc : public exception
    00056   {
    00057   public:
    00058     bad_alloc() throw() { }
    00059     // This declaration is not useless:
    00060     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
    00061     virtual ~bad_alloc() throw();
    00062   };
    00063
    00064   struct nothrow_t { };
    00065   extern const nothrow_t nothrow;
    00066   /** If you write your own error handler to be called by @c new, it must
    00067    *  be of this type.  */
    00068   typedef void (*new_handler)();
    00069   /// Takes a replacement handler as the argument, returns the previous handler.
    00070   new_handler set_new_handler(new_handler) throw();
    00071 } // namespace std
    00072
    00073 //@{
    00074 /** These are replaceable signatures:
    00075  *  - normal single new and delete (no arguments, throw @c bad_alloc on error)
    00076  *  - normal array new and delete (same)
    00077  *  - @c nothrow single new and delete (take a @c nothrow argument, return
    00078  *    @c NULL on error)
    00079  *  - @c nothrow array new and delete (same)
    00080  *
    00081  *  Placement new and delete signatures (take a memory address argument,
    00082  *  does nothing) may not be replaced by a user's program.
    00083 */
    00084 void* operator new(std::size_t) throw (std::bad_alloc);
    00085 void* operator new[](std::size_t) throw (std::bad_alloc);
    00086 void operator delete(void*) throw();
    00087 void operator delete[](void*) throw();
    00088 void* operator new(std::size_t, const std::nothrow_t&) throw();
    00089 void* operator new[](std::size_t, const std::nothrow_t&) throw();
    00090 void operator delete(void*, const std::nothrow_t&) throw();
    00091 void operator delete[](void*, const std::nothrow_t&) throw();
    00092
    00093 // Default placement versions of operator new.
    00094 inline void* operator new(std::size_t, void* __p) throw() { return __p; }
    00095 inline void* operator new[](std::size_t, void* __p) throw() { return __p; }
    00096
    00097 // Default placement versions of operator delete.
    00098 inline void  operator delete  (void*, void*) throw() { }
    00099 inline void  operator delete[](void*, void*) throw() { }
    00100 //@}
    00101 } // extern "C++"
    00102
    00103 #pragma GCC visibility pop
    00104
    00105 #endif