Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/child/child_discardable_shared_memory_manager.h" | 5 #include "content/child/child_discardable_shared_memory_manager.h" |
| 6 | 6 |
| 7 #include "base/memory/discardable_shared_memory.h" | 7 #include "base/memory/discardable_shared_memory.h" |
| 8 #include "base/process/process_metrics.h" | 8 #include "base/process/process_metrics.h" |
| 9 #include "content/child/child_thread.h" | 9 #include "content/child/child_thread.h" |
| 10 #include "content/common/child_process_messages.h" | 10 #include "content/common/child_process_messages.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 | 65 |
| 66 for (;;) { | 66 for (;;) { |
| 67 // Search free list for available space. | 67 // Search free list for available space. |
| 68 scoped_ptr<DiscardableSharedMemoryHeap::Span> free_span = | 68 scoped_ptr<DiscardableSharedMemoryHeap::Span> free_span = |
| 69 heap_.SearchFreeList(pages); | 69 heap_.SearchFreeList(pages); |
| 70 if (!free_span.get()) | 70 if (!free_span.get()) |
| 71 break; | 71 break; |
| 72 | 72 |
| 73 // Attempt to lock |free_span|. Delete span and search free list again | 73 // Attempt to lock |free_span|. Delete span and search free list again |
| 74 // if locking failed. | 74 // if locking failed. |
| 75 if (!free_span->shared_memory()->Lock( | 75 if (free_span->shared_memory()->Lock( |
| 76 free_span->start() * base::GetPageSize() - | 76 free_span->start() * base::GetPageSize() - |
| 77 reinterpret_cast<size_t>(free_span->shared_memory()->memory()), | 77 reinterpret_cast<size_t>(free_span->shared_memory()->memory()), |
| 78 free_span->length() * base::GetPageSize())) { | 78 free_span->length() * base::GetPageSize()) != |
| 79 base::DiscardableSharedMemory::SUCCESS) { | |
| 79 heap_.DeleteSpan(free_span.Pass()); | 80 heap_.DeleteSpan(free_span.Pass()); |
| 80 continue; | 81 continue; |
| 81 } | 82 } |
| 82 | 83 |
| 83 return make_scoped_ptr( | 84 return make_scoped_ptr( |
| 84 new DiscardableMemoryShmemChunkImpl(this, free_span.Pass())); | 85 new DiscardableMemoryShmemChunkImpl(this, free_span.Pass())); |
| 85 } | 86 } |
| 86 | 87 |
| 87 size_t pages_to_allocate = | 88 size_t pages_to_allocate = |
| 88 std::max(kAllocationSize / base::GetPageSize(), pages); | 89 std::max(kAllocationSize / base::GetPageSize(), pages); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 107 heap_.MergeIntoFreeList(leftover.Pass()); | 108 heap_.MergeIntoFreeList(leftover.Pass()); |
| 108 } | 109 } |
| 109 | 110 |
| 110 return make_scoped_ptr( | 111 return make_scoped_ptr( |
| 111 new DiscardableMemoryShmemChunkImpl(this, new_span.Pass())); | 112 new DiscardableMemoryShmemChunkImpl(this, new_span.Pass())); |
| 112 } | 113 } |
| 113 | 114 |
| 114 bool ChildDiscardableSharedMemoryManager::LockSpan( | 115 bool ChildDiscardableSharedMemoryManager::LockSpan( |
| 115 DiscardableSharedMemoryHeap::Span* span) { | 116 DiscardableSharedMemoryHeap::Span* span) { |
| 116 base::AutoLock lock(lock_); | 117 base::AutoLock lock(lock_); |
| 117 return span->shared_memory()->Lock( | 118 |
| 118 span->start() * base::GetPageSize() - | 119 size_t offset = span->start() * base::GetPageSize() - |
| 119 reinterpret_cast<size_t>(span->shared_memory()->memory()), | 120 reinterpret_cast<size_t>(span->shared_memory()->memory()); |
| 120 span->length() * base::GetPageSize()); | 121 size_t length = span->length() * base::GetPageSize(); |
| 122 | |
| 123 auto result = span->shared_memory()->Lock(offset, length); | |
| 124 if (result == base::DiscardableSharedMemory::SUCCESS) | |
|
danakj
2015/01/24 00:16:06
switch instead of series of ifs?
reveman
2015/01/24 00:27:48
Good idea. Done.
| |
| 125 return true; | |
| 126 if (result == base::DiscardableSharedMemory::PURGED) | |
| 127 span->shared_memory()->Unlock(offset, length); | |
| 128 | |
| 129 return false; | |
| 121 } | 130 } |
| 122 | 131 |
| 123 void ChildDiscardableSharedMemoryManager::UnlockSpan( | 132 void ChildDiscardableSharedMemoryManager::UnlockSpan( |
| 124 DiscardableSharedMemoryHeap::Span* span) { | 133 DiscardableSharedMemoryHeap::Span* span) { |
| 125 base::AutoLock lock(lock_); | 134 base::AutoLock lock(lock_); |
| 126 return span->shared_memory()->Unlock( | 135 |
| 127 span->start() * base::GetPageSize() - | 136 size_t offset = span->start() * base::GetPageSize() - |
| 128 reinterpret_cast<size_t>(span->shared_memory()->memory()), | 137 reinterpret_cast<size_t>(span->shared_memory()->memory()); |
| 129 span->length() * base::GetPageSize()); | 138 size_t length = span->length() * base::GetPageSize(); |
| 139 | |
| 140 return span->shared_memory()->Unlock(offset, length); | |
| 130 } | 141 } |
| 131 | 142 |
| 132 bool ChildDiscardableSharedMemoryManager::IsSpanResident( | 143 bool ChildDiscardableSharedMemoryManager::IsSpanResident( |
| 133 DiscardableSharedMemoryHeap::Span* span) const { | 144 DiscardableSharedMemoryHeap::Span* span) const { |
| 134 base::AutoLock lock(lock_); | 145 base::AutoLock lock(lock_); |
| 135 return span->shared_memory()->IsMemoryResident(); | 146 return span->shared_memory()->IsMemoryResident(); |
| 136 } | 147 } |
| 137 | 148 |
| 138 void ChildDiscardableSharedMemoryManager::ReleaseSpan( | 149 void ChildDiscardableSharedMemoryManager::ReleaseSpan( |
| 139 scoped_ptr<DiscardableSharedMemoryHeap::Span> span) { | 150 scoped_ptr<DiscardableSharedMemoryHeap::Span> span) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 161 sender_->Send( | 172 sender_->Send( |
| 162 new ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory( | 173 new ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory( |
| 163 size, &handle)); | 174 size, &handle)); |
| 164 scoped_ptr<base::DiscardableSharedMemory> memory( | 175 scoped_ptr<base::DiscardableSharedMemory> memory( |
| 165 new base::DiscardableSharedMemory(handle)); | 176 new base::DiscardableSharedMemory(handle)); |
| 166 CHECK(memory->Map(size)); | 177 CHECK(memory->Map(size)); |
| 167 return memory.Pass(); | 178 return memory.Pass(); |
| 168 } | 179 } |
| 169 | 180 |
| 170 } // namespace content | 181 } // namespace content |
| OLD | NEW |