| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/html_viewer/discardable_memory_allocator.h" | 5 #include "components/html_viewer/discardable_memory_allocator.h" |
| 6 | 6 |
| 7 #include "base/memory/discardable_memory.h" | 7 #include "base/memory/discardable_memory.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace html_viewer { | 10 namespace html_viewer { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const size_t kOneKilobyte = 1024; | 13 const size_t kOneKilobyte = 1024; |
| 14 const size_t kAlmostOneMegabyte = 1023 * kOneKilobyte; | 14 const size_t kAlmostOneMegabyte = 1023 * kOneKilobyte; |
| 15 const size_t kOneMegabyte = 1024 * kOneKilobyte; | 15 const size_t kOneMegabyte = 1024 * kOneKilobyte; |
| 16 | 16 |
| 17 TEST(DiscardableMemoryAllocator, Basic) { | 17 TEST(DiscardableMemoryAllocator, Basic) { |
| 18 DiscardableMemoryAllocator allocator(kOneMegabyte); |
| 18 scoped_ptr<base::DiscardableMemory> chunk; | 19 scoped_ptr<base::DiscardableMemory> chunk; |
| 20 // Make sure the chunk is locked when allocated. In debug mode, we will |
| 21 // dcheck. |
| 22 chunk = allocator.AllocateLockedDiscardableMemory(kOneKilobyte); |
| 23 chunk->Unlock(); |
| 19 | 24 |
| 20 { | 25 // Make sure we can lock a chunk. |
| 21 DiscardableMemoryAllocator allocator(kOneMegabyte); | 26 EXPECT_TRUE(chunk->Lock()); |
| 22 | 27 chunk->Unlock(); |
| 23 // Make sure the chunk is locked when allocated. In debug mode, we will | |
| 24 // dcheck. | |
| 25 chunk = allocator.AllocateLockedDiscardableMemory(kOneKilobyte); | |
| 26 chunk->Unlock(); | |
| 27 | |
| 28 // Make sure we can lock a chunk. | |
| 29 EXPECT_TRUE(chunk->Lock()); | |
| 30 chunk->Unlock(); | |
| 31 } | |
| 32 | |
| 33 // The chunk's backing should have disappeared with the allocator. | |
| 34 EXPECT_FALSE(chunk->Lock()); | |
| 35 } | 28 } |
| 36 | 29 |
| 37 TEST(DiscardableMemoryAllocator, DiscardChunks) { | 30 TEST(DiscardableMemoryAllocator, DiscardChunks) { |
| 38 DiscardableMemoryAllocator allocator(kOneMegabyte); | 31 DiscardableMemoryAllocator allocator(kOneMegabyte); |
| 39 | 32 |
| 40 scoped_ptr<base::DiscardableMemory> chunk_to_remove = | 33 scoped_ptr<base::DiscardableMemory> chunk_to_remove = |
| 41 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); | 34 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); |
| 42 chunk_to_remove->Unlock(); | 35 chunk_to_remove->Unlock(); |
| 43 | 36 |
| 44 // Allocating a second chunk should deallocate the first one due to memory | 37 // Allocating a second chunk should deallocate the first one due to memory |
| (...skipping 22 matching lines...) Expand all Loading... |
| 67 EXPECT_NE(nullptr, chunk_two->data()); | 60 EXPECT_NE(nullptr, chunk_two->data()); |
| 68 EXPECT_NE(nullptr, chunk_three->data()); | 61 EXPECT_NE(nullptr, chunk_three->data()); |
| 69 | 62 |
| 70 chunk_one->Unlock(); | 63 chunk_one->Unlock(); |
| 71 chunk_two->Unlock(); | 64 chunk_two->Unlock(); |
| 72 chunk_three->Unlock(); | 65 chunk_three->Unlock(); |
| 73 } | 66 } |
| 74 | 67 |
| 75 } // namespace | 68 } // namespace |
| 76 } // namespace html_viewer | 69 } // namespace html_viewer |
| OLD | NEW |