Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: mojo/services/html_viewer/discardable_memory_allocator_unittest.cc

Issue 1016493002: Add a DiscardableMemoryShmemeAllocator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: And fix the unittests Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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) {
19 scoped_ptr<base::DiscardableMemory> chunk;
20
21 {
22 html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte);
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
54 TEST(DiscardableMemoryAllocator, DontDiscardLiveChunks) {
55 html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte);
56
57 scoped_ptr<base::DiscardableMemory> chunk_one =
58 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
59 scoped_ptr<base::DiscardableMemory> chunk_two =
60 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
61 scoped_ptr<base::DiscardableMemory> chunk_three =
62 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
63
64 // These accesses will fail if the underlying weak ptr has been deallocated.
65 EXPECT_NE(nullptr, chunk_one->Memory());
66 EXPECT_NE(nullptr, chunk_two->Memory());
67 EXPECT_NE(nullptr, chunk_three->Memory());
jamesr 2015/03/18 22:40:14 please also unlock one of these and see what happe
68 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698