allocate.pass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <memory>
  9. // allocator:
  10. // pointer allocate(size_type n, allocator<void>::const_pointer hint=0);
  11. #include <memory>
  12. #include <cassert>
  13. #include <cstddef> // for std::max_align_t
  14. #include "test_macros.h"
  15. #include "count_new.h"
  16. #ifdef TEST_HAS_NO_ALIGNED_ALLOCATION
  17. static const bool UsingAlignedNew = false;
  18. #else
  19. static const bool UsingAlignedNew = true;
  20. #endif
  21. #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
  22. static const size_t MaxAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__;
  23. #else
  24. static const size_t MaxAligned = std::alignment_of<std::max_align_t>::value;
  25. #endif
  26. static const size_t OverAligned = MaxAligned * 2;
  27. template <size_t Align>
  28. struct TEST_ALIGNAS(Align) AlignedType {
  29. char data;
  30. static int constructed;
  31. AlignedType() { ++constructed; }
  32. AlignedType(AlignedType const&) { ++constructed; }
  33. ~AlignedType() { --constructed; }
  34. };
  35. template <size_t Align>
  36. int AlignedType<Align>::constructed = 0;
  37. template <size_t Align>
  38. void test_aligned() {
  39. typedef AlignedType<Align> T;
  40. T::constructed = 0;
  41. globalMemCounter.reset();
  42. std::allocator<T> a;
  43. const bool IsOverAlignedType = Align > MaxAligned;
  44. const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew;
  45. {
  46. assert(globalMemCounter.checkOutstandingNewEq(0));
  47. assert(T::constructed == 0);
  48. globalMemCounter.last_new_size = 0;
  49. globalMemCounter.last_new_align = 0;
  50. T* ap = a.allocate(3);
  51. DoNotOptimize(ap);
  52. assert(globalMemCounter.checkOutstandingNewEq(1));
  53. assert(globalMemCounter.checkNewCalledEq(1));
  54. assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
  55. assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(T)));
  56. assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
  57. assert(T::constructed == 0);
  58. globalMemCounter.last_delete_align = 0;
  59. a.deallocate(ap, 3);
  60. assert(globalMemCounter.checkOutstandingNewEq(0));
  61. assert(globalMemCounter.checkDeleteCalledEq(1));
  62. assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
  63. assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
  64. assert(T::constructed == 0);
  65. }
  66. globalMemCounter.reset();
  67. {
  68. globalMemCounter.last_new_size = 0;
  69. globalMemCounter.last_new_align = 0;
  70. T* volatile ap2 = a.allocate(11, (const void*)5);
  71. DoNotOptimize(ap2);
  72. assert(globalMemCounter.checkOutstandingNewEq(1));
  73. assert(globalMemCounter.checkNewCalledEq(1));
  74. assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
  75. assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T)));
  76. assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
  77. assert(T::constructed == 0);
  78. globalMemCounter.last_delete_align = 0;
  79. a.deallocate(ap2, 11);
  80. DoNotOptimize(ap2);
  81. assert(globalMemCounter.checkOutstandingNewEq(0));
  82. assert(globalMemCounter.checkDeleteCalledEq(1));
  83. assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
  84. assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
  85. assert(T::constructed == 0);
  86. }
  87. }
  88. int main(int, char**) {
  89. test_aligned<1>();
  90. test_aligned<2>();
  91. test_aligned<4>();
  92. test_aligned<8>();
  93. test_aligned<16>();
  94. test_aligned<MaxAligned>();
  95. test_aligned<OverAligned>();
  96. test_aligned<OverAligned * 2>();
  97. return 0;
  98. }