| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_one_block_arena.h" | 5 #include "net/quic/quic_one_block_arena.h" |
| 6 | 6 |
| 7 #include "net/quic/interval_set.h" | 7 #include "net/quic/interval_set.h" |
| 8 #include "net/quic/quic_flags.h" | 8 #include "net/quic/quic_flags.h" |
| 9 #include "net/quic/quic_utils.h" | 9 #include "net/quic/quic_utils.h" |
| 10 #include "net/test/gtest_util.h" | 10 #include "net/test/gtest_util.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 static const uint32_t kMaxAlign = 8; | 17 static const uint32_t kMaxAlign = 8; |
| 18 | 18 |
| 19 struct TestObject { | 19 struct TestObject { |
| 20 uint32_t value; | 20 uint32_t value; |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 TEST(QuicOneBlockArenaTest, AllocateSuccess) { | 23 TEST(QuicOneBlockArenaTest, AllocateSuccess) { |
| 24 FLAGS_quic_enable_arena_allocation = true; | |
| 25 QuicOneBlockArena<1024> arena; | 24 QuicOneBlockArena<1024> arena; |
| 26 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); | 25 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); |
| 27 EXPECT_TRUE(ptr.is_from_arena()); | 26 EXPECT_TRUE(ptr.is_from_arena()); |
| 28 } | 27 } |
| 29 | 28 |
| 30 TEST(QuicOneBlockArenaTest, Disabled) { | |
| 31 FLAGS_quic_enable_arena_allocation = false; | |
| 32 QuicOneBlockArena<1024> arena; | |
| 33 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); | |
| 34 EXPECT_FALSE(ptr.is_from_arena()); | |
| 35 } | |
| 36 | |
| 37 TEST(QuicOneBlockArenaTest, Exhaust) { | 29 TEST(QuicOneBlockArenaTest, Exhaust) { |
| 38 FLAGS_quic_enable_arena_allocation = true; | |
| 39 QuicOneBlockArena<1024> arena; | 30 QuicOneBlockArena<1024> arena; |
| 40 for (size_t i = 0; i < 1024 / kMaxAlign; ++i) { | 31 for (size_t i = 0; i < 1024 / kMaxAlign; ++i) { |
| 41 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); | 32 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); |
| 42 EXPECT_TRUE(ptr.is_from_arena()); | 33 EXPECT_TRUE(ptr.is_from_arena()); |
| 43 } | 34 } |
| 44 QuicArenaScopedPtr<TestObject> ptr; | 35 QuicArenaScopedPtr<TestObject> ptr; |
| 45 EXPECT_DFATAL(ptr = arena.New<TestObject>(), | 36 EXPECT_DFATAL(ptr = arena.New<TestObject>(), |
| 46 "Ran out of space in QuicOneBlockArena"); | 37 "Ran out of space in QuicOneBlockArena"); |
| 47 EXPECT_FALSE(ptr.is_from_arena()); | 38 EXPECT_FALSE(ptr.is_from_arena()); |
| 48 } | 39 } |
| 49 | 40 |
| 50 TEST(QuicOneBlockArenaTest, NoOverlaps) { | 41 TEST(QuicOneBlockArenaTest, NoOverlaps) { |
| 51 FLAGS_quic_enable_arena_allocation = true; | |
| 52 QuicOneBlockArena<1024> arena; | 42 QuicOneBlockArena<1024> arena; |
| 53 std::vector<QuicArenaScopedPtr<TestObject>> objects; | 43 std::vector<QuicArenaScopedPtr<TestObject>> objects; |
| 54 IntervalSet<uintptr_t> used; | 44 IntervalSet<uintptr_t> used; |
| 55 for (size_t i = 0; i < 1024 / kMaxAlign; ++i) { | 45 for (size_t i = 0; i < 1024 / kMaxAlign; ++i) { |
| 56 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); | 46 QuicArenaScopedPtr<TestObject> ptr = arena.New<TestObject>(); |
| 57 EXPECT_TRUE(ptr.is_from_arena()); | 47 EXPECT_TRUE(ptr.is_from_arena()); |
| 58 | 48 |
| 59 uintptr_t begin = reinterpret_cast<uintptr_t>(ptr.get()); | 49 uintptr_t begin = reinterpret_cast<uintptr_t>(ptr.get()); |
| 60 uintptr_t end = begin + sizeof(TestObject); | 50 uintptr_t end = begin + sizeof(TestObject); |
| 61 EXPECT_FALSE(used.Contains(begin)); | 51 EXPECT_FALSE(used.Contains(begin)); |
| 62 EXPECT_FALSE(used.Contains(end - 1)); | 52 EXPECT_FALSE(used.Contains(end - 1)); |
| 63 used.Add(begin, end); | 53 used.Add(begin, end); |
| 64 } | 54 } |
| 65 } | 55 } |
| 66 | 56 |
| 67 } // namespace | 57 } // namespace |
| 68 } // namespace net | 58 } // namespace net |
| OLD | NEW |