allocate.pass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // <memory>
  10. // allocator:
  11. // pointer allocate(size_type n, allocator<void>::const_pointer hint=0);
  12. #include <memory>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "count_new.hpp"
  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* volatile ap = a.allocate(3);
  51. assert(globalMemCounter.checkOutstandingNewEq(1));
  52. assert(globalMemCounter.checkNewCalledEq(1));
  53. assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
  54. assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(T)));
  55. assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
  56. assert(T::constructed == 0);
  57. globalMemCounter.last_delete_align = 0;
  58. a.deallocate(ap, 3);
  59. assert(globalMemCounter.checkOutstandingNewEq(0));
  60. assert(globalMemCounter.checkDeleteCalledEq(1));
  61. assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
  62. assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
  63. assert(T::constructed == 0);
  64. }
  65. globalMemCounter.reset();
  66. {
  67. globalMemCounter.last_new_size = 0;
  68. globalMemCounter.last_new_align = 0;
  69. T* volatile ap2 = a.allocate(11, (const void*)5);
  70. assert(globalMemCounter.checkOutstandingNewEq(1));
  71. assert(globalMemCounter.checkNewCalledEq(1));
  72. assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
  73. assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T)));
  74. assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
  75. assert(T::constructed == 0);
  76. globalMemCounter.last_delete_align = 0;
  77. a.deallocate(ap2, 11);
  78. assert(globalMemCounter.checkOutstandingNewEq(0));
  79. assert(globalMemCounter.checkDeleteCalledEq(1));
  80. assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
  81. assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
  82. assert(T::constructed == 0);
  83. }
  84. }
  85. int main() {
  86. test_aligned<1>();
  87. test_aligned<2>();
  88. test_aligned<4>();
  89. test_aligned<8>();
  90. test_aligned<16>();
  91. test_aligned<MaxAligned>();
  92. test_aligned<OverAligned>();
  93. test_aligned<OverAligned * 2>();
  94. }