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

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

Issue 114923005: base: Discardable memory types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add switches::kUseDiscardableMemory to kForwardSwitches Created 6 years, 11 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 | « base/memory/discardable_memory_android.h ('k') | base/memory/discardable_memory_emulated.h » ('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_android.h" 5 #include "base/memory/discardable_memory_android.h"
6 6
7 #include <sys/mman.h> 7 #include <sys/mman.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 9
10 #include <limits> 10 #include <limits>
11 11
12 #include "base/android/sys_utils.h" 12 #include "base/android/sys_utils.h"
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/file_util.h" 15 #include "base/file_util.h"
16 #include "base/lazy_instance.h" 16 #include "base/lazy_instance.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/discardable_memory.h"
19 #include "base/memory/discardable_memory_allocator_android.h" 18 #include "base/memory/discardable_memory_allocator_android.h"
19 #include "base/memory/discardable_memory_emulated.h"
20 #include "third_party/ashmem/ashmem.h" 20 #include "third_party/ashmem/ashmem.h"
21 21
22 namespace base { 22 namespace base {
23 namespace { 23 namespace {
24 24
25 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; 25 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator";
26 26
27 struct DiscardableMemoryAllocatorWrapper { 27 struct DiscardableMemoryAllocatorWrapper {
28 DiscardableMemoryAllocatorWrapper() 28 DiscardableMemoryAllocatorWrapper()
29 : allocator(kAshmemAllocatorName, 29 : allocator(kAshmemAllocatorName,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 93
94 bool CloseAshmemRegion(int fd, size_t size, void* address) { 94 bool CloseAshmemRegion(int fd, size_t size, void* address) {
95 if (munmap(address, size) == -1) { 95 if (munmap(address, size) == -1) {
96 DPLOG(ERROR) << "Failed to unmap memory."; 96 DPLOG(ERROR) << "Failed to unmap memory.";
97 close(fd); 97 close(fd);
98 return false; 98 return false;
99 } 99 }
100 return close(fd) == 0; 100 return close(fd) == 0;
101 } 101 }
102 102
103 LockDiscardableMemoryStatus LockAshmemRegion(int fd, 103 DiscardableMemoryLockStatus LockAshmemRegion(int fd,
104 size_t off, 104 size_t off,
105 size_t size, 105 size_t size,
106 const void* address) { 106 const void* address) {
107 const int result = ashmem_pin_region(fd, off, size); 107 const int result = ashmem_pin_region(fd, off, size);
108 DCHECK_EQ(0, mprotect(address, size, PROT_READ | PROT_WRITE)); 108 DCHECK_EQ(0, mprotect(address, size, PROT_READ | PROT_WRITE));
109 return result == ASHMEM_WAS_PURGED ? 109 return result == ASHMEM_WAS_PURGED ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
110 DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; 110 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
111 } 111 }
112 112
113 bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) { 113 bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) {
114 const int failed = ashmem_unpin_region(fd, off, size); 114 const int failed = ashmem_unpin_region(fd, off, size);
115 if (failed) 115 if (failed)
116 DLOG(ERROR) << "Failed to unpin memory."; 116 DLOG(ERROR) << "Failed to unpin memory.";
117 // This allows us to catch accesses to unlocked memory. 117 // This allows us to catch accesses to unlocked memory.
118 DCHECK_EQ(0, mprotect(address, size, PROT_NONE)); 118 DCHECK_EQ(0, mprotect(address, size, PROT_NONE));
119 return !failed; 119 return !failed;
120 } 120 }
121 121
122 } // namespace internal 122 } // namespace internal
123 123
124 // static 124 // static
125 bool DiscardableMemory::SupportedNatively() { 125 void DiscardableMemory::GetSupportedTypes(
126 return true; 126 std::vector<DiscardableMemoryType>* types) {
127 const DiscardableMemoryType supported_types[] = {
128 DISCARDABLE_MEMORY_TYPE_ANDROID,
129 DISCARDABLE_MEMORY_TYPE_EMULATED
130 };
131 types->assign(supported_types, supported_types + arraysize(supported_types));
127 } 132 }
128 133
129 // static 134 // static
130 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( 135 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
131 size_t size) { 136 DiscardableMemoryType type, size_t size) {
132 return g_context.Pointer()->allocator.Allocate(size); 137 switch (type) {
138 case DISCARDABLE_MEMORY_TYPE_NONE:
139 case DISCARDABLE_MEMORY_TYPE_MAC:
140 return scoped_ptr<DiscardableMemory>();
141 case DISCARDABLE_MEMORY_TYPE_ANDROID: {
142 return g_context.Pointer()->allocator.Allocate(size);
143 }
144 case DISCARDABLE_MEMORY_TYPE_EMULATED: {
145 scoped_ptr<internal::DiscardableMemoryEmulated> memory(
146 new internal::DiscardableMemoryEmulated(size));
147 if (!memory->Initialize())
148 return scoped_ptr<DiscardableMemory>();
149
150 return memory.PassAs<DiscardableMemory>();
151 }
152 }
153
154 NOTREACHED();
155 return scoped_ptr<DiscardableMemory>();
133 } 156 }
134 157
135 // static 158 // static
136 bool DiscardableMemory::PurgeForTestingSupported() { 159 bool DiscardableMemory::PurgeForTestingSupported() {
137 return false; 160 return false;
138 } 161 }
139 162
140 // static 163 // static
141 void DiscardableMemory::PurgeForTesting() { 164 void DiscardableMemory::PurgeForTesting() {
142 NOTIMPLEMENTED(); 165 NOTIMPLEMENTED();
143 } 166 }
144 167
145 } // namespace base 168 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_memory_android.h ('k') | base/memory/discardable_memory_emulated.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698