libstdc++
new
Go to the documentation of this file.
1 // The -*- C++ -*- dynamic memory management header.
2 
3 // Copyright (C) 1994-2014 Free Software Foundation, Inc.
4 
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file new
27  * This is a Standard C++ Library header.
28  *
29  * The header @c new defines several functions to manage dynamic memory and
30  * handling memory allocation errors; see
31  * http://gcc.gnu.org/onlinedocs/libstdc++/18_support/howto.html#4 for more.
32  */
33 
34 #ifndef _NEW
35 #define _NEW
36 
37 #pragma GCC system_header
38 
39 #include <bits/c++config.h>
40 #include <exception>
41 
42 #pragma GCC visibility push(default)
43 
44 extern "C++" {
45 
46 namespace std
47 {
48  /**
49  * @brief Exception possibly thrown by @c new.
50  * @ingroup exceptions
51  *
52  * @c bad_alloc (or classes derived from it) is used to report allocation
53  * errors from the throwing forms of @c new. */
54  class bad_alloc : public exception
55  {
56  public:
57  bad_alloc() throw() { }
58 
59  // This declaration is not useless:
60  // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
61  virtual ~bad_alloc() throw();
62 
63  // See comment in eh_exception.cc.
64  virtual const char* what() const throw();
65  };
66 
67 #if __cplusplus >= 201103L
68  class bad_array_new_length : public bad_alloc
69  {
70  public:
71  bad_array_new_length() throw() { };
72 
73  // This declaration is not useless:
74  // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
75  virtual ~bad_array_new_length() throw();
76 
77  // See comment in eh_exception.cc.
78  virtual const char* what() const throw();
79  };
80 #endif
81 
82  // We throw this exception for GNU VLAs of negative length in all C++
83  // dialects, so declare it if we aren't in strict conformance mode.
84 #if __cplusplus > 201103L || !defined(__STRICT_ANSI__)
85  class bad_array_length : public bad_alloc
86  {
87  public:
88  bad_array_length() throw() { };
89 
90  // This declaration is not useless:
91  // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
92  virtual ~bad_array_length() throw();
93 
94  // See comment in eh_exception.cc.
95  virtual const char* what() const throw();
96  };
97 #endif
98 
99  struct nothrow_t { };
100 
101  extern const nothrow_t nothrow;
102 
103  /** If you write your own error handler to be called by @c new, it must
104  * be of this type. */
105  typedef void (*new_handler)();
106 
107  /// Takes a replacement handler as the argument, returns the
108  /// previous handler.
110 
111 #if __cplusplus >= 201103L
112  /// Return the current new handler.
113  new_handler get_new_handler() noexcept;
114 #endif
115 } // namespace std
116 
117 //@{
118 /** These are replaceable signatures:
119  * - normal single new and delete (no arguments, throw @c bad_alloc on error)
120  * - normal array new and delete (same)
121  * - @c nothrow single new and delete (take a @c nothrow argument, return
122  * @c NULL on error)
123  * - @c nothrow array new and delete (same)
124  *
125  * Placement new and delete signatures (take a memory address argument,
126  * does nothing) may not be replaced by a user's program.
127 */
128 void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
129  __attribute__((__externally_visible__));
130 void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
131  __attribute__((__externally_visible__));
132 void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
133  __attribute__((__externally_visible__));
134 void operator delete[](void*) _GLIBCXX_USE_NOEXCEPT
135  __attribute__((__externally_visible__));
136 void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
137  __attribute__((__externally_visible__));
138 void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
139  __attribute__((__externally_visible__));
140 void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
141  __attribute__((__externally_visible__));
142 void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
143  __attribute__((__externally_visible__));
144 
145 // Default placement versions of operator new.
146 inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
147 { return __p; }
148 inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
149 { return __p; }
150 
151 // Default placement versions of operator delete.
152 inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
153 inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
154 //@}
155 } // extern "C++"
156 
157 #pragma GCC visibility pop
158 
159 #endif
void(* new_handler)()
Definition: new:105
Base class for all library exceptions.
Definition: exception:60
new_handler set_new_handler(new_handler)
Takes a replacement handler as the argument, returns the previous handler.
new_handler get_new_handler() noexcept
Return the current new handler.
virtual const char * what() const
Exception possibly thrown by new.bad_alloc (or classes derived from it) is used to report allocation ...
Definition: new:54