new 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // The -*- C++ -*- dynamic memory management header.
  2. // Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  3. // 2003, 2004, 2005, 2006, 2007, 2009
  4. // Free Software Foundation
  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. // You should have received a copy of the GNU General Public License and
  21. // a copy of the GCC Runtime Library Exception along with this program;
  22. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  23. // <http://www.gnu.org/licenses/>.
  24. /** @file new
  25. * This is a Standard C++ Library header.
  26. *
  27. * The header @c new defines several functions to manage dynamic memory and
  28. * handling memory allocation errors; see
  29. * http://gcc.gnu.org/onlinedocs/libstdc++/18_support/howto.html#4 for more.
  30. */
  31. #ifndef _NEW
  32. #define _NEW
  33. #include <cstddef>
  34. #include <exception>
  35. #pragma GCC visibility push(default)
  36. extern "C++" {
  37. namespace std
  38. {
  39. /**
  40. * @brief Exception possibly thrown by @c new.
  41. * @ingroup exceptions
  42. *
  43. * @c bad_alloc (or classes derived from it) is used to report allocation
  44. * errors from the throwing forms of @c new. */
  45. class bad_alloc : public exception
  46. {
  47. public:
  48. bad_alloc() throw() { }
  49. // This declaration is not useless:
  50. // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
  51. virtual ~bad_alloc() throw();
  52. // See comment in eh_exception.cc.
  53. virtual const char* what() const throw();
  54. };
  55. struct nothrow_t { };
  56. extern const nothrow_t nothrow;
  57. /** If you write your own error handler to be called by @c new, it must
  58. * be of this type. */
  59. typedef void (*new_handler)();
  60. /// Takes a replacement handler as the argument, returns the
  61. /// previous handler.
  62. new_handler set_new_handler(new_handler) throw();
  63. } // namespace std
  64. //@{
  65. /** These are replaceable signatures:
  66. * - normal single new and delete (no arguments, throw @c bad_alloc on error)
  67. * - normal array new and delete (same)
  68. * - @c nothrow single new and delete (take a @c nothrow argument, return
  69. * @c NULL on error)
  70. * - @c nothrow array new and delete (same)
  71. *
  72. * Placement new and delete signatures (take a memory address argument,
  73. * does nothing) may not be replaced by a user's program.
  74. */
  75. void* operator new(std::size_t) throw (std::bad_alloc);
  76. void* operator new[](std::size_t) throw (std::bad_alloc);
  77. void operator delete(void*) throw();
  78. void operator delete[](void*) throw();
  79. void* operator new(std::size_t, const std::nothrow_t&) throw();
  80. void* operator new[](std::size_t, const std::nothrow_t&) throw();
  81. void operator delete(void*, const std::nothrow_t&) throw();
  82. void operator delete[](void*, const std::nothrow_t&) throw();
  83. // Default placement versions of operator new.
  84. inline void* operator new(std::size_t, void* __p) throw() { return __p; }
  85. inline void* operator new[](std::size_t, void* __p) throw() { return __p; }
  86. // Default placement versions of operator delete.
  87. inline void operator delete (void*, void*) throw() { }
  88. inline void operator delete[](void*, void*) throw() { }
  89. //@}
  90. } // extern "C++"
  91. #pragma GCC visibility pop
  92. #endif