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

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

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

Powered by Google App Engine
This is Rietveld 408576698