OLD | NEW |
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/memory/scoped_ptr.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 enum LockDiscardableMemoryStatus { | 15 enum LockDiscardableMemoryStatus { |
15 DISCARDABLE_MEMORY_FAILED = -1, | 16 DISCARDABLE_MEMORY_FAILED = -1, |
16 DISCARDABLE_MEMORY_PURGED = 0, | 17 DISCARDABLE_MEMORY_PURGED = 0, |
17 DISCARDABLE_MEMORY_SUCCESS = 1 | 18 DISCARDABLE_MEMORY_SUCCESS = 1 |
18 }; | 19 }; |
19 | 20 |
20 // Platform abstraction for discardable memory. DiscardableMemory is used to | 21 // Platform abstraction for discardable memory. DiscardableMemory is used to |
(...skipping 17 matching lines...) Expand all Loading... |
38 // larger than the requested memory size. It is not very efficient for | 39 // larger than the requested memory size. It is not very efficient for |
39 // small allocations. | 40 // small allocations. |
40 // | 41 // |
41 // References: | 42 // References: |
42 // - Linux: http://lwn.net/Articles/452035/ | 43 // - Linux: http://lwn.net/Articles/452035/ |
43 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp | 44 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp |
44 // the comment starting with "vm_object_purgable_control" at | 45 // 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 | 46 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c |
46 class BASE_EXPORT DiscardableMemory { | 47 class BASE_EXPORT DiscardableMemory { |
47 public: | 48 public: |
48 DiscardableMemory(); | 49 virtual ~DiscardableMemory() {} |
49 | 50 |
50 // If the discardable memory is locked, the destructor will unlock it. | 51 // Returns whether the system supports discardable memory. |
51 // The opened file will also be closed after this. | |
52 ~DiscardableMemory(); | |
53 | |
54 // Check whether the system supports discardable memory. | |
55 static bool Supported(); | 52 static bool Supported(); |
56 | 53 |
57 // Initialize the DiscardableMemory object. On success, this function returns | 54 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
58 // 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 | |
60 // should stop using the DiscardableMemory afterwards. | |
61 bool InitializeAndLock(size_t size); | |
62 | 55 |
63 // Lock the memory so that it will not be purged by the system. Returns | 56 // Locks the memory so that it will not be purged by the system. Returns |
64 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is | 57 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is |
65 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and | 58 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and |
66 // a new one should be created. If the return value is | 59 // a new one should be created. If the return value is |
67 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that | 60 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that |
68 // was in it is gone. | 61 // was in it is gone. |
69 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT; | 62 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; |
70 | 63 |
71 // Unlock the memory so that it can be purged by the system. Must be called | 64 // Unlocks the memory so that it can be purged by the system. Must be called |
72 // after every successful lock call. | 65 // after every successful lock call. |
73 void Unlock(); | 66 virtual void Unlock() = 0; |
74 | 67 |
75 // Return the memory address held by this object. The object must be locked | 68 // Returns the memory address held by this object. The object must be locked |
76 // before calling this. Otherwise, this will cause a DCHECK error. | 69 // before calling this. Otherwise, this will cause a DCHECK error. |
77 void* Memory() const; | 70 virtual void* Memory() const = 0; |
78 | 71 |
79 // Testing utility calls. | 72 // Testing utility calls. |
80 | 73 |
81 // Check whether a purge of all discardable memory in the system is supported. | 74 // Check whether a purge of all discardable memory in the system is supported. |
82 // Use only for testing! | 75 // Use only for testing! |
83 static bool PurgeForTestingSupported(); | 76 static bool PurgeForTestingSupported(); |
84 | 77 |
85 // Purge all discardable memory in the system. This call has global effects | 78 // 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! | 79 // across all running processes, so it should only be used for testing! |
87 static void PurgeForTesting(); | 80 static void PurgeForTesting(); |
88 | |
89 private: | |
90 #if defined(OS_ANDROID) | |
91 // Maps the discardable memory into the caller's address space. | |
92 // Returns true on success, false otherwise. | |
93 bool Map(); | |
94 | |
95 // Unmaps the discardable memory from the caller's address space. | |
96 void Unmap(); | |
97 | |
98 // Reserve a file descriptor. When reaching the fd limit, this call returns | |
99 // false and initialization should fail. | |
100 bool ReserveFileDescriptor(); | |
101 | |
102 // Release a file descriptor so that others can reserve it. | |
103 void ReleaseFileDescriptor(); | |
104 #endif // OS_ANDROID | |
105 | |
106 void* memory_; | |
107 size_t size_; | |
108 bool is_locked_; | |
109 #if defined(OS_ANDROID) | |
110 int fd_; | |
111 #endif // OS_ANDROID | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory); | |
114 }; | 81 }; |
115 | 82 |
116 } // namespace base | 83 } // namespace base |
117 | 84 |
118 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 85 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
OLD | NEW |