ArenaSmartPtrTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * @author: Marcelo Juchem <marcelo@fb.com>
  18. */
  19. #include <folly/Memory.h>
  20. #include <folly/memory/Arena.h>
  21. #include <folly/portability/GTest.h>
  22. using namespace folly;
  23. struct global_counter {
  24. global_counter() : count_(0) {}
  25. void increase() {
  26. ++count_;
  27. }
  28. void decrease() {
  29. EXPECT_GT(count_, 0);
  30. --count_;
  31. }
  32. unsigned count() const {
  33. return count_;
  34. }
  35. private:
  36. unsigned count_;
  37. };
  38. struct Foo {
  39. explicit Foo(global_counter& counter) : counter_(counter) {
  40. counter_.increase();
  41. }
  42. ~Foo() {
  43. counter_.decrease();
  44. }
  45. private:
  46. global_counter& counter_;
  47. };
  48. template <typename Allocator>
  49. void unique_ptr_test(Allocator& allocator) {
  50. using ptr_type = std::unique_ptr<Foo, allocator_delete<Allocator>>;
  51. global_counter counter;
  52. EXPECT_EQ(counter.count(), 0);
  53. Foo* foo = nullptr;
  54. {
  55. auto p = folly::allocate_unique<Foo>(allocator, counter);
  56. EXPECT_EQ(counter.count(), 1);
  57. p.reset();
  58. EXPECT_EQ(counter.count(), 0);
  59. p = folly::allocate_unique<Foo>(allocator, counter);
  60. EXPECT_EQ(counter.count(), 1);
  61. foo = p.release();
  62. EXPECT_EQ(counter.count(), 1);
  63. }
  64. EXPECT_EQ(counter.count(), 1);
  65. {
  66. auto p = folly::allocate_unique<Foo>(allocator, counter);
  67. EXPECT_EQ(counter.count(), 2);
  68. [&](ptr_type g) {
  69. EXPECT_EQ(counter.count(), 2);
  70. g.reset();
  71. EXPECT_EQ(counter.count(), 1);
  72. }(std::move(p));
  73. }
  74. EXPECT_EQ(counter.count(), 1);
  75. std::allocator_traits<Allocator>::destroy(allocator, foo);
  76. EXPECT_EQ(counter.count(), 0);
  77. }
  78. TEST(ArenaSmartPtr, unique_ptr_SysArena) {
  79. SysArena arena;
  80. SysArenaAllocator<Foo> alloc(arena);
  81. unique_ptr_test(alloc);
  82. }
  83. template <typename Allocator>
  84. void shared_ptr_test(Allocator& allocator) {
  85. typedef std::shared_ptr<Foo> ptr_type;
  86. global_counter counter;
  87. EXPECT_EQ(counter.count(), 0);
  88. ptr_type foo;
  89. EXPECT_EQ(counter.count(), 0);
  90. EXPECT_EQ(foo.use_count(), 0);
  91. {
  92. auto p = std::allocate_shared<Foo>(allocator, counter);
  93. EXPECT_EQ(counter.count(), 1);
  94. EXPECT_EQ(p.use_count(), 1);
  95. p.reset();
  96. EXPECT_EQ(counter.count(), 0);
  97. EXPECT_EQ(p.use_count(), 0);
  98. p = std::allocate_shared<Foo>(allocator, counter);
  99. EXPECT_EQ(counter.count(), 1);
  100. EXPECT_EQ(p.use_count(), 1);
  101. foo = p;
  102. EXPECT_EQ(p.use_count(), 2);
  103. }
  104. EXPECT_EQ(counter.count(), 1);
  105. EXPECT_EQ(foo.use_count(), 1);
  106. {
  107. auto p = foo;
  108. EXPECT_EQ(counter.count(), 1);
  109. EXPECT_EQ(p.use_count(), 2);
  110. [&](ptr_type g) {
  111. EXPECT_EQ(counter.count(), 1);
  112. EXPECT_EQ(p.use_count(), 3);
  113. EXPECT_EQ(g.use_count(), 3);
  114. g.reset();
  115. EXPECT_EQ(counter.count(), 1);
  116. EXPECT_EQ(p.use_count(), 2);
  117. EXPECT_EQ(g.use_count(), 0);
  118. }(p);
  119. EXPECT_EQ(counter.count(), 1);
  120. EXPECT_EQ(p.use_count(), 2);
  121. }
  122. EXPECT_EQ(counter.count(), 1);
  123. EXPECT_EQ(foo.use_count(), 1);
  124. foo.reset();
  125. EXPECT_EQ(counter.count(), 0);
  126. EXPECT_EQ(foo.use_count(), 0);
  127. }
  128. TEST(ArenaSmartPtr, shared_ptr_SysArena) {
  129. SysArena arena;
  130. SysArenaAllocator<Foo> alloc(arena);
  131. shared_ptr_test(alloc);
  132. }
  133. int main(int argc, char* argv[]) {
  134. testing::InitGoogleTest(&argc, argv);
  135. return RUN_ALL_TESTS();
  136. }