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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// Copyright (c) 2013 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.
#ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_
#define BASE_MEMORY_DISCARDABLE_MEMORY_H_
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/synchronization/lock.h"
namespace base {
enum LockDiscardableMemoryStatus {
DISCARDABLE_MEMORY_FAILED = -1,
DISCARDABLE_MEMORY_PURGED = 0,
DISCARDABLE_MEMORY_SUCCESS = 1
};
// Platform abstraction for discardable memory. The DiscardableMemory should
// be mainly used for mobile devices where VM swap is not available, such as
// android. It is particularly helpful for caching large objects without
// worrying about OOM situation.
// Discardable memory allows user to lock(pin) and unlock(unpin) pages so that
// the allocated memory can be discarded by the kernel under memory pressure.
// From http://lwn.net/Articles/452035/: "Pinned pages (the default) behave
// like any anonymous memory. Unpinned pages are available to the kernel for
// eviction during VM pressure. When repinning the pages, the return value
// instructs user-space as to any eviction. In this manner, user-space processes
// may implement caching and similar resource management that efficiently
// integrates with kernel memory management."
// Compared to relying on OOM signals to clean up the memory, DiscardableMemory
// is much simpler as the OS will taking care of the LRU algorithm and there
// is no need to implement a separate cleanup() call. However, there is no
// guarantee which cached objects will be discarded.
// Because of memory alignment, the actual locked discardable memory could be
// larger than the requested memory size. It may not be very efficient for
// small size allocations.
class BASE_EXPORT DiscardableMemory {
public:
DiscardableMemory();
// If the discardable memory is locked, the destructor will unlock it.
// The opened file will also be closed after this.
~DiscardableMemory();
// Check whether the system supports discardable memory.
static bool Supported() {
#if defined(OS_ANDROID)
return true;
#endif
return false;
}
// Initialize the DiscardableMemory object. On success, this function returns
// true and the memory is locked. This should only be called once.
bool InitializeAndLock(size_t size);
// Lock the memory so that it will not be purged by the system. Returns
// DISCARDABLE_MEMORY_SUCCESS on success. If the return value is
// DISCARDABLE_MEMORY_FAILED then this object should be discarded and
// a new one should be created. If the return value is
// DISCARDABLE_MEMORY_PURGED then the memory is present but any data that
// was in it is gone.
LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT;
// Unlock the memory so that it can be purged by the system. Must be called
// after every successful lock call.
void Unlock();
// Return the memory address held by this object. When this object is locked,
// this call will return the address in caller's address space. Otherwise,
// this call returns NULL.
void* Memory() const;
private:
// Maps the discardable memory into the caller's address space.
// Returns true on success, false otherwise.
bool Map();
// Unmaps the discardable memory from the caller's address space.
void Unmap();
int fd_;
void* memory_;
size_t size_;
bool is_pinned_;
DISALLOW_COPY_AND_ASSIGN(DiscardableMemory);
};
} // namespace base
#endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_
|