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

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

Issue 195863005: Use DiscardableMemoryManager on Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix DiscardableMemoryAshmem + enable unit test on Android that exposes the bug 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/memory/discardable_memory_ashmem.h"
6
7 #include "base/memory/discardable_memory_ashmem_allocator.h"
8
9 namespace base {
10 namespace internal {
11
12 DiscardableMemoryAshmem::DiscardableMemoryAshmem(
13 size_t bytes,
14 DiscardableMemoryAshmemAllocator* allocator,
15 DiscardableMemoryManager* manager)
16 : bytes_(bytes),
17 allocator_(allocator),
18 manager_(manager) {
19 manager_->Register(this, bytes_);
20 }
21
22 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() {
23 manager_->Unregister(this);
24 }
25
26 bool DiscardableMemoryAshmem::Initialize() {
27 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED;
28 }
29
30 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() {
31 bool purged = false;
32 if (!manager_->AcquireLock(this, &purged))
33 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
34
35 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
36 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
37 }
38
39 void DiscardableMemoryAshmem::Unlock() {
40 manager_->ReleaseLock(this);
41 }
42
43 void* DiscardableMemoryAshmem::Memory() const {
44 DCHECK(ashmem_chunk_);
45 return ashmem_chunk_->Memory();
46 }
47
48 bool DiscardableMemoryAshmem::AllocateAndAcquireLock() {
49 if (ashmem_chunk_)
50 return ashmem_chunk_->Lock();
51
52 ashmem_chunk_ = allocator_->Allocate(bytes_);
53 return false;
54 }
55
56 void DiscardableMemoryAshmem::ReleaseLock() {
57 ashmem_chunk_->Unlock();
58 }
59
60 void DiscardableMemoryAshmem::Purge() {
61 ashmem_chunk_.reset();
62 }
63
64 } // namespace internal
65 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698