Chromium Code Reviews| Index: base/memory/discardable_shared_memory.h |
| diff --git a/base/memory/discardable_shared_memory.h b/base/memory/discardable_shared_memory.h |
| index ca2accf1a2ebb51e6f027ab13882c0f1e30b0747..6ab360bc755f0d9382758075bcb310eeb473abaf 100644 |
| --- a/base/memory/discardable_shared_memory.h |
| +++ b/base/memory/discardable_shared_memory.h |
| @@ -5,19 +5,27 @@ |
| #ifndef BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
| #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
| +#ifndef NDEBUG |
| +#include <set> |
| +#endif |
| + |
| #include "base/base_export.h" |
| #include "base/memory/shared_memory.h" |
| +#include "base/threading/thread_collision_warner.h" |
| #include "base/time/time.h" |
| namespace base { |
| // Platform abstraction for discardable shared memory. |
| +// |
| +// 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.
|
| +// access to an instance of this class. |
| class BASE_EXPORT DiscardableSharedMemory { |
| public: |
| DiscardableSharedMemory(); |
| // Create a new DiscardableSharedMemory object from an existing, open shared |
| - // memory file. |
| + // memory file. Memory must be locked. |
| explicit DiscardableSharedMemory(SharedMemoryHandle handle); |
| // Closes any open files. |
| @@ -27,26 +35,35 @@ class BASE_EXPORT DiscardableSharedMemory { |
| // Returns true on success and false on failure. |
| bool CreateAndMap(size_t size); |
| - // Maps the discardable memory into the caller's address space. |
| + // Maps the locked discardable memory into the caller's address space. |
| // Returns true on success, false otherwise. |
| bool Map(size_t size); |
| // The actual size of the mapped memory (may be larger than requested). |
| - size_t mapped_size() const { return shared_memory_.mapped_size(); } |
| + size_t mapped_size() const { return mapped_size_; } |
| // Returns a shared memory handle for this DiscardableSharedMemory object. |
| SharedMemoryHandle handle() const { return shared_memory_.handle(); } |
| - // Locks the memory so that it will not be purged by the system. Returns |
| - // true if successful and the memory is still resident. Locking can fail |
| - // for three reasons; object might have been purged, our last known usage |
| + // Locks a range of memory so that it will not be purged by the system. |
| + // Returns true if successful and the memory is still resident. Locking can |
| + // fail for three reasons; object might have been purged, our last known usage |
| // timestamp might be out of date or memory might already be locked. Last |
| // know usage time is updated to the actual last usage timestamp if memory |
| - // is still resident or 0 if not. |
| - bool Lock(); |
| - |
| - // Unlock previously successfully locked memory. |
| - void Unlock(); |
| + // is still resident or 0 if not. The range of memory must be unlocked. The |
| + // result of trying to lock an already locked range is undefined. |
| + // |offset| and |length| must both be a multiple of the page size as returned |
| + // by GetPageSize(). |
| + // Passing 0 for |length| means "everything onward". |
| + bool Lock(size_t offset, size_t length); |
| + |
| + // Unlock a previously successfully locked range of memory. The range of |
| + // memory must be locked. The result of trying to unlock a not |
| + // previously locked range is undefined. |
| + // |offset| and |length| must both be a multiple of the page size as returned |
| + // by GetPageSize(). |
| + // Passing 0 for |length| means "everything onward". |
| + void Unlock(size_t offset, size_t length); |
| // Gets a pointer to the opened discardable memory space. Discardable memory |
| // must have been mapped via Map(). |
| @@ -99,6 +116,14 @@ class BASE_EXPORT DiscardableSharedMemory { |
| virtual Time Now() const; |
| SharedMemory shared_memory_; |
| + size_t mapped_size_; |
| + size_t locked_page_count_; |
| +#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.
|
| + std::set<size_t> locked_pages_; |
| +#endif |
| + // 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.
|
| + // synchronized somehow. Use a collision warner to detect incorrect usage. |
| + DFAKE_MUTEX(thread_collision_warner_); |
| Time last_known_usage_; |
| DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |