stack_container_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "butil/containers/stack_container.h"
  5. #include <algorithm>
  6. #include "butil/memory/aligned_memory.h"
  7. #include "butil/memory/ref_counted.h"
  8. #include <gtest/gtest.h>
  9. namespace butil {
  10. namespace {
  11. class Dummy : public butil::RefCounted<Dummy> {
  12. public:
  13. explicit Dummy(int* alive) : alive_(alive) {
  14. ++*alive_;
  15. }
  16. private:
  17. friend class butil::RefCounted<Dummy>;
  18. ~Dummy() {
  19. --*alive_;
  20. }
  21. int* const alive_;
  22. };
  23. } // namespace
  24. TEST(StackContainer, Vector) {
  25. const int stack_size = 3;
  26. StackVector<int, stack_size> vect;
  27. const int* stack_buffer = &vect.stack_data().stack_buffer()[0];
  28. // The initial |stack_size| elements should appear in the stack buffer.
  29. EXPECT_EQ(static_cast<size_t>(stack_size), vect.container().capacity());
  30. for (int i = 0; i < stack_size; i++) {
  31. vect.container().push_back(i);
  32. EXPECT_EQ(stack_buffer, &vect.container()[0]);
  33. EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
  34. }
  35. // Adding more elements should push the array onto the heap.
  36. for (int i = 0; i < stack_size; i++) {
  37. vect.container().push_back(i + stack_size);
  38. EXPECT_NE(stack_buffer, &vect.container()[0]);
  39. EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
  40. }
  41. // The array should still be in order.
  42. for (int i = 0; i < stack_size * 2; i++)
  43. EXPECT_EQ(i, vect.container()[i]);
  44. // Resize to smaller. Our STL implementation won't reallocate in this case,
  45. // otherwise it might use our stack buffer. We reserve right after the resize
  46. // to guarantee it isn't using the stack buffer, even though it doesn't have
  47. // much data.
  48. vect.container().resize(stack_size);
  49. vect.container().reserve(stack_size * 2);
  50. EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
  51. // Copying the small vector to another should use the same allocator and use
  52. // the now-unused stack buffer. GENERALLY CALLERS SHOULD NOT DO THIS since
  53. // they have to get the template types just right and it can cause errors.
  54. std::vector<int, StackAllocator<int, stack_size> > other(vect.container());
  55. EXPECT_EQ(stack_buffer, &other.front());
  56. EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
  57. for (int i = 0; i < stack_size; i++)
  58. EXPECT_EQ(i, other[i]);
  59. }
  60. TEST(StackContainer, VectorDoubleDelete) {
  61. // Regression testing for double-delete.
  62. typedef StackVector<scoped_refptr<Dummy>, 2> Vector;
  63. typedef Vector::ContainerType Container;
  64. Vector vect;
  65. int alive = 0;
  66. scoped_refptr<Dummy> dummy(new Dummy(&alive));
  67. EXPECT_EQ(alive, 1);
  68. vect->push_back(dummy);
  69. EXPECT_EQ(alive, 1);
  70. Dummy* dummy_unref = dummy.get();
  71. dummy = NULL;
  72. EXPECT_EQ(alive, 1);
  73. Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref);
  74. EXPECT_EQ(itr->get(), dummy_unref);
  75. vect->erase(itr);
  76. EXPECT_EQ(alive, 0);
  77. // Shouldn't crash at exit.
  78. }
  79. namespace {
  80. template <size_t alignment>
  81. class AlignedData {
  82. public:
  83. AlignedData() { memset(data_.void_data(), 0, alignment); }
  84. ~AlignedData() {}
  85. butil::AlignedMemory<alignment, alignment> data_;
  86. };
  87. } // anonymous namespace
  88. #define EXPECT_ALIGNED(ptr, align) \
  89. EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
  90. // stack alignment is buggy before gcc 4.6
  91. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=16660
  92. #if defined(COMPILER_GCC) && \
  93. ( __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  94. #define GOOD_GCC_STACK_ALIGNMENT
  95. #endif
  96. TEST(StackContainer, BufferAlignment) {
  97. StackVector<wchar_t, 16> text;
  98. text->push_back(L'A');
  99. EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t));
  100. StackVector<double, 1> doubles;
  101. doubles->push_back(0.0);
  102. EXPECT_ALIGNED(&doubles[0], ALIGNOF(double));
  103. StackVector<AlignedData<16>, 1> aligned16;
  104. aligned16->push_back(AlignedData<16>());
  105. EXPECT_ALIGNED(&aligned16[0], 16);
  106. #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY) && \
  107. defined(GOOD_GCC_STACK_ALIGNMENT)
  108. // It seems that non-X86 gcc doesn't respect greater than 16 byte alignment.
  109. // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33721 for details.
  110. // TODO(sbc):re-enable this if GCC starts respecting higher alignments.
  111. StackVector<AlignedData<256>, 1> aligned256;
  112. aligned256->push_back(AlignedData<256>());
  113. EXPECT_ALIGNED(&aligned256[0], 256);
  114. #endif
  115. }
  116. template class StackVector<int, 2>;
  117. template class StackVector<scoped_refptr<Dummy>, 2>;
  118. } // namespace butil