| 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 class Pickle; | 29 class Pickle; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // Whether passing this object as a parameter to an IPC message passes | 80 // Whether passing this object as a parameter to an IPC message passes |
| 79 // ownership of |handle_| to the IPC stack. This is meant to mimic the | 81 // ownership of |handle_| to the IPC stack. This is meant to mimic the |
| 80 // behavior of the |auto_close| parameter of FileDescriptor. This member only | 82 // behavior of the |auto_close| parameter of FileDescriptor. This member only |
| 81 // affects attachment-brokered SharedMemoryHandles. | 83 // affects attachment-brokered SharedMemoryHandles. |
| 82 // Defaults to |false|. | 84 // Defaults to |false|. |
| 83 bool ownership_passes_to_ipc_; | 85 bool ownership_passes_to_ipc_; |
| 84 }; | 86 }; |
| 85 #else | 87 #else |
| 86 class BASE_EXPORT SharedMemoryHandle { | 88 class BASE_EXPORT SharedMemoryHandle { |
| 87 public: | 89 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 |
| 88 // The default constructor returns an invalid SharedMemoryHandle. | 103 // The default constructor returns an invalid SharedMemoryHandle. |
| 89 SharedMemoryHandle(); | 104 SharedMemoryHandle(); |
| 90 | 105 |
| 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 |
| 91 // Makes a Mach-based SharedMemoryHandle of the given size. On error, | 116 // Makes a Mach-based SharedMemoryHandle of the given size. On error, |
| 92 // subsequent calls to IsValid() return false. | 117 // subsequent calls to IsValid() return false. |
| 93 explicit SharedMemoryHandle(mach_vm_size_t size); | 118 explicit SharedMemoryHandle(mach_vm_size_t size); |
| 94 | 119 |
| 95 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry | 120 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry |
| 96 // in the task with process id |pid|. The memory region has size |size|. | 121 // in the task with process id |pid|. The memory region has size |size|. |
| 97 SharedMemoryHandle(mach_port_t memory_object, | 122 SharedMemoryHandle(mach_port_t memory_object, |
| 98 mach_vm_size_t size, | 123 mach_vm_size_t size, |
| 99 base::ProcessId pid); | 124 base::ProcessId pid); |
| 100 | 125 |
| 101 // Standard copy constructor. The new instance shares the underlying OS | 126 // Standard copy constructor. The new instance shares the underlying OS |
| 102 // primitives. | 127 // primitives. |
| 103 SharedMemoryHandle(const SharedMemoryHandle& handle); | 128 SharedMemoryHandle(const SharedMemoryHandle& handle); |
| 104 | 129 |
| 105 // Standard assignment operator. The updated instance shares the underlying | 130 // Standard assignment operator. The updated instance shares the underlying |
| 106 // OS primitives. | 131 // OS primitives. |
| 107 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); | 132 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); |
| 108 | 133 |
| 109 // Duplicates the underlying OS resources. | 134 // Duplicates the underlying OS resources. |
| 110 SharedMemoryHandle Duplicate() const; | 135 SharedMemoryHandle Duplicate() const; |
| 111 | 136 |
| 112 // Comparison operators. | 137 // Comparison operators. |
| 113 bool operator==(const SharedMemoryHandle& handle) const; | 138 bool operator==(const SharedMemoryHandle& handle) const; |
| 114 bool operator!=(const SharedMemoryHandle& handle) const; | 139 bool operator!=(const SharedMemoryHandle& handle) const; |
| 115 | 140 |
| 141 // Returns the type. |
| 142 Type GetType() const; |
| 143 |
| 116 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle | 144 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle |
| 117 // is backed by a valid OS primitive, it becomes immutable. | 145 // is backed by a valid OS primitive, it becomes immutable. |
| 118 bool IsValid() const; | 146 bool IsValid() const; |
| 119 | 147 |
| 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 |
| 120 // Exposed so that the SharedMemoryHandle can be transported between | 157 // Exposed so that the SharedMemoryHandle can be transported between |
| 121 // processes. | 158 // processes. |
| 122 mach_port_t GetMemoryObject() const; | 159 mach_port_t GetMemoryObject() const; |
| 123 | 160 |
| 124 // Returns false on a failure to determine the size. On success, populates the | 161 // Returns false on a failure to determine the size. On success, populates the |
| 125 // output variable |size|. Returns 0 if the handle is invalid. | 162 // output variable |size|. |
| 126 bool GetSize(size_t* size) const; | 163 bool GetSize(size_t* size) const; |
| 127 | 164 |
| 128 // The SharedMemoryHandle must be valid. | 165 // The SharedMemoryHandle must be valid. |
| 129 // Returns whether the SharedMemoryHandle was successfully mapped into memory. | 166 // Returns whether the SharedMemoryHandle was successfully mapped into memory. |
| 130 // On success, |memory| is an output variable that contains the start of the | 167 // On success, |memory| is an output variable that contains the start of the |
| 131 // mapped memory. | 168 // mapped memory. |
| 132 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); | 169 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); |
| 133 | 170 |
| 134 // Closes the underlying OS primitive. | 171 // Closes the underlying OS primitive. |
| 135 void Close() const; | 172 void Close() const; |
| 136 | 173 |
| 137 void SetOwnershipPassesToIPC(bool ownership_passes); | 174 void SetOwnershipPassesToIPC(bool ownership_passes); |
| 138 bool OwnershipPassesToIPC() const; | 175 bool OwnershipPassesToIPC() const; |
| 139 | 176 |
| 140 private: | 177 private: |
| 141 // Shared code between copy constructor and operator=. | 178 // Shared code between copy constructor and operator=. |
| 142 void CopyRelevantData(const SharedMemoryHandle& handle); | 179 void CopyRelevantData(const SharedMemoryHandle& handle); |
| 143 | 180 |
| 144 mach_port_t memory_object_ = MACH_PORT_NULL; | 181 Type type_; |
| 145 | 182 |
| 146 // The size of the shared memory region when |type_| is MACH. Only | 183 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a |
| 147 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | 184 // mach port. |type_| determines the backing member. |
| 148 mach_vm_size_t size_ = 0; | 185 union { |
| 186 FileDescriptor file_descriptor_; |
| 149 | 187 |
| 150 // The pid of the process in which |memory_object_| is usable. Only | 188 struct { |
| 151 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | 189 mach_port_t memory_object_; |
| 152 base::ProcessId pid_ = 0; | |
| 153 | 190 |
| 154 // Whether passing this object as a parameter to an IPC message passes | 191 // The size of the shared memory region when |type_| is MACH. Only |
| 155 // ownership of |memory_object_| to the IPC stack. This is meant to mimic | 192 // relevant if |memory_object_| is not |MACH_PORT_NULL|. |
| 156 // the behavior of the |auto_close| parameter of FileDescriptor. | 193 mach_vm_size_t size_; |
| 157 // Defaults to |false|. | 194 |
| 158 bool ownership_passes_to_ipc_ = false; | 195 // The pid of the process in which |memory_object_| is usable. Only |
| 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 }; |
| 159 }; | 206 }; |
| 160 #endif | 207 #endif |
| 161 | 208 |
| 162 } // namespace base | 209 } // namespace base |
| 163 | 210 |
| 164 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 211 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
| OLD | NEW |