 Chromium Code Reviews
 Chromium Code Reviews Issue 1016493002:
  Add a DiscardableMemoryShmemeAllocator.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1016493002:
  Add a DiscardableMemoryShmemeAllocator.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/services/html_viewer/discardable_memory_allocator.h" | |
| 6 | |
| 7 #include "base/memory/discardable_memory.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace { | |
| 11 | |
| 12 const size_t kOneKilobyte = 1024; | |
| 13 const size_t kAlmostOneMegabyte = 1023 * kOneKilobyte; | |
| 14 const size_t kOneMegabyte = 1024 * kOneKilobyte; | |
| 15 | |
| 16 } // namespace | |
| 17 | |
| 18 TEST(DiscardableMemoryAllocator, Basic) { | |
| 
jamesr
2015/03/19 22:09:56
put this in an anonymous namespace. i've had ODR v
 | |
| 19 scoped_ptr<base::DiscardableMemory> chunk; | |
| 20 | |
| 21 { | |
| 22 html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte); | |
| 
jamesr
2015/03/19 22:09:56
if you wrap the whole file in
namespace html_view
 | |
| 23 | |
| 24 // Make sure the chunk is locked when allocated. In debug mode, we will | |
| 25 // dcheck. | |
| 26 chunk = allocator.AllocateLockedDiscardableMemory(kOneKilobyte); | |
| 27 chunk->Unlock(); | |
| 28 | |
| 29 // Make sure we can lock a chunk. | |
| 30 EXPECT_TRUE(chunk->Lock()); | |
| 31 chunk->Unlock(); | |
| 32 } | |
| 33 | |
| 34 // The chunk's backing should have disappeared with the allocator. | |
| 35 EXPECT_FALSE(chunk->Lock()); | |
| 36 } | |
| 37 | |
| 38 TEST(DiscardableMemoryAllocator, DiscardChunks) { | |
| 39 html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte); | |
| 40 | |
| 41 scoped_ptr<base::DiscardableMemory> chunk_to_remove = | |
| 42 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); | |
| 43 chunk_to_remove->Unlock(); | |
| 44 | |
| 45 // Allocating a second chunk should deallocate the first one due to memory | |
| 46 // pressure, since we only have one megabyte available. | |
| 47 scoped_ptr<base::DiscardableMemory> chunk_to_keep = | |
| 48 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); | |
| 49 | |
| 50 // Fail to get a lock because allocating the second chunk removed the first. | |
| 51 EXPECT_FALSE(chunk_to_remove->Lock()); | |
| 52 | |
| 53 chunk_to_keep->Unlock(); | |
| 54 } | |
| 55 | |
| 56 TEST(DiscardableMemoryAllocator, DontDiscardLiveChunks) { | |
| 57 html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte); | |
| 58 | |
| 59 scoped_ptr<base::DiscardableMemory> chunk_one = | |
| 60 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); | |
| 61 scoped_ptr<base::DiscardableMemory> chunk_two = | |
| 62 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); | |
| 63 scoped_ptr<base::DiscardableMemory> chunk_three = | |
| 64 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte); | |
| 65 | |
| 66 // These accesses will fail if the underlying weak ptr has been deallocated. | |
| 67 EXPECT_NE(nullptr, chunk_one->Memory()); | |
| 68 EXPECT_NE(nullptr, chunk_two->Memory()); | |
| 69 EXPECT_NE(nullptr, chunk_three->Memory()); | |
| 70 | |
| 71 chunk_one->Unlock(); | |
| 72 chunk_two->Unlock(); | |
| 73 chunk_three->Unlock(); | |
| 74 } | |
| OLD | NEW |