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
3 // found in the LICENSE file.
5 #ifndef BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_
6 #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_
8 #include "base/base_export.h"
9 #include "base/logging.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/threading/thread_collision_warner.h"
12 #include "base/time/time.h"
18 // Define DISCARDABLE_SHARED_MEMORY_SHRINKING if platform supports shrinking
19 // of discardable shared memory segments.
20 #if defined(OS_POSIX) && !defined(OS_ANDROID)
21 #define DISCARDABLE_SHARED_MEMORY_SHRINKING
26 // Platform abstraction for discardable shared memory.
28 // This class is not thread-safe. Clients are responsible for synchronizing
29 // access to an instance of this class.
30 class BASE_EXPORT DiscardableSharedMemory
{
32 enum LockResult
{ SUCCESS
, PURGED
, FAILED
};
34 DiscardableSharedMemory();
36 // Create a new DiscardableSharedMemory object from an existing, open shared
37 // memory file. Memory must be locked.
38 explicit DiscardableSharedMemory(SharedMemoryHandle handle
);
40 // Closes any open files.
41 virtual ~DiscardableSharedMemory();
43 // Creates and maps a locked DiscardableSharedMemory object with |size|.
44 // Returns true on success and false on failure.
45 bool CreateAndMap(size_t size
);
47 // Maps the locked discardable memory into the caller's address space.
48 // Returns true on success, false otherwise.
49 bool Map(size_t size
);
51 // The actual size of the mapped memory (may be larger than requested).
52 size_t mapped_size() const { return mapped_size_
; }
54 // Returns a shared memory handle for this DiscardableSharedMemory object.
55 SharedMemoryHandle
handle() const { return shared_memory_
.handle(); }
57 // Locks a range of memory so that it will not be purged by the system.
58 // The range of memory must be unlocked. The result of trying to lock an
59 // already locked range is undefined. |offset| and |length| must both be
60 // a multiple of the page size as returned by GetPageSize().
61 // Passing 0 for |length| means "everything onward".
62 // Returns SUCCESS if range was successfully locked and the memory is still
63 // resident, PURGED if range was successfully locked but has been purged
64 // since last time it was locked and FAILED if range could not be locked.
65 // Locking can fail for two reasons; object might have been purged, our
66 // last known usage timestamp might be out of date. Last known usage time
67 // is updated to the actual last usage timestamp if memory is still resident
69 LockResult
Lock(size_t offset
, size_t length
);
71 // Unlock a previously successfully locked range of memory. The range of
72 // memory must be locked. The result of trying to unlock a not
73 // previously locked range is undefined.
74 // |offset| and |length| must both be a multiple of the page size as returned
76 // Passing 0 for |length| means "everything onward".
77 void Unlock(size_t offset
, size_t length
);
79 // Gets a pointer to the opened discardable memory space. Discardable memory
80 // must have been mapped via Map().
83 // Returns the last known usage time for DiscardableSharedMemory object. This
84 // may be earlier than the "true" usage time when memory has been used by a
85 // different process. Returns NULL time if purged.
86 Time
last_known_usage() const { return last_known_usage_
; }
88 // This returns true and sets |last_known_usage_| to 0 if
89 // DiscardableSharedMemory object was successfully purged. Purging can fail
90 // for two reasons; object might be locked or our last known usage timestamp
91 // might be out of date. Last known usage time is updated to |current_time|
92 // if locked or the actual last usage timestamp if unlocked. It is often
93 // necessary to call this function twice for the object to successfully be
94 // purged. First call, updates |last_known_usage_|. Second call, successfully
95 // purges the object using the updated |last_known_usage_|.
96 // Note: there is no guarantee that multiple calls to this function will
97 // successfully purge object. DiscardableSharedMemory object might be locked
98 // or another thread/process might be able to lock and unlock it in between
100 bool Purge(Time current_time
);
102 // Returns true if memory is still resident.
103 bool IsMemoryResident() const;
105 // Closes the open discardable memory segment.
106 // It is safe to call Close repeatedly.
109 // Shares the discardable memory segment to another process. Attempts to
110 // create a platform-specific |new_handle| which can be used in a remote
111 // process to access the discardable memory segment. |new_handle| is an
112 // output parameter to receive the handle for use in the remote process.
113 // Returns true on success, false otherwise.
114 bool ShareToProcess(ProcessHandle process_handle
,
115 SharedMemoryHandle
* new_handle
) {
116 return shared_memory_
.ShareToProcess(process_handle
, new_handle
);
119 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
120 // Release as much memory as possible to the OS. The change in size will
121 // be reflected by the return value of mapped_size().
126 // Virtual for tests.
127 virtual Time
Now() const;
129 SharedMemory shared_memory_
;
131 size_t locked_page_count_
;
133 std::set
<size_t> locked_pages_
;
135 // Implementation is not thread-safe but still usable if clients are
136 // synchronized somehow. Use a collision warner to detect incorrect usage.
137 DFAKE_MUTEX(thread_collision_warner_
);
138 Time last_known_usage_
;
140 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory
);
145 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_