testsuite_new_operators.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // -*- C++ -*-
  2. // Utility subroutines for the C++ library testsuite.
  3. //
  4. // Copyright (C) 2000-2022 Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. //
  12. // This library 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. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. //
  21. #ifndef _GLIBCXX_TESTSUITE_NEW_OPERATORS_H
  22. #define _GLIBCXX_TESTSUITE_NEW_OPERATORS_H
  23. #include <new>
  24. #include <testsuite_hooks.h>
  25. namespace __gnu_test
  26. {
  27. std::size_t&
  28. get_new_limit()
  29. {
  30. static std::size_t limit = 1024 * 1024;
  31. return limit;
  32. }
  33. void
  34. set_new_limit(std::size_t l)
  35. { get_new_limit() = l; }
  36. }
  37. void* operator new(std::size_t size) THROW(std::bad_alloc)
  38. {
  39. if (size > __gnu_test::get_new_limit())
  40. throw std::bad_alloc();
  41. void* p = std::malloc(size);
  42. if (!p)
  43. throw std::bad_alloc();
  44. return p;
  45. }
  46. void* operator new (std::size_t size, const std::nothrow_t&) throw()
  47. {
  48. if (size > __gnu_test::get_new_limit())
  49. return 0;
  50. return std::malloc(size);
  51. }
  52. void operator delete(void* p) throw()
  53. {
  54. if (p)
  55. std::free(p);
  56. }
  57. #if __cpp_sized_deallocation
  58. void operator delete(void* p, std::size_t) throw()
  59. { ::operator delete(p); }
  60. #endif
  61. void operator delete(void* p, const std::nothrow_t&) throw()
  62. {
  63. if (p)
  64. std::free(p);
  65. }
  66. #endif // _GLIBCXX_TESTSUITE_NEW_OPERATORS_H