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