Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1551)

Side by Side Diff: base/memory/discardable_memory.h

Issue 17106004: Add discardable memory emulation for non-android/mac platforms (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing review. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_
7 7
8 #include "base/base_export.h" 8 #include "base/base_export.h"
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/threading/non_thread_safe.h"
11 12
12 namespace base { 13 namespace base {
13 14
15 namespace internal {
16 class DiscardableMemoryProvider;
willchan no longer on Chromium 2013/10/01 18:12:05 No indentation
reveman 2013/10/09 22:40:24 Done.
17 }
18
14 enum LockDiscardableMemoryStatus { 19 enum LockDiscardableMemoryStatus {
15 DISCARDABLE_MEMORY_FAILED = -1, 20 DISCARDABLE_MEMORY_FAILED = -1,
16 DISCARDABLE_MEMORY_PURGED = 0, 21 DISCARDABLE_MEMORY_PURGED = 0,
17 DISCARDABLE_MEMORY_SUCCESS = 1 22 DISCARDABLE_MEMORY_SUCCESS = 1
18 }; 23 };
19 24
20 // Platform abstraction for discardable memory. DiscardableMemory is used to 25 // Platform abstraction for discardable memory. DiscardableMemory is used to
21 // cache large objects without worrying about blowing out memory, both on mobile 26 // cache large objects without worrying about blowing out memory, both on mobile
22 // devices where there is no swap, and desktop devices where unused free memory 27 // devices where there is no swap, and desktop devices where unused free memory
23 // should be used to help the user experience. This is preferable to releasing 28 // should be used to help the user experience. This is preferable to releasing
24 // memory in response to an OOM signal because it is simpler, though it has less 29 // memory in response to an OOM signal because it is simpler, though it has less
25 // flexibility as to which objects get discarded. 30 // flexibility as to which objects get discarded.
26 // 31 //
27 // Discardable memory has two states: locked and unlocked. While the memory is 32 // Discardable memory has two states: locked and unlocked. While the memory is
28 // locked, it will not be discarded. Unlocking the memory allows the OS to 33 // locked, it will not be discarded. Unlocking the memory allows the OS to
29 // reclaim it if needed. Locks do not nest. 34 // reclaim it if needed. Locks do not nest.
30 // 35 //
31 // Notes: 36 // Notes:
32 // - The paging behavior of memory while it is locked is not specified. While 37 // - The paging behavior of memory while it is locked is not specified. While
33 // mobile platforms will not swap it out, it may qualify for swapping 38 // mobile platforms will not swap it out, it may qualify for swapping
34 // on desktop platforms. It is not expected that this will matter, as the 39 // on desktop platforms. It is not expected that this will matter, as the
35 // preferred pattern of usage for DiscardableMemory is to lock down the 40 // preferred pattern of usage for DiscardableMemory is to lock down the
36 // memory, use it as quickly as possible, and then unlock it. 41 // memory, use it as quickly as possible, and then unlock it.
37 // - Because of memory alignment, the amount of memory allocated can be 42 // - Because of memory alignment, the amount of memory allocated can be
38 // larger than the requested memory size. It is not very efficient for 43 // larger than the requested memory size. It is not very efficient for
39 // small allocations. 44 // small allocations.
45 // - A discardable memory instance is not thread safe. It is the
46 // responsibility of users of discardable memory to ensure there is no
47 // contention for the instances.
willchan no longer on Chromium 2013/10/01 18:12:05 It's more technically correct to say there are no
reveman 2013/10/09 22:40:24 Done.
40 // 48 //
41 // References: 49 // References:
42 // - Linux: http://lwn.net/Articles/452035/ 50 // - Linux: http://lwn.net/Articles/452035/
43 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur geableBufferMac.cpp 51 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur geableBufferMac.cpp
44 // the comment starting with "vm_object_purgable_control" at 52 // the comment starting with "vm_object_purgable_control" at
45 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v m_object.c 53 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v m_object.c
46 class BASE_EXPORT DiscardableMemory { 54 class BASE_EXPORT DiscardableMemory {
47 public: 55 public:
48 DiscardableMemory(); 56 DiscardableMemory();
49 57
50 // If the discardable memory is locked, the destructor will unlock it. 58 // If the discardable memory is locked, the destructor will unlock it.
51 // The opened file will also be closed after this. 59 // The opened file will also be closed after this.
52 ~DiscardableMemory(); 60 ~DiscardableMemory();
53 61
54 // Check whether the system supports discardable memory. 62 // Check whether the system supports discardable memory natively. Returns
55 static bool Supported(); 63 // false if the support is emulated.
64 static bool SupportedNatively();
56 65
57 // Initialize the DiscardableMemory object. On success, this function returns 66 // Initialize the DiscardableMemory object. On success, this function returns
58 // true and the memory is locked. This should only be called once. 67 // true and the memory is locked. This should only be called once.
59 // This call could fail because of platform-specific limitations and the user 68 // This call could fail because of platform-specific limitations and the user
60 // should stop using the DiscardableMemory afterwards. 69 // should stop using the DiscardableMemory afterwards.
61 bool InitializeAndLock(size_t size); 70 bool InitializeAndLock(size_t size);
62 71
63 // Lock the memory so that it will not be purged by the system. Returns 72 // Lock the memory so that it will not be purged by the system. Returns
64 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is 73 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is
65 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and 74 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and
66 // a new one should be created. If the return value is 75 // a new one should be created. If the return value is
67 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that 76 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that
68 // was in it is gone. 77 // was in it is gone.
69 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT; 78 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT;
70 79
71 // Unlock the memory so that it can be purged by the system. Must be called 80 // Unlock the memory so that it can be purged by the system. Must be called
72 // after every successful lock call. 81 // after every successful lock call.
73 void Unlock(); 82 void Unlock();
74 83
84 bool is_locked() const { return is_locked_; }
85
75 // Return the memory address held by this object. The object must be locked 86 // Return the memory address held by this object. The object must be locked
76 // before calling this. Otherwise, this will cause a DCHECK error. 87 // before calling this. Otherwise, this will cause a DCHECK error.
77 void* Memory() const; 88 void* Memory() const;
78 89
79 // Testing utility calls. 90 // Testing utility calls.
80 91
81 // Check whether a purge of all discardable memory in the system is supported. 92 // Check whether a purge of all discardable memory in the system is supported.
82 // Use only for testing! 93 // Use only for testing!
83 static bool PurgeForTestingSupported(); 94 static bool PurgeForTestingSupported();
84 95
85 // Purge all discardable memory in the system. This call has global effects 96 // Purge all discardable memory in the system. This call has global effects
86 // across all running processes, so it should only be used for testing! 97 // across all running processes, so it should only be used for testing!
87 static void PurgeForTesting(); 98 static void PurgeForTesting();
88 99
89 private: 100 private:
101 #if !defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
102 friend class base::internal::DiscardableMemoryProvider;
willchan no longer on Chromium 2013/10/01 18:12:05 You can ditch the base::.
reveman 2013/10/09 22:40:24 Done.
103 friend class DiscardableMemoryProviderTest;
104
105 // The following functions are used by the DiscardableMemoryProvider to
106 // allocate and free memory. Allocate returns true if successful.
107 bool Allocate();
108 void Deallocate();
109 #endif
110
90 #if defined(OS_ANDROID) 111 #if defined(OS_ANDROID)
91 // Maps the discardable memory into the caller's address space. 112 // Maps the discardable memory into the caller's address space.
92 // Returns true on success, false otherwise. 113 // Returns true on success, false otherwise.
93 bool Map(); 114 bool Map();
94 115
95 // Unmaps the discardable memory from the caller's address space. 116 // Unmaps the discardable memory from the caller's address space.
96 void Unmap(); 117 void Unmap();
97 118
98 // Reserve a file descriptor. When reaching the fd limit, this call returns 119 // Reserve a file descriptor. When reaching the fd limit, this call returns
99 // false and initialization should fail. 120 // false and initialization should fail.
100 bool ReserveFileDescriptor(); 121 bool ReserveFileDescriptor();
101 122
102 // Release a file descriptor so that others can reserve it. 123 // Release a file descriptor so that others can reserve it.
103 void ReleaseFileDescriptor(); 124 void ReleaseFileDescriptor();
104 #endif // OS_ANDROID 125 #endif // OS_ANDROID
105 126
106 void* memory_; 127 void* memory_;
107 size_t size_; 128 size_t size_;
108 bool is_locked_; 129 bool is_locked_;
109 #if defined(OS_ANDROID) 130 #if defined(OS_ANDROID)
110 int fd_; 131 int fd_;
111 #endif // OS_ANDROID 132 #endif // OS_ANDROID
112 133
113 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory); 134 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory);
114 }; 135 };
115 136
116 } // namespace base 137 } // namespace base
117 138
118 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ 139 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698