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

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

Issue 114923005: base: Discardable memory types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review feedback Created 7 years 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 <vector>
9
8 #include "base/base_export.h" 10 #include "base/base_export.h"
9 #include "base/basictypes.h" 11 #include "base/basictypes.h"
10 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
12 14
13 namespace base { 15 namespace base {
14 16
15 enum LockDiscardableMemoryStatus { 17 enum DiscardableMemoryType {
16 DISCARDABLE_MEMORY_FAILED = -1, 18 DISCARDABLE_MEMORY_TYPE_NONE,
17 DISCARDABLE_MEMORY_PURGED = 0, 19 DISCARDABLE_MEMORY_TYPE_ANDROID,
18 DISCARDABLE_MEMORY_SUCCESS = 1 20 DISCARDABLE_MEMORY_TYPE_MAC,
21 DISCARDABLE_MEMORY_TYPE_EMULATED
22 };
23
24 enum DiscardableMemoryLockStatus {
25 DISCARDABLE_MEMORY_LOCK_STATUS_FAILED,
26 DISCARDABLE_MEMORY_LOCK_STATUS_PURGED,
27 DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
19 }; 28 };
20 29
21 // Platform abstraction for discardable memory. DiscardableMemory is used to 30 // Platform abstraction for discardable memory. DiscardableMemory is used to
22 // cache large objects without worrying about blowing out memory, both on mobile 31 // cache large objects without worrying about blowing out memory, both on mobile
23 // devices where there is no swap, and desktop devices where unused free memory 32 // devices where there is no swap, and desktop devices where unused free memory
24 // should be used to help the user experience. This is preferable to releasing 33 // should be used to help the user experience. This is preferable to releasing
25 // memory in response to an OOM signal because it is simpler, though it has less 34 // memory in response to an OOM signal because it is simpler, though it has less
26 // flexibility as to which objects get discarded. 35 // flexibility as to which objects get discarded.
27 // 36 //
28 // Discardable memory has two states: locked and unlocked. While the memory is 37 // Discardable memory has two states: locked and unlocked. While the memory is
(...skipping 17 matching lines...) Expand all
46 // - Linux: http://lwn.net/Articles/452035/ 55 // - Linux: http://lwn.net/Articles/452035/
47 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur geableBufferMac.cpp 56 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur geableBufferMac.cpp
48 // the comment starting with "vm_object_purgable_control" at 57 // the comment starting with "vm_object_purgable_control" at
49 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v m_object.c 58 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v m_object.c
50 // 59 //
51 // Thread-safety: DiscardableMemory instances are not thread-safe. 60 // Thread-safety: DiscardableMemory instances are not thread-safe.
52 class BASE_EXPORT DiscardableMemory { 61 class BASE_EXPORT DiscardableMemory {
53 public: 62 public:
54 virtual ~DiscardableMemory() {} 63 virtual ~DiscardableMemory() {}
55 64
56 // Check whether the system supports discardable memory natively. Returns 65 // Get system supported discardable memory types. Preferred type at the front
Philippe 2013/12/18 09:07:47 Tiny nit: I would s/Get/Gets and same below for co
reveman 2013/12/18 19:44:25 Done.
57 // false if the support is emulated. 66 // of vector.
58 static bool SupportedNatively(); 67 static void GetSupportedTypes(std::vector<DiscardableMemoryType>* types);
68
69 // Set the current discardable memory type.
70 static void SetType(DiscardableMemoryType type);
71
72 // Get the current discardable memory type.
73 static DiscardableMemoryType GetType();
59 74
60 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); 75 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size);
61 76
62 // Locks the memory so that it will not be purged by the system. Returns 77 // Locks the memory so that it will not be purged by the system. Returns
63 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is 78 // DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS on success. If the return value is
64 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and 79 // DISCARDABLE_MEMORY_LOCK_STATUS_FAILED then this object should be
65 // a new one should be created. If the return value is 80 // discarded and a new one should be created. If the return value is
66 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that 81 // DISCARDABLE_MEMORY_LOCK_STATUS_PURGED then the memory is present but any
67 // was in it is gone. 82 // data that was in it is gone.
68 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; 83 virtual DiscardableMemoryLockStatus Lock() WARN_UNUSED_RESULT = 0;
69 84
70 // Unlocks the memory so that it can be purged by the system. Must be called 85 // Unlocks the memory so that it can be purged by the system. Must be called
71 // after every successful lock call. 86 // after every successful lock call.
72 virtual void Unlock() = 0; 87 virtual void Unlock() = 0;
73 88
74 // Returns the memory address held by this object. The object must be locked 89 // Returns the memory address held by this object. The object must be locked
75 // before calling this. Otherwise, this will cause a DCHECK error. 90 // before calling this. Otherwise, this will cause a DCHECK error.
76 virtual void* Memory() const = 0; 91 virtual void* Memory() const = 0;
77 92
78 // Testing utility calls. 93 // Testing utility calls.
79 94
80 // Check whether a purge of all discardable memory in the system is supported. 95 // Check whether a purge of all discardable memory in the system is supported.
81 // Use only for testing! 96 // Use only for testing!
82 static bool PurgeForTestingSupported(); 97 static bool PurgeForTestingSupported();
83 98
84 // Purge all discardable memory in the system. This call has global effects 99 // Purge all discardable memory in the system. This call has global effects
85 // across all running processes, so it should only be used for testing! 100 // across all running processes, so it should only be used for testing!
86 static void PurgeForTesting(); 101 static void PurgeForTesting();
87 }; 102 };
88 103
89 } // namespace base 104 } // namespace base
90 105
91 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ 106 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698