Chromium Code Reviews| 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 #ifndef NDEBUG | |
| 9 #include <set> | |
| 10 #endif | |
| 11 | |
| 8 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 9 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 10 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 11 | 15 |
| 12 namespace base { | 16 namespace base { |
| 13 | 17 |
| 14 // Platform abstraction for discardable shared memory. | 18 // Platform abstraction for discardable shared memory. |
| 15 class BASE_EXPORT DiscardableSharedMemory { | 19 class BASE_EXPORT DiscardableSharedMemory { |
| 16 public: | 20 public: |
| 17 DiscardableSharedMemory(); | 21 DiscardableSharedMemory(); |
| 18 | 22 |
| 19 // Create a new DiscardableSharedMemory object from an existing, open shared | 23 // Create a new DiscardableSharedMemory object from an existing, open shared |
| 20 // memory file. | 24 // memory file. Memory should be locked. |
|
Avi (use Gerrit)
2014/12/17 16:58:17
should? must
reveman
2014/12/17 18:38:49
Done.
| |
| 21 explicit DiscardableSharedMemory(SharedMemoryHandle handle); | 25 explicit DiscardableSharedMemory(SharedMemoryHandle handle); |
| 22 | 26 |
| 23 // Closes any open files. | 27 // Closes any open files. |
| 24 virtual ~DiscardableSharedMemory(); | 28 virtual ~DiscardableSharedMemory(); |
| 25 | 29 |
| 26 // Creates and maps a locked DiscardableSharedMemory object with |size|. | 30 // Creates and maps a locked DiscardableSharedMemory object with |size|. |
| 27 // Returns true on success and false on failure. | 31 // Returns true on success and false on failure. |
| 28 bool CreateAndMap(size_t size); | 32 bool CreateAndMap(size_t size); |
| 29 | 33 |
| 30 // Maps the discardable memory into the caller's address space. | 34 // Maps the locked discardable memory into the caller's address space. |
| 31 // Returns true on success, false otherwise. | 35 // Returns true on success, false otherwise. |
| 32 bool Map(size_t size); | 36 bool Map(size_t size); |
| 33 | 37 |
| 34 // The actual size of the mapped memory (may be larger than requested). | 38 // The actual size of the mapped memory (may be larger than requested). |
| 35 size_t mapped_size() const { return shared_memory_.mapped_size(); } | 39 size_t mapped_size() const { return mapped_size_; } |
| 36 | 40 |
| 37 // Returns a shared memory handle for this DiscardableSharedMemory object. | 41 // Returns a shared memory handle for this DiscardableSharedMemory object. |
| 38 SharedMemoryHandle handle() const { return shared_memory_.handle(); } | 42 SharedMemoryHandle handle() const { return shared_memory_.handle(); } |
| 39 | 43 |
| 40 // Locks the memory so that it will not be purged by the system. Returns | 44 // 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 | 45 // 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 | 46 // 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 | 47 // 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 | 48 // know usage time is updated to the actual last usage timestamp if memory |
| 45 // is still resident or 0 if not. | 49 // is still resident or 0 if not. The range of memory must be unlocked. The |
| 46 bool Lock(); | 50 // result of trying to lock an already locked range is undefined. |
| 51 // |offset| and |length| must both be a multiple of the systems page size. | |
|
Avi (use Gerrit)
2014/12/17 16:58:17
system's
You might want to call out base::GetPage
reveman
2014/12/17 18:38:49
Yes, I fixed this typo already.
| |
| 52 // Passing 0 for |length| means "everything onward". | |
| 53 bool Lock(size_t offset, size_t length); | |
| 47 | 54 |
| 48 // Unlock previously successfully locked memory. | 55 // Unlock a previously successfully locked range of memory. The range of |
| 49 void Unlock(); | 56 // memory must be locked. The result of trying to unlock a not |
| 57 // previously locked range is undefined. | |
| 58 // |offset| and |length| must both be a multiple of the systems page size. | |
|
Avi (use Gerrit)
2014/12/17 16:58:17
Same comment as above.
reveman
2014/12/17 18:38:49
Done.
| |
| 59 // Passing 0 for |length| means "everything onward". | |
| 60 void Unlock(size_t offset, size_t length); | |
| 50 | 61 |
| 51 // Gets a pointer to the opened discardable memory space. Discardable memory | 62 // Gets a pointer to the opened discardable memory space. Discardable memory |
| 52 // must have been mapped via Map(). | 63 // must have been mapped via Map(). |
| 53 void* memory() const; | 64 void* memory() const; |
| 54 | 65 |
| 55 // Returns the last know usage time for DiscardableSharedMemory object. This | 66 // 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 | 67 // may be earlier than the "true" usage time when memory has been used by a |
| 57 // different process. Returns NULL time if purged. | 68 // different process. Returns NULL time if purged. |
| 58 Time last_known_usage() const { return last_known_usage_; } | 69 Time last_known_usage() const { return last_known_usage_; } |
| 59 | 70 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 bool ShareToProcess(ProcessHandle process_handle, | 103 bool ShareToProcess(ProcessHandle process_handle, |
| 93 SharedMemoryHandle* new_handle) { | 104 SharedMemoryHandle* new_handle) { |
| 94 return shared_memory_.ShareToProcess(process_handle, new_handle); | 105 return shared_memory_.ShareToProcess(process_handle, new_handle); |
| 95 } | 106 } |
| 96 | 107 |
| 97 private: | 108 private: |
| 98 // Virtual for tests. | 109 // Virtual for tests. |
| 99 virtual Time Now() const; | 110 virtual Time Now() const; |
| 100 | 111 |
| 101 SharedMemory shared_memory_; | 112 SharedMemory shared_memory_; |
| 113 size_t mapped_size_; | |
| 114 size_t locked_page_count_; | |
| 115 #ifndef NDEBUG | |
| 116 std::set<size_t> locked_pages_; | |
| 117 #endif | |
| 102 Time last_known_usage_; | 118 Time last_known_usage_; |
| 103 | 119 |
| 104 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 120 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
| 105 }; | 121 }; |
| 106 | 122 |
| 107 } // namespace base | 123 } // namespace base |
| 108 | 124 |
| 109 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 125 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
| OLD | NEW |