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