Added llvm into exceptions as we can't add README.chromium into 3rd party repository
[chromium-blink-merge.git] / base / memory / discardable_memory_shmem.cc
blob9056279492f4a2544904b3f38c63d52386df1774
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.
5 #include "base/memory/discardable_memory_shmem.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/discardable_memory_shmem_allocator.h"
10 namespace base {
11 namespace internal {
13 DiscardableMemoryShmem::DiscardableMemoryShmem(size_t bytes)
14 : bytes_(bytes), is_locked_(false) {
17 DiscardableMemoryShmem::~DiscardableMemoryShmem() {
18 if (is_locked_)
19 Unlock();
22 bool DiscardableMemoryShmem::Initialize() {
23 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
26 DiscardableMemoryLockStatus DiscardableMemoryShmem::Lock() {
27 DCHECK(!is_locked_);
29 if (chunk_ && chunk_->Lock()) {
30 is_locked_ = true;
31 return DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
34 chunk_ = DiscardableMemoryShmemAllocator::GetInstance()
35 ->AllocateLockedDiscardableMemory(bytes_);
36 if (!chunk_)
37 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
39 is_locked_ = true;
40 return DISCARDABLE_MEMORY_LOCK_STATUS_PURGED;
43 void DiscardableMemoryShmem::Unlock() {
44 DCHECK(is_locked_);
45 DCHECK(chunk_);
46 chunk_->Unlock();
47 is_locked_ = false;
50 void* DiscardableMemoryShmem::Memory() const {
51 DCHECK(is_locked_);
52 DCHECK(chunk_);
53 return chunk_->Memory();
56 } // namespace internal
57 } // namespace base