OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_SHARED_MEMORY_HANDLE_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 | 11 |
12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
13 #include <windows.h> | 13 #include <windows.h> |
14 #include "base/process/process_handle.h" | 14 #include "base/process/process_handle.h" |
15 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 15 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
16 #include <mach/mach.h> | 16 #include <mach/mach.h> |
17 #include <sys/types.h> | |
17 #include "base/base_export.h" | 18 #include "base/base_export.h" |
19 #include "base/file_descriptor_posix.h" | |
18 #include "base/macros.h" | 20 #include "base/macros.h" |
19 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
20 #elif defined(OS_POSIX) | 22 #elif defined(OS_POSIX) |
21 #include <sys/types.h> | 23 #include <sys/types.h> |
22 #include "base/file_descriptor_posix.h" | 24 #include "base/file_descriptor_posix.h" |
23 #endif | 25 #endif |
24 | 26 |
25 namespace base { | 27 namespace base { |
26 | 28 |
27 // SharedMemoryHandle is a platform specific type which represents | 29 // SharedMemoryHandle is a platform specific type which represents |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 // Whether passing this object as a parameter to an IPC message passes | 78 // Whether passing this object as a parameter to an IPC message passes |
77 // ownership of |handle_| to the IPC stack. This is meant to mimic the | 79 // ownership of |handle_| to the IPC stack. This is meant to mimic the |
78 // behavior of the |auto_close| parameter of FileDescriptor. This member only | 80 // behavior of the |auto_close| parameter of FileDescriptor. This member only |
79 // affects attachment-brokered SharedMemoryHandles. | 81 // affects attachment-brokered SharedMemoryHandles. |
80 // Defaults to |false|. | 82 // Defaults to |false|. |
81 bool ownership_passes_to_ipc_; | 83 bool ownership_passes_to_ipc_; |
82 }; | 84 }; |
83 #else | 85 #else |
84 class BASE_EXPORT SharedMemoryHandle { | 86 class BASE_EXPORT SharedMemoryHandle { |
85 public: | 87 public: |
88 // The values of these enums must not change, as they are used by the | |
89 // histogram OSX.SharedMemory.Mechanism. | |
90 enum Type { | |
91 // The SharedMemoryHandle is backed by a POSIX fd. | |
92 POSIX, | |
93 // The SharedMemoryHandle is backed by the Mach primitive "memory object". | |
94 MACH, | |
95 }; | |
96 static const int TypeMax = 2; | |
erikchen
2016/12/06 19:00:41
Is TypeMax needed? Since you don't need IPC, I thi
lawrencewu
2016/12/06 21:04:40
Removed.
| |
97 | |
98 // The format that should be used to transmit |Type| over the wire. | |
99 typedef int TypeWireFormat; | |
100 | |
86 // The default constructor returns an invalid SharedMemoryHandle. | 101 // The default constructor returns an invalid SharedMemoryHandle. |
87 SharedMemoryHandle(); | 102 SharedMemoryHandle(); |
88 | 103 |
104 // Constructs a SharedMemoryHandle backed by the components of a | |
105 // FileDescriptor. The newly created instance has the same ownership semantics | |
106 // as base::FileDescriptor. This typically means that the SharedMemoryHandle | |
107 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's | |
108 // common for existing code to make shallow copies of SharedMemoryHandle, and | |
109 // the one that is finally passed into a base::SharedMemory is the one that | |
110 // "consumes" the fd. | |
111 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); | |
112 SharedMemoryHandle(int fd, bool auto_close); | |
erikchen
2016/12/06 19:00:41
I think you probably don't need line #112, right?
lawrencewu
2016/12/06 21:04:40
Nope, removed and made existing calls use the firs
| |
113 | |
89 // Makes a Mach-based SharedMemoryHandle of the given size. On error, | 114 // Makes a Mach-based SharedMemoryHandle of the given size. On error, |
90 // subsequent calls to IsValid() return false. | 115 // subsequent calls to IsValid() return false. |
91 explicit SharedMemoryHandle(mach_vm_size_t size); | 116 explicit SharedMemoryHandle(mach_vm_size_t size); |
92 | 117 |
93 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry | 118 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry |
94 // in the task with process id |pid|. The memory region has size |size|. | 119 // in the task with process id |pid|. The memory region has size |size|. |
95 SharedMemoryHandle(mach_port_t memory_object, | 120 SharedMemoryHandle(mach_port_t memory_object, |
96 mach_vm_size_t size, | 121 mach_vm_size_t size, |
97 base::ProcessId pid); | 122 base::ProcessId pid); |
98 | 123 |
99 // Standard copy constructor. The new instance shares the underlying OS | 124 // Standard copy constructor. The new instance shares the underlying OS |
100 // primitives. | 125 // primitives. |
101 SharedMemoryHandle(const SharedMemoryHandle& handle); | 126 SharedMemoryHandle(const SharedMemoryHandle& handle); |
102 | 127 |
103 // Standard assignment operator. The updated instance shares the underlying | 128 // Standard assignment operator. The updated instance shares the underlying |
104 // OS primitives. | 129 // OS primitives. |
105 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); | 130 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); |
106 | 131 |
107 // Duplicates the underlying OS resources. | 132 // Duplicates the underlying OS resources. |
108 SharedMemoryHandle Duplicate() const; | 133 SharedMemoryHandle Duplicate() const; |
109 | 134 |
110 // Comparison operators. | 135 // Comparison operators. |
111 bool operator==(const SharedMemoryHandle& handle) const; | 136 bool operator==(const SharedMemoryHandle& handle) const; |
112 bool operator!=(const SharedMemoryHandle& handle) const; | 137 bool operator!=(const SharedMemoryHandle& handle) const; |
113 | 138 |
139 // Returns the type. | |
140 Type GetType() const; | |
erikchen
2016/12/06 19:00:41
Given that there's only 1 producer/consumer of POS
lawrencewu
2016/12/06 21:04:40
Nope, made private.
| |
141 | |
114 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle | 142 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle |
115 // is backed by a valid OS primitive, it becomes immutable. | 143 // is backed by a valid OS primitive, it becomes immutable. |
116 bool IsValid() const; | 144 bool IsValid() const; |
117 | 145 |
146 // Sets the POSIX fd backing the SharedMemoryHandle. Requires that the | |
147 // SharedMemoryHandle be backed by a POSIX fd. | |
148 void SetFileHandle(int fd, bool auto_close); | |
149 | |
150 // This method assumes that the SharedMemoryHandle is backed by a POSIX fd. | |
151 // This is eventually no longer going to be true, so please avoid adding new | |
152 // uses of this method. | |
153 const FileDescriptor GetFileDescriptor() const; | |
154 | |
118 // Exposed so that the SharedMemoryHandle can be transported between | 155 // Exposed so that the SharedMemoryHandle can be transported between |
119 // processes. | 156 // processes. |
120 mach_port_t GetMemoryObject() const; | 157 mach_port_t GetMemoryObject() const; |
121 | 158 |
122 // Returns false on a failure to determine the size. On success, populates the | 159 // Returns false on a failure to determine the size. On success, populates the |
123 // output variable |size|. Returns 0 if the handle is invalid. | 160 // output variable |size|. |
124 bool GetSize(size_t* size) const; | 161 bool GetSize(size_t* size) const; |
125 | 162 |
126 // The SharedMemoryHandle must be valid. | 163 // The SharedMemoryHandle must be valid. |
127 // Returns whether the SharedMemoryHandle was successfully mapped into memory. | 164 // Returns whether the SharedMemoryHandle was successfully mapped into memory. |
128 // On success, |memory| is an output variable that contains the start of the | 165 // On success, |memory| is an output variable that contains the start of the |
129 // mapped memory. | 166 // mapped memory. |
130 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); | 167 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); |
131 | 168 |
132 // Closes the underlying OS primitive. | 169 // Closes the underlying OS primitive. |
133 void Close() const; | 170 void Close() const; |
134 | 171 |
135 void SetOwnershipPassesToIPC(bool ownership_passes); | 172 void SetOwnershipPassesToIPC(bool ownership_passes); |
136 bool OwnershipPassesToIPC() const; | 173 bool OwnershipPassesToIPC() const; |
137 | 174 |
138 private: | 175 private: |
139 // Shared code between copy constructor and operator=. | 176 // Shared code between copy constructor and operator=. |
140 void CopyRelevantData(const SharedMemoryHandle& handle); | 177 void CopyRelevantData(const SharedMemoryHandle& handle); |
141 | 178 |
142 mach_port_t memory_object_ = MACH_PORT_NULL; | 179 Type type_; |
143 | 180 |
144 // The size of the shared memory region when |type_| is MACH. Only | 181 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a |
145 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | 182 // mach port. |type_| determines the backing member. |
146 mach_vm_size_t size_ = 0; | 183 union { |
184 FileDescriptor file_descriptor_; | |
147 | 185 |
148 // The pid of the process in which |memory_object_| is usable. Only | 186 struct { |
149 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | 187 mach_port_t memory_object_; |
150 base::ProcessId pid_ = 0; | |
151 | 188 |
152 // Whether passing this object as a parameter to an IPC message passes | 189 // The size of the shared memory region when |type_| is MACH. Only |
153 // ownership of |memory_object_| to the IPC stack. This is meant to mimic | 190 // relevant if |memory_object_| is not |MACH_PORT_NULL|. |
154 // the behavior of the |auto_close| parameter of FileDescriptor. | 191 mach_vm_size_t size_; |
155 // Defaults to |false|. | 192 |
156 bool ownership_passes_to_ipc_ = false; | 193 // The pid of the process in which |memory_object_| is usable. Only |
194 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | |
195 base::ProcessId pid_; | |
196 | |
197 // Whether passing this object as a parameter to an IPC message passes | |
198 // ownership of |memory_object_| to the IPC stack. This is meant to mimic | |
199 // the behavior of the |auto_close| parameter of FileDescriptor. | |
200 // Defaults to |false|. | |
201 bool ownership_passes_to_ipc_; | |
202 }; | |
203 }; | |
157 }; | 204 }; |
158 #endif | 205 #endif |
159 | 206 |
160 } // namespace base | 207 } // namespace base |
161 | 208 |
162 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 209 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
OLD | NEW |