OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SHARED_MEMORY_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
6 #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 6 #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/logging.h" |
9 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| 11 #include "base/threading/thread_collision_warner.h" |
10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
11 | 13 |
| 14 #if DCHECK_IS_ON |
| 15 #include <set> |
| 16 #endif |
| 17 |
12 namespace base { | 18 namespace base { |
13 | 19 |
14 // Platform abstraction for discardable shared memory. | 20 // Platform abstraction for discardable shared memory. |
| 21 // |
| 22 // This class is not thread-safe. Clients are responsible for synchronizing |
| 23 // access to an instance of this class. |
15 class BASE_EXPORT DiscardableSharedMemory { | 24 class BASE_EXPORT DiscardableSharedMemory { |
16 public: | 25 public: |
17 DiscardableSharedMemory(); | 26 DiscardableSharedMemory(); |
18 | 27 |
19 // Create a new DiscardableSharedMemory object from an existing, open shared | 28 // Create a new DiscardableSharedMemory object from an existing, open shared |
20 // memory file. | 29 // memory file. Memory must be locked. |
21 explicit DiscardableSharedMemory(SharedMemoryHandle handle); | 30 explicit DiscardableSharedMemory(SharedMemoryHandle handle); |
22 | 31 |
23 // Closes any open files. | 32 // Closes any open files. |
24 virtual ~DiscardableSharedMemory(); | 33 virtual ~DiscardableSharedMemory(); |
25 | 34 |
26 // Creates and maps a locked DiscardableSharedMemory object with |size|. | 35 // Creates and maps a locked DiscardableSharedMemory object with |size|. |
27 // Returns true on success and false on failure. | 36 // Returns true on success and false on failure. |
28 bool CreateAndMap(size_t size); | 37 bool CreateAndMap(size_t size); |
29 | 38 |
30 // Maps the discardable memory into the caller's address space. | 39 // Maps the locked discardable memory into the caller's address space. |
31 // Returns true on success, false otherwise. | 40 // Returns true on success, false otherwise. |
32 bool Map(size_t size); | 41 bool Map(size_t size); |
33 | 42 |
34 // The actual size of the mapped memory (may be larger than requested). | 43 // The actual size of the mapped memory (may be larger than requested). |
35 size_t mapped_size() const { return shared_memory_.mapped_size(); } | 44 size_t mapped_size() const { return mapped_size_; } |
36 | 45 |
37 // Returns a shared memory handle for this DiscardableSharedMemory object. | 46 // Returns a shared memory handle for this DiscardableSharedMemory object. |
38 SharedMemoryHandle handle() const { return shared_memory_.handle(); } | 47 SharedMemoryHandle handle() const { return shared_memory_.handle(); } |
39 | 48 |
40 // Locks the memory so that it will not be purged by the system. Returns | 49 // Locks a range of memory so that it will not be purged by the system. |
41 // true if successful and the memory is still resident. Locking can fail | 50 // Returns true if successful and the memory is still resident. Locking can |
42 // for three reasons; object might have been purged, our last known usage | 51 // fail for three reasons; object might have been purged, our last known usage |
43 // timestamp might be out of date or memory might already be locked. Last | 52 // timestamp might be out of date or memory might already be locked. Last |
44 // know usage time is updated to the actual last usage timestamp if memory | 53 // know usage time is updated to the actual last usage timestamp if memory |
45 // is still resident or 0 if not. | 54 // is still resident or 0 if not. The range of memory must be unlocked. The |
46 bool Lock(); | 55 // result of trying to lock an already locked range is undefined. |
| 56 // |offset| and |length| must both be a multiple of the page size as returned |
| 57 // by GetPageSize(). |
| 58 // Passing 0 for |length| means "everything onward". |
| 59 bool Lock(size_t offset, size_t length); |
47 | 60 |
48 // Unlock previously successfully locked memory. | 61 // Unlock a previously successfully locked range of memory. The range of |
49 void Unlock(); | 62 // memory must be locked. The result of trying to unlock a not |
| 63 // previously locked range is undefined. |
| 64 // |offset| and |length| must both be a multiple of the page size as returned |
| 65 // by GetPageSize(). |
| 66 // Passing 0 for |length| means "everything onward". |
| 67 void Unlock(size_t offset, size_t length); |
50 | 68 |
51 // Gets a pointer to the opened discardable memory space. Discardable memory | 69 // Gets a pointer to the opened discardable memory space. Discardable memory |
52 // must have been mapped via Map(). | 70 // must have been mapped via Map(). |
53 void* memory() const; | 71 void* memory() const; |
54 | 72 |
55 // Returns the last know usage time for DiscardableSharedMemory object. This | 73 // Returns the last know usage time for DiscardableSharedMemory object. This |
56 // may be earlier than the "true" usage time when memory has been used by a | 74 // may be earlier than the "true" usage time when memory has been used by a |
57 // different process. Returns NULL time if purged. | 75 // different process. Returns NULL time if purged. |
58 Time last_known_usage() const { return last_known_usage_; } | 76 Time last_known_usage() const { return last_known_usage_; } |
59 | 77 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 bool ShareToProcess(ProcessHandle process_handle, | 110 bool ShareToProcess(ProcessHandle process_handle, |
93 SharedMemoryHandle* new_handle) { | 111 SharedMemoryHandle* new_handle) { |
94 return shared_memory_.ShareToProcess(process_handle, new_handle); | 112 return shared_memory_.ShareToProcess(process_handle, new_handle); |
95 } | 113 } |
96 | 114 |
97 private: | 115 private: |
98 // Virtual for tests. | 116 // Virtual for tests. |
99 virtual Time Now() const; | 117 virtual Time Now() const; |
100 | 118 |
101 SharedMemory shared_memory_; | 119 SharedMemory shared_memory_; |
| 120 size_t mapped_size_; |
| 121 size_t locked_page_count_; |
| 122 #if DCHECK_IS_ON |
| 123 std::set<size_t> locked_pages_; |
| 124 #endif |
| 125 // Implementation is not thread-safe but still usable if clients are |
| 126 // synchronized somehow. Use a collision warner to detect incorrect usage. |
| 127 DFAKE_MUTEX(thread_collision_warner_); |
102 Time last_known_usage_; | 128 Time last_known_usage_; |
103 | 129 |
104 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 130 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
105 }; | 131 }; |
106 | 132 |
107 } // namespace base | 133 } // namespace base |
108 | 134 |
109 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 135 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |