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

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

Issue 15650016: [Not for review] Discardable memory emulation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 6 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 11
12 namespace base { 12 namespace base {
13 13
14 enum LockDiscardableMemoryStatus { 14 enum LockDiscardableMemoryStatus {
15 DISCARDABLE_MEMORY_FAILED = -1, 15 DISCARDABLE_MEMORY_FAILED = -1,
16 DISCARDABLE_MEMORY_PURGED = 0, 16 DISCARDABLE_MEMORY_PURGED = 0,
17 DISCARDABLE_MEMORY_SUCCESS = 1 17 DISCARDABLE_MEMORY_SUCCESS = 1
18 }; 18 };
19 19
20 #if defined(OS_MACOSX) || defined(OS_ANDROID)
21 // This should only be defined on platforms that guarantee that
22 // DiscardableMemoryProvider::GetInstance will not return NULL. If your
23 // platform's support cannot provide this guarantee (e.g., can only return a
24 // non-NULL value in certain circumstances), do not process this define.
25 #define DISCARDABLE_MEMORY_SUPPORTED_NATIVELY
avi_google.com 2013/06/14 11:09:33 BTW it's likely that we will need to turn this int
26 class DiscardableMemoryProvider;
Avi (use Gerrit) 2013/06/12 14:56:49 Why is this class forward declared here? It's only
27 #endif
28
20 // Platform abstraction for discardable memory. DiscardableMemory is used to 29 // Platform abstraction for discardable memory. DiscardableMemory is used to
21 // cache large objects without worrying about blowing out memory, both on mobile 30 // 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 31 // 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 32 // 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 33 // memory in response to an OOM signal because it is simpler, though it has less
25 // flexibility as to which objects get discarded. 34 // flexibility as to which objects get discarded.
26 // 35 //
27 // Discardable memory has two states: locked and unlocked. While the memory is 36 // 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 37 // locked, it will not be discarded. Unlocking the memory allows the OS to
29 // reclaim it if needed. Locks do not nest. 38 // reclaim it if needed. Locks do not nest.
(...skipping 14 matching lines...) Expand all
44 // the comment starting with "vm_object_purgable_control" at 53 // 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 54 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v m_object.c
46 class BASE_EXPORT DiscardableMemory { 55 class BASE_EXPORT DiscardableMemory {
47 public: 56 public:
48 DiscardableMemory(); 57 DiscardableMemory();
49 58
50 // If the discardable memory is locked, the destructor will unlock it. 59 // If the discardable memory is locked, the destructor will unlock it.
51 // The opened file will also be closed after this. 60 // The opened file will also be closed after this.
52 ~DiscardableMemory(); 61 ~DiscardableMemory();
53 62
54 // Check whether the system supports discardable memory.
55 static bool Supported();
56
57 // Initialize the DiscardableMemory object. On success, this function returns 63 // Initialize the DiscardableMemory object. On success, this function returns
58 // true and the memory is locked. This should only be called once. 64 // 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 65 // This call could fail because of platform-specific limitations and the user
60 // should stop using the DiscardableMemory afterwards. 66 // should stop using the DiscardableMemory afterwards.
61 bool InitializeAndLock(size_t size); 67 bool InitializeAndLock(size_t size);
62 68
63 // Lock the memory so that it will not be purged by the system. Returns 69 // 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 70 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is
65 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and 71 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and
66 // a new one should be created. If the return value is 72 // a new one should be created. If the return value is
67 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that 73 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that
68 // was in it is gone. 74 // was in it is gone.
69 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT; 75 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT;
70 76
71 // Unlock the memory so that it can be purged by the system. Must be called 77 // Unlock the memory so that it can be purged by the system. Must be called
72 // after every successful lock call. 78 // after every successful lock call.
73 void Unlock(); 79 void Unlock();
74 80
81 bool is_locked() const { return is_locked_; }
82
75 // Return the memory address held by this object. The object must be locked 83 // Return the memory address held by this object. The object must be locked
76 // before calling this. Otherwise, this will cause a DCHECK error. 84 // before calling this. Otherwise, this will cause a DCHECK error.
77 void* Memory() const; 85 void* Memory() const;
78 86
79 // Testing utility calls. 87 // Testing utility calls.
80 88
81 // Check whether a purge of all discardable memory in the system is supported. 89 // Check whether a purge of all discardable memory in the system is supported.
82 // Use only for testing! 90 // Use only for testing!
83 static bool PurgeForTestingSupported(); 91 static bool PurgeForTestingSupported();
84 92
85 // Purge all discardable memory in the system. This call has global effects 93 // 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! 94 // across all running processes, so it should only be used for testing!
87 static void PurgeForTesting(); 95 static void PurgeForTesting();
88 96
97 #if !defined(DISCARDABLE_MEMORY_SUPPORTED_NATIVELY)
98 // Platforms without native support for discardable memory may use this
99 // to force discardable memory to be purged as they see fit.
100 static void Purge();
101 #endif
Avi (use Gerrit) 2013/06/12 14:56:49 I would re-order this up two functions, so it does
102
89 private: 103 private:
104 #if !defined(DISCARDABLE_MEMORY_SUPPORTED_NATIVELY)
105 friend class DiscardableMemoryProvider;
106
107 // Used by the DiscardableMemoryProvider to allocate and free memory.
108 bool Allocate();
109 void Deallocate();
110 #endif
111
90 #if defined(OS_ANDROID) 112 #if defined(OS_ANDROID)
91 // Maps the discardable memory into the caller's address space. 113 // Maps the discardable memory into the caller's address space.
92 // Returns true on success, false otherwise. 114 // Returns true on success, false otherwise.
93 bool Map(); 115 bool Map();
94 116
95 // Unmaps the discardable memory from the caller's address space. 117 // Unmaps the discardable memory from the caller's address space.
96 void Unmap(); 118 void Unmap();
97 119
98 // Reserve a file descriptor. When reaching the fd limit, this call returns 120 // Reserve a file descriptor. When reaching the fd limit, this call returns
99 // false and initialization should fail. 121 // false and initialization should fail.
100 bool ReserveFileDescriptor(); 122 bool ReserveFileDescriptor();
101 123
102 // Release a file descriptor so that others can reserve it. 124 // Release a file descriptor so that others can reserve it.
103 void ReleaseFileDescriptor(); 125 void ReleaseFileDescriptor();
104 #endif // OS_ANDROID 126 #endif // OS_ANDROID
105 127
106 void* memory_; 128 void* memory_;
107 size_t size_; 129 size_t size_;
108 bool is_locked_; 130 bool is_locked_;
109 #if defined(OS_ANDROID) 131 #if defined(OS_ANDROID)
110 int fd_; 132 int fd_;
111 #endif // OS_ANDROID 133 #endif // OS_ANDROID
112 134
113 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory); 135 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory);
114 }; 136 };
115 137
116 } // namespace base 138 } // namespace base
117 139
118 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ 140 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698