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

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

Issue 2550723002: Resurrect POSIX shared memory support on mac (Closed)
Patch Set: git cl format 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
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;
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);
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;
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 const FileDescriptor GetFileDescriptor() const;
152
118 // Exposed so that the SharedMemoryHandle can be transported between 153 // Exposed so that the SharedMemoryHandle can be transported between
119 // processes. 154 // processes.
120 mach_port_t GetMemoryObject() const; 155 mach_port_t GetMemoryObject() const;
121 156
122 // Returns false on a failure to determine the size. On success, populates the 157 // Returns false on a failure to determine the size. On success, populates the
123 // output variable |size|. Returns 0 if the handle is invalid. 158 // output variable |size|. Returns 0 if the handle is invalid.
124 bool GetSize(size_t* size) const; 159 bool GetSize(size_t* size) const;
125 160
126 // The SharedMemoryHandle must be valid. 161 // The SharedMemoryHandle must be valid.
127 // Returns whether the SharedMemoryHandle was successfully mapped into memory. 162 // Returns whether the SharedMemoryHandle was successfully mapped into memory.
128 // On success, |memory| is an output variable that contains the start of the 163 // On success, |memory| is an output variable that contains the start of the
129 // mapped memory. 164 // mapped memory.
130 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); 165 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only);
131 166
132 // Closes the underlying OS primitive. 167 // Closes the underlying OS primitive.
133 void Close() const; 168 void Close() const;
134 169
135 void SetOwnershipPassesToIPC(bool ownership_passes); 170 void SetOwnershipPassesToIPC(bool ownership_passes);
136 bool OwnershipPassesToIPC() const; 171 bool OwnershipPassesToIPC() const;
137 172
138 private: 173 private:
139 // Shared code between copy constructor and operator=. 174 // Shared code between copy constructor and operator=.
140 void CopyRelevantData(const SharedMemoryHandle& handle); 175 void CopyRelevantData(const SharedMemoryHandle& handle);
141 176
142 mach_port_t memory_object_ = MACH_PORT_NULL; 177 Type type_;
143 178
144 // The size of the shared memory region when |type_| is MACH. Only 179 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a
145 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 180 // mach port. |type_| determines the backing member.
146 mach_vm_size_t size_ = 0; 181 union {
182 FileDescriptor file_descriptor_;
147 183
148 // The pid of the process in which |memory_object_| is usable. Only 184 struct {
149 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 185 mach_port_t memory_object_;
150 base::ProcessId pid_ = 0;
151 186
152 // Whether passing this object as a parameter to an IPC message passes 187 // 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 188 // relevant if |memory_object_| is not |MACH_PORT_NULL|.
154 // the behavior of the |auto_close| parameter of FileDescriptor. 189 mach_vm_size_t size_;
155 // Defaults to |false|. 190
156 bool ownership_passes_to_ipc_ = false; 191 // The pid of the process in which |memory_object_| is usable. Only
192 // relevant if |memory_object_| is not |MACH_PORT_NULL|.
193 base::ProcessId pid_;
194
195 // Whether passing this object as a parameter to an IPC message passes
196 // ownership of |memory_object_| to the IPC stack. This is meant to mimic
197 // the behavior of the |auto_close| parameter of FileDescriptor.
198 // Defaults to |false|.
199 bool ownership_passes_to_ipc_;
200 };
201 };
157 }; 202 };
158 #endif 203 #endif
159 204
160 } // namespace base 205 } // namespace base
161 206
162 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 207 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698