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