Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: base/memory/shared_memory_handle.h

Issue 2555483002: Add POSIX shared memory support for Mac (Closed)
Patch Set: Remove GetType(). Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
Robert Sesek 2016/12/06 21:45:55 Is this comment still relevant? I don't think we n
lawrencewu 2016/12/06 22:33:24 Don't think so -- removed.
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
86 // The default constructor returns an invalid SharedMemoryHandle. 97 // The default constructor returns an invalid SharedMemoryHandle.
87 SharedMemoryHandle(); 98 SharedMemoryHandle();
88 99
100 // Constructs a SharedMemoryHandle backed by the components of a
101 // FileDescriptor. The newly created instance has the same ownership semantics
102 // as base::FileDescriptor. This typically means that the SharedMemoryHandle
103 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's
104 // common for existing code to make shallow copies of SharedMemoryHandle, and
105 // the one that is finally passed into a base::SharedMemory is the one that
106 // "consumes" the fd.
107 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor);
108
89 // Makes a Mach-based SharedMemoryHandle of the given size. On error, 109 // Makes a Mach-based SharedMemoryHandle of the given size. On error,
90 // subsequent calls to IsValid() return false. 110 // subsequent calls to IsValid() return false.
91 explicit SharedMemoryHandle(mach_vm_size_t size); 111 explicit SharedMemoryHandle(mach_vm_size_t size);
92 112
93 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry 113 // 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|. 114 // in the task with process id |pid|. The memory region has size |size|.
95 SharedMemoryHandle(mach_port_t memory_object, 115 SharedMemoryHandle(mach_port_t memory_object,
96 mach_vm_size_t size, 116 mach_vm_size_t size,
97 base::ProcessId pid); 117 base::ProcessId pid);
98 118
99 // Standard copy constructor. The new instance shares the underlying OS 119 // Standard copy constructor. The new instance shares the underlying OS
100 // primitives. 120 // primitives.
101 SharedMemoryHandle(const SharedMemoryHandle& handle); 121 SharedMemoryHandle(const SharedMemoryHandle& handle);
102 122
103 // Standard assignment operator. The updated instance shares the underlying 123 // Standard assignment operator. The updated instance shares the underlying
104 // OS primitives. 124 // OS primitives.
105 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); 125 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
106 126
107 // Duplicates the underlying OS resources. 127 // Duplicates the underlying OS resources.
108 SharedMemoryHandle Duplicate() const; 128 SharedMemoryHandle Duplicate() const;
109 129
110 // Comparison operators. 130 // Comparison operators.
111 bool operator==(const SharedMemoryHandle& handle) const; 131 bool operator==(const SharedMemoryHandle& handle) const;
112 bool operator!=(const SharedMemoryHandle& handle) const; 132 bool operator!=(const SharedMemoryHandle& handle) const;
113 133
114 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle 134 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle
115 // is backed by a valid OS primitive, it becomes immutable. 135 // is backed by a valid OS primitive, it becomes immutable.
116 bool IsValid() const; 136 bool IsValid() const;
117 137
138 // Sets the POSIX fd backing the SharedMemoryHandle. Requires that the
139 // SharedMemoryHandle be backed by a POSIX fd.
140 void SetFileHandle(int fd, bool auto_close);
141
142 // This method assumes that the SharedMemoryHandle is backed by a POSIX fd.
143 // This is eventually no longer going to be true, so please avoid adding new
144 // uses of this method.
145 const FileDescriptor GetFileDescriptor() const;
146
118 // Exposed so that the SharedMemoryHandle can be transported between 147 // Exposed so that the SharedMemoryHandle can be transported between
119 // processes. 148 // processes.
120 mach_port_t GetMemoryObject() const; 149 mach_port_t GetMemoryObject() const;
121 150
122 // Returns false on a failure to determine the size. On success, populates the 151 // Returns false on a failure to determine the size. On success, populates the
123 // output variable |size|. Returns 0 if the handle is invalid. 152 // output variable |size|.
124 bool GetSize(size_t* size) const; 153 bool GetSize(size_t* size) const;
125 154
126 // The SharedMemoryHandle must be valid. 155 // The SharedMemoryHandle must be valid.
127 // Returns whether the SharedMemoryHandle was successfully mapped into memory. 156 // Returns whether the SharedMemoryHandle was successfully mapped into memory.
128 // On success, |memory| is an output variable that contains the start of the 157 // On success, |memory| is an output variable that contains the start of the
129 // mapped memory. 158 // mapped memory.
130 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); 159 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only);
131 160
132 // Closes the underlying OS primitive. 161 // Closes the underlying OS primitive.
133 void Close() const; 162 void Close() const;
134 163
135 void SetOwnershipPassesToIPC(bool ownership_passes); 164 void SetOwnershipPassesToIPC(bool ownership_passes);
136 bool OwnershipPassesToIPC() const; 165 bool OwnershipPassesToIPC() const;
137 166
138 private: 167 private:
168 friend class SharedMemory;
169
139 // Shared code between copy constructor and operator=. 170 // Shared code between copy constructor and operator=.
140 void CopyRelevantData(const SharedMemoryHandle& handle); 171 void CopyRelevantData(const SharedMemoryHandle& handle);
141 172
142 mach_port_t memory_object_ = MACH_PORT_NULL; 173 Type type_;
143 174
144 // The size of the shared memory region when |type_| is MACH. Only 175 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a
145 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 176 // mach port. |type_| determines the backing member.
146 mach_vm_size_t size_ = 0; 177 union {
178 FileDescriptor file_descriptor_;
147 179
148 // The pid of the process in which |memory_object_| is usable. Only 180 struct {
149 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 181 mach_port_t memory_object_;
150 base::ProcessId pid_ = 0;
151 182
152 // Whether passing this object as a parameter to an IPC message passes 183 // 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 184 // relevant if |memory_object_| is not |MACH_PORT_NULL|.
154 // the behavior of the |auto_close| parameter of FileDescriptor. 185 mach_vm_size_t size_;
155 // Defaults to |false|. 186
156 bool ownership_passes_to_ipc_ = false; 187 // The pid of the process in which |memory_object_| is usable. Only
188 // relevant if |memory_object_| is not |MACH_PORT_NULL|.
189 base::ProcessId pid_;
190
191 // Whether passing this object as a parameter to an IPC message passes
192 // ownership of |memory_object_| to the IPC stack. This is meant to mimic
193 // the behavior of the |auto_close| parameter of FileDescriptor.
194 // Defaults to |false|.
195 bool ownership_passes_to_ipc_;
196 };
197 };
157 }; 198 };
158 #endif 199 #endif
159 200
160 } // namespace base 201 } // namespace base
161 202
162 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 203 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698