OLD | NEW |
| (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_shmem.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "base/memory/discardable_memory_shmem_allocator.h" | |
9 | |
10 namespace base { | |
11 namespace internal { | |
12 | |
13 DiscardableMemoryShmem::DiscardableMemoryShmem(size_t bytes) | |
14 : chunk_(DiscardableMemoryShmemAllocator::GetInstance() | |
15 ->AllocateLockedDiscardableMemory(bytes)), | |
16 is_locked_(true) { | |
17 DCHECK(chunk_); | |
18 } | |
19 | |
20 DiscardableMemoryShmem::~DiscardableMemoryShmem() { | |
21 if (is_locked_) | |
22 Unlock(); | |
23 } | |
24 | |
25 bool DiscardableMemoryShmem::Lock() { | |
26 DCHECK(!is_locked_); | |
27 DCHECK(chunk_); | |
28 | |
29 if (!chunk_->Lock()) { | |
30 chunk_.reset(); | |
31 return false; | |
32 } | |
33 | |
34 is_locked_ = true; | |
35 return true; | |
36 } | |
37 | |
38 void DiscardableMemoryShmem::Unlock() { | |
39 DCHECK(is_locked_); | |
40 chunk_->Unlock(); | |
41 is_locked_ = false; | |
42 } | |
43 | |
44 void* DiscardableMemoryShmem::Memory() const { | |
45 DCHECK(is_locked_); | |
46 return chunk_->Memory(); | |
47 } | |
48 | |
49 } // namespace internal | |
50 } // namespace base | |
OLD | NEW |