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

Side by Side Diff: base/memory/discardable_memory_android.cc

Issue 261993003: base: Use DiscardableMemoryManager on MacOSX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix initial return value of AllocateAndAcquireLock and CHECKs for allocation failure Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | base/memory/discardable_memory_emulated.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "base/memory/discardable_memory.h" 5 #include "base/memory/discardable_memory.h"
6 6
7 #include "base/android/sys_utils.h" 7 #include "base/android/sys_utils.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/discardable_memory_ashmem.h" 12 #include "base/memory/discardable_memory_ashmem.h"
13 #include "base/memory/discardable_memory_ashmem_allocator.h" 13 #include "base/memory/discardable_memory_ashmem_allocator.h"
14 #include "base/memory/discardable_memory_emulated.h" 14 #include "base/memory/discardable_memory_emulated.h"
15 #include "base/memory/discardable_memory_malloc.h" 15 #include "base/memory/discardable_memory_malloc.h"
16 16
17 namespace base { 17 namespace base {
18 namespace { 18 namespace {
19 19
20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; 20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator";
21 21
22 // When ashmem is used, have the DiscardableMemoryManager trigger userspace 22 // For Ashmem, have the DiscardableMemoryManager trigger userspace eviction
23 // eviction when address space usage gets too high (e.g. 512 MBytes). 23 // when address space usage gets too high (e.g. 512 MBytes).
24 const size_t kAshmemMaxAddressSpaceUsage = 512 * 1024 * 1024; 24 const size_t kAshmemMemoryLimit = 512 * 1024 * 1024;
25 25
26 // Holds the state used for ashmem allocations. 26 size_t GetOptimalAshmemRegionSizeForAllocator() {
27 struct AshmemGlobalContext { 27 // Note that this may do some I/O (without hitting the disk though) so it
28 AshmemGlobalContext() 28 // should not be called on the critical path.
29 : allocator(kAshmemAllocatorName, 29 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8;
30 GetOptimalAshmemRegionSizeForAllocator()) { 30 }
31 manager.SetMemoryLimit(kAshmemMaxAddressSpaceUsage);
32 }
33 31
32 // Holds the shared state used for allocations.
33 struct SharedState {
34 SharedState()
35 : manager(kAshmemMemoryLimit, kAshmemMemoryLimit),
36 allocator(kAshmemAllocatorName,
37 GetOptimalAshmemRegionSizeForAllocator()) {}
38
39 internal::DiscardableMemoryManager manager;
34 internal::DiscardableMemoryAshmemAllocator allocator; 40 internal::DiscardableMemoryAshmemAllocator allocator;
35 internal::DiscardableMemoryManager manager;
36
37 private:
38 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes...
39 static size_t GetOptimalAshmemRegionSizeForAllocator() {
40 // Note that this may do some I/O (without hitting the disk though) so it
41 // should not be called on the critical path.
42 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8;
43 }
44 }; 41 };
45 42 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
46 LazyInstance<AshmemGlobalContext>::Leaky g_context = LAZY_INSTANCE_INITIALIZER;
47 43
48 } // namespace 44 } // namespace
49 45
50 // static 46 // static
51 void DiscardableMemory::RegisterMemoryPressureListeners() { 47 void DiscardableMemory::RegisterMemoryPressureListeners() {
52 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); 48 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners();
53 } 49 }
54 50
55 // static 51 // static
56 void DiscardableMemory::UnregisterMemoryPressureListeners() { 52 void DiscardableMemory::UnregisterMemoryPressureListeners() {
(...skipping 12 matching lines...) Expand all
69 } 65 }
70 66
71 // static 67 // static
72 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( 68 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
73 DiscardableMemoryType type, size_t size) { 69 DiscardableMemoryType type, size_t size) {
74 switch (type) { 70 switch (type) {
75 case DISCARDABLE_MEMORY_TYPE_NONE: 71 case DISCARDABLE_MEMORY_TYPE_NONE:
76 case DISCARDABLE_MEMORY_TYPE_MAC: 72 case DISCARDABLE_MEMORY_TYPE_MAC:
77 return scoped_ptr<DiscardableMemory>(); 73 return scoped_ptr<DiscardableMemory>();
78 case DISCARDABLE_MEMORY_TYPE_ASHMEM: { 74 case DISCARDABLE_MEMORY_TYPE_ASHMEM: {
79 AshmemGlobalContext* const global_context = g_context.Pointer(); 75 SharedState* const shared_state = g_shared_state.Pointer();
80 scoped_ptr<internal::DiscardableMemoryAshmem> memory( 76 scoped_ptr<internal::DiscardableMemoryAshmem> memory(
81 new internal::DiscardableMemoryAshmem( 77 new internal::DiscardableMemoryAshmem(
82 size, &global_context->allocator, &global_context->manager)); 78 size, &shared_state->allocator, &shared_state->manager));
83 if (!memory->Initialize()) 79 if (!memory->Initialize())
84 return scoped_ptr<DiscardableMemory>(); 80 return scoped_ptr<DiscardableMemory>();
85 81
86 return memory.PassAs<DiscardableMemory>(); 82 return memory.PassAs<DiscardableMemory>();
87 } 83 }
88 case DISCARDABLE_MEMORY_TYPE_EMULATED: { 84 case DISCARDABLE_MEMORY_TYPE_EMULATED: {
89 scoped_ptr<internal::DiscardableMemoryEmulated> memory( 85 scoped_ptr<internal::DiscardableMemoryEmulated> memory(
90 new internal::DiscardableMemoryEmulated(size)); 86 new internal::DiscardableMemoryEmulated(size));
91 if (!memory->Initialize()) 87 if (!memory->Initialize())
92 return scoped_ptr<DiscardableMemory>(); 88 return scoped_ptr<DiscardableMemory>();
93 89
94 return memory.PassAs<DiscardableMemory>(); 90 return memory.PassAs<DiscardableMemory>();
95 } 91 }
96 case DISCARDABLE_MEMORY_TYPE_MALLOC: { 92 case DISCARDABLE_MEMORY_TYPE_MALLOC: {
97 scoped_ptr<internal::DiscardableMemoryMalloc> memory( 93 scoped_ptr<internal::DiscardableMemoryMalloc> memory(
98 new internal::DiscardableMemoryMalloc(size)); 94 new internal::DiscardableMemoryMalloc(size));
99 if (!memory->Initialize()) 95 if (!memory->Initialize())
100 return scoped_ptr<DiscardableMemory>(); 96 return scoped_ptr<DiscardableMemory>();
101 97
102 return memory.PassAs<DiscardableMemory>(); 98 return memory.PassAs<DiscardableMemory>();
103 } 99 }
104 } 100 }
105 101
106 NOTREACHED(); 102 NOTREACHED();
107 return scoped_ptr<DiscardableMemory>(); 103 return scoped_ptr<DiscardableMemory>();
108 } 104 }
109 105
110 // static 106 // static
111 void DiscardableMemory::PurgeForTesting() { 107 void DiscardableMemory::PurgeForTesting() {
112 g_context.Pointer()->manager.PurgeAll(); 108 g_shared_state.Pointer()->manager.PurgeAll();
113 internal::DiscardableMemoryEmulated::PurgeForTesting(); 109 internal::DiscardableMemoryEmulated::PurgeForTesting();
114 } 110 }
115 111
116 } // namespace base 112 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/memory/discardable_memory_emulated.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698