new 4.6 KB

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