blob: a590e531b7a1e9749f80fce38c62c15aeaa4cc5d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/discardable_memory_ashmem.h"
#include "base/memory/discardable_memory_ashmem_allocator.h"
namespace base {
namespace internal {
DiscardableMemoryAshmem::DiscardableMemoryAshmem(
size_t bytes,
DiscardableMemoryAshmemAllocator* allocator,
DiscardableMemoryManager* manager)
: bytes_(bytes),
allocator_(allocator),
manager_(manager),
is_locked_(false) {
manager_->Register(this, bytes_);
}
DiscardableMemoryAshmem::~DiscardableMemoryAshmem() {
if (is_locked_)
Unlock();
manager_->Unregister(this);
}
bool DiscardableMemoryAshmem::Initialize() {
return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
}
DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() {
DCHECK(!is_locked_);
bool purged = false;
if (!manager_->AcquireLock(this, &purged))
return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
is_locked_ = true;
return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
: DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
}
void DiscardableMemoryAshmem::Unlock() {
DCHECK(is_locked_);
manager_->ReleaseLock(this);
is_locked_ = false;
}
void* DiscardableMemoryAshmem::Memory() const {
DCHECK(is_locked_);
DCHECK(ashmem_chunk_);
return ashmem_chunk_->Memory();
}
bool DiscardableMemoryAshmem::AllocateAndAcquireLock() {
if (ashmem_chunk_)
return ashmem_chunk_->Lock();
ashmem_chunk_ = allocator_->Allocate(bytes_);
return false;
}
void DiscardableMemoryAshmem::ReleaseLock() {
ashmem_chunk_->Unlock();
}
void DiscardableMemoryAshmem::Purge() {
ashmem_chunk_.reset();
}
} // namespace internal
} // namespace base
|