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

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

Powered by Google App Engine
This is Rietveld 408576698