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 "base/memory/discardable_memory_malloc.h" | 5 #include "base/memory/discardable_memory_ashmem.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 namespace internal { | 10 namespace internal { |
11 | 11 |
12 DiscardableMemoryMalloc::DiscardableMemoryMalloc(size_t size) : size_(size) { | 12 DiscardableMemoryAshmem::DiscardableMemoryAshmem( |
| 13 DiscardableMemoryManager* manager, |
| 14 size_t size) |
| 15 : manager_(manager), |
| 16 allocation_id_(manager->Register(size)), |
| 17 memory_(NULL) { |
13 } | 18 } |
14 | 19 |
15 DiscardableMemoryMalloc::~DiscardableMemoryMalloc() { | 20 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() { |
| 21 manager_->Unregister(allocation_id_); |
16 } | 22 } |
17 | 23 |
18 bool DiscardableMemoryMalloc::Initialize() { | 24 bool DiscardableMemoryAshmem::Initialize() { |
19 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | 25 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; |
20 } | 26 } |
21 | 27 |
22 DiscardableMemoryLockStatus DiscardableMemoryMalloc::Lock() { | 28 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() { |
23 DCHECK(!memory_); | 29 DCHECK(!memory_); |
24 | 30 |
25 memory_.reset(static_cast<uint8*>(malloc(size_))); | 31 bool purged = false; |
| 32 memory_ = manager_->Lock(allocation_id_, &purged); |
26 if (!memory_) | 33 if (!memory_) |
27 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 34 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
28 | 35 |
29 return DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | 36 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 37 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
30 } | 38 } |
31 | 39 |
32 void DiscardableMemoryMalloc::Unlock() { | 40 void DiscardableMemoryAshmem::Unlock() { |
33 DCHECK(memory_); | 41 DCHECK(memory_); |
34 memory_.reset(); | 42 manager_->Unlock(allocation_id_); |
| 43 memory_ = NULL; |
35 } | 44 } |
36 | 45 |
37 void* DiscardableMemoryMalloc::Memory() const { | 46 void* DiscardableMemoryAshmem::Memory() const { |
38 DCHECK(memory_); | 47 DCHECK(memory_); |
39 return memory_.get(); | 48 return memory_; |
40 } | 49 } |
41 | 50 |
42 } // namespace internal | 51 } // namespace internal |
43 } // namespace base | 52 } // namespace base |
OLD | NEW |