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/logging.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "base/threading/thread_collision_warner.h" | 11 #include "base/threading/thread_collision_warner.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 | 13 |
14 #if DCHECK_IS_ON() | 14 #if DCHECK_IS_ON() |
15 #include <set> | 15 #include <set> |
16 #endif | 16 #endif |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 | 19 |
20 // Platform abstraction for discardable shared memory. | 20 // Platform abstraction for discardable shared memory. |
21 // | 21 // |
22 // This class is not thread-safe. Clients are responsible for synchronizing | 22 // This class is not thread-safe. Clients are responsible for synchronizing |
23 // access to an instance of this class. | 23 // access to an instance of this class. |
24 class BASE_EXPORT DiscardableSharedMemory { | 24 class BASE_EXPORT DiscardableSharedMemory { |
25 public: | 25 public: |
| 26 enum LockResult { SUCCESS, PURGED, FAILED }; |
| 27 |
26 DiscardableSharedMemory(); | 28 DiscardableSharedMemory(); |
27 | 29 |
28 // Create a new DiscardableSharedMemory object from an existing, open shared | 30 // Create a new DiscardableSharedMemory object from an existing, open shared |
29 // memory file. Memory must be locked. | 31 // memory file. Memory must be locked. |
30 explicit DiscardableSharedMemory(SharedMemoryHandle handle); | 32 explicit DiscardableSharedMemory(SharedMemoryHandle handle); |
31 | 33 |
32 // Closes any open files. | 34 // Closes any open files. |
33 virtual ~DiscardableSharedMemory(); | 35 virtual ~DiscardableSharedMemory(); |
34 | 36 |
35 // Creates and maps a locked DiscardableSharedMemory object with |size|. | 37 // Creates and maps a locked DiscardableSharedMemory object with |size|. |
36 // Returns true on success and false on failure. | 38 // Returns true on success and false on failure. |
37 bool CreateAndMap(size_t size); | 39 bool CreateAndMap(size_t size); |
38 | 40 |
39 // Maps the locked discardable memory into the caller's address space. | 41 // Maps the locked discardable memory into the caller's address space. |
40 // Returns true on success, false otherwise. | 42 // Returns true on success, false otherwise. |
41 bool Map(size_t size); | 43 bool Map(size_t size); |
42 | 44 |
43 // The actual size of the mapped memory (may be larger than requested). | 45 // The actual size of the mapped memory (may be larger than requested). |
44 size_t mapped_size() const { return mapped_size_; } | 46 size_t mapped_size() const { return mapped_size_; } |
45 | 47 |
46 // Returns a shared memory handle for this DiscardableSharedMemory object. | 48 // Returns a shared memory handle for this DiscardableSharedMemory object. |
47 SharedMemoryHandle handle() const { return shared_memory_.handle(); } | 49 SharedMemoryHandle handle() const { return shared_memory_.handle(); } |
48 | 50 |
49 // Locks a range of memory so that it will not be purged by the system. | 51 // Locks a range of memory so that it will not be purged by the system. |
50 // Returns true if successful and the memory is still resident. Locking can | 52 // The range of memory must be unlocked. The result of trying to lock an |
51 // fail for three reasons; object might have been purged, our last known usage | 53 // already locked range is undefined. |offset| and |length| must both be |
52 // timestamp might be out of date or memory might already be locked. Last | 54 // a multiple of the page size as returned by GetPageSize(). |
53 // know usage time is updated to the actual last usage timestamp if memory | |
54 // is still resident or 0 if not. The range of memory must be unlocked. The | |
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". | 55 // Passing 0 for |length| means "everything onward". |
59 bool Lock(size_t offset, size_t length); | 56 // Returns SUCCESS if range was successfully locked and the memory is still |
| 57 // resident, PURGED if range was successfully locked but has been purged |
| 58 // since last time it was locked and FAILED if range could not be locked. |
| 59 // Locking can fail for two reasons; object might have been purged, our |
| 60 // last known usage timestamp might be out of date. Last known usage time |
| 61 // is updated to the actual last usage timestamp if memory is still resident |
| 62 // or 0 if not. |
| 63 LockResult Lock(size_t offset, size_t length); |
60 | 64 |
61 // Unlock a previously successfully locked range of memory. The range of | 65 // Unlock a previously successfully locked range of memory. The range of |
62 // memory must be locked. The result of trying to unlock a not | 66 // memory must be locked. The result of trying to unlock a not |
63 // previously locked range is undefined. | 67 // previously locked range is undefined. |
64 // |offset| and |length| must both be a multiple of the page size as returned | 68 // |offset| and |length| must both be a multiple of the page size as returned |
65 // by GetPageSize(). | 69 // by GetPageSize(). |
66 // Passing 0 for |length| means "everything onward". | 70 // Passing 0 for |length| means "everything onward". |
67 void Unlock(size_t offset, size_t length); | 71 void Unlock(size_t offset, size_t length); |
68 | 72 |
69 // Gets a pointer to the opened discardable memory space. Discardable memory | 73 // Gets a pointer to the opened discardable memory space. Discardable memory |
(...skipping 56 matching lines...) Loading... |
126 // synchronized somehow. Use a collision warner to detect incorrect usage. | 130 // synchronized somehow. Use a collision warner to detect incorrect usage. |
127 DFAKE_MUTEX(thread_collision_warner_); | 131 DFAKE_MUTEX(thread_collision_warner_); |
128 Time last_known_usage_; | 132 Time last_known_usage_; |
129 | 133 |
130 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 134 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
131 }; | 135 }; |
132 | 136 |
133 } // namespace base | 137 } // namespace base |
134 | 138 |
135 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 139 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |