new_replace.pass.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // test operator new replacement
  10. // UNSUPPORTED: sanitizer-new-delete
  11. #include <new>
  12. #include <cstddef>
  13. #include <cstdlib>
  14. #include <cassert>
  15. #include <limits>
  16. #include "test_macros.h"
  17. int new_called = 0;
  18. void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
  19. {
  20. ++new_called;
  21. void* ret = std::malloc(s);
  22. if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
  23. return ret;
  24. }
  25. void operator delete(void* p) TEST_NOEXCEPT
  26. {
  27. --new_called;
  28. std::free(p);
  29. }
  30. bool A_constructed = false;
  31. struct A
  32. {
  33. A() {A_constructed = true;}
  34. ~A() {A_constructed = false;}
  35. };
  36. int main()
  37. {
  38. A* ap = new A;
  39. assert(ap);
  40. assert(A_constructed);
  41. assert(new_called);
  42. delete ap;
  43. assert(!A_constructed);
  44. assert(!new_called);
  45. }