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

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

Issue 2875453002: Add a size parameter to SharedMemoryHandle. (Closed)
Patch Set: Fix IPC on POSIX. Created 3 years, 7 months 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 "base/unguessable_token.h" 10 #include "base/unguessable_token.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // Duplicates the underlying OS resource. Using the return value as a 64 // Duplicates the underlying OS resource. Using the return value as a
65 // parameter to an IPC message will cause the IPC subsystem to consume the OS 65 // parameter to an IPC message will cause the IPC subsystem to consume the OS
66 // resource. 66 // resource.
67 SharedMemoryHandle Duplicate() const; 67 SharedMemoryHandle Duplicate() const;
68 68
69 // Uniques identifies the shared memory region that the underlying OS resource 69 // Uniques identifies the shared memory region that the underlying OS resource
70 // points to. Multiple SharedMemoryHandles that point to the same shared 70 // points to. Multiple SharedMemoryHandles that point to the same shared
71 // memory region will have the same GUID. Preserved across IPC. 71 // memory region will have the same GUID. Preserved across IPC.
72 base::UnguessableToken GetGUID() const; 72 base::UnguessableToken GetGUID() const;
73 73
74 // Returns false on a failure to determine the size. On success, populates the
75 // output variable |size|.
Nico 2017/05/15 22:13:36 What?
erikchen 2017/05/15 22:57:18 Updated comment.
76 size_t GetSize() const;
77
74 #if defined(OS_MACOSX) && !defined(OS_IOS) 78 #if defined(OS_MACOSX) && !defined(OS_IOS)
75 enum Type { 79 enum Type {
76 // The SharedMemoryHandle is backed by a POSIX fd. 80 // The SharedMemoryHandle is backed by a POSIX fd.
77 POSIX, 81 POSIX,
78 // The SharedMemoryHandle is backed by the Mach primitive "memory object". 82 // The SharedMemoryHandle is backed by the Mach primitive "memory object".
79 MACH, 83 MACH,
80 }; 84 };
81 85
82 // Constructs a SharedMemoryHandle backed by the components of a 86 // Constructs a SharedMemoryHandle backed by the components of a
83 // FileDescriptor. The newly created instance has the same ownership semantics 87 // FileDescriptor. The newly created instance has the same ownership semantics
84 // as base::FileDescriptor. This typically means that the SharedMemoryHandle 88 // as base::FileDescriptor. This typically means that the SharedMemoryHandle
85 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's 89 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's
86 // common for existing code to make shallow copies of SharedMemoryHandle, and 90 // common for existing code to make shallow copies of SharedMemoryHandle, and
87 // the one that is finally passed into a base::SharedMemory is the one that 91 // the one that is finally passed into a base::SharedMemory is the one that
88 // "consumes" the fd. 92 // "consumes" the fd.
89 // |guid| uniquely identifies the shared memory region pointed to by the 93 // |guid| uniquely identifies the shared memory region pointed to by the
90 // underlying OS resource. If |file_descriptor| is associated with another 94 // underlying OS resource. If |file_descriptor| is associated with another
91 // SharedMemoryHandle, the caller must pass the |guid| of that 95 // SharedMemoryHandle, the caller must pass the |guid| of that
92 // SharedMemoryHandle. Otherwise, the caller should generate a new 96 // SharedMemoryHandle. Otherwise, the caller should generate a new
93 // UnguessableToken. 97 // UnguessableToken.
Nico 2017/05/15 22:13:36 Document size and what happens if the passed-in si
erikchen 2017/05/15 22:57:18 Done.
94 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, 98 SharedMemoryHandle(const base::FileDescriptor& file_descriptor,
99 size_t size,
95 const base::UnguessableToken& guid); 100 const base::UnguessableToken& guid);
96 101
97 // Makes a Mach-based SharedMemoryHandle of the given size. On error, 102 // Makes a Mach-based SharedMemoryHandle of the given size. On error,
98 // subsequent calls to IsValid() return false. 103 // subsequent calls to IsValid() return false.
99 SharedMemoryHandle(mach_vm_size_t size, const base::UnguessableToken& guid); 104 SharedMemoryHandle(mach_vm_size_t size, const base::UnguessableToken& guid);
100 105
101 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry 106 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry
102 // in the current task. The memory region has size |size|. 107 // in the current task. The memory region has size |size|.
103 SharedMemoryHandle(mach_port_t memory_object, 108 SharedMemoryHandle(mach_port_t memory_object,
104 mach_vm_size_t size, 109 mach_vm_size_t size,
105 const base::UnguessableToken& guid); 110 const base::UnguessableToken& guid);
106 111
107 // Exposed so that the SharedMemoryHandle can be transported between 112 // Exposed so that the SharedMemoryHandle can be transported between
108 // processes. 113 // processes.
109 mach_port_t GetMemoryObject() const; 114 mach_port_t GetMemoryObject() const;
110 115
111 // Returns false on a failure to determine the size. On success, populates the
112 // output variable |size|.
113 bool GetSize(size_t* size) const;
114
115 // The SharedMemoryHandle must be valid. 116 // The SharedMemoryHandle must be valid.
116 // Returns whether the SharedMemoryHandle was successfully mapped into memory. 117 // Returns whether the SharedMemoryHandle was successfully mapped into memory.
117 // On success, |memory| is an output variable that contains the start of the 118 // On success, |memory| is an output variable that contains the start of the
118 // mapped memory. 119 // mapped memory.
119 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); 120 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only);
120 #elif defined(OS_WIN) 121 #elif defined(OS_WIN)
121 // Takes implicit ownership of |h|. 122 // Takes implicit ownership of |h|.
122 // |guid| uniquely identifies the shared memory region pointed to by the 123 // |guid| uniquely identifies the shared memory region pointed to by the
123 // underlying OS resource. If the HANDLE is associated with another 124 // underlying OS resource. If the HANDLE is associated with another
124 // SharedMemoryHandle, the caller must pass the |guid| of that 125 // SharedMemoryHandle, the caller must pass the |guid| of that
125 // SharedMemoryHandle. Otherwise, the caller should generate a new 126 // SharedMemoryHandle. Otherwise, the caller should generate a new
126 // UnguessableToken. 127 // UnguessableToken.
127 SharedMemoryHandle(HANDLE h, const base::UnguessableToken& guid); 128 SharedMemoryHandle(HANDLE h, size_t size, const base::UnguessableToken& guid);
128 HANDLE GetHandle() const; 129 HANDLE GetHandle() const;
129 #else 130 #else
130 // |guid| uniquely identifies the shared memory region pointed to by the 131 // |guid| uniquely identifies the shared memory region pointed to by the
131 // underlying OS resource. If |file_descriptor| is associated with another 132 // underlying OS resource. If |file_descriptor| is associated with another
132 // SharedMemoryHandle, the caller must pass the |guid| of that 133 // SharedMemoryHandle, the caller must pass the |guid| of that
133 // SharedMemoryHandle. Otherwise, the caller should generate a new 134 // SharedMemoryHandle. Otherwise, the caller should generate a new
134 // UnguessableToken. 135 // UnguessableToken.
135 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, 136 SharedMemoryHandle(const base::FileDescriptor& file_descriptor,
137 size_t size,
136 const base::UnguessableToken& guid); 138 const base::UnguessableToken& guid);
137 139
138 // Creates a SharedMemoryHandle from an |fd| supplied from an external 140 // Creates a SharedMemoryHandle from an |fd| supplied from an external
139 // service. 141 // service.
140 static SharedMemoryHandle ImportHandle(int fd); 142 static SharedMemoryHandle ImportHandle(int fd, size_t size);
141 143
142 // Returns the underlying OS resource. 144 // Returns the underlying OS resource.
143 int GetHandle() const; 145 int GetHandle() const;
144 146
145 // Takes ownership of the OS resource.
146 void SetHandle(int fd);
147
148 // Invalidates [but doesn't close] the underlying OS resource. This will leak 147 // Invalidates [but doesn't close] the underlying OS resource. This will leak
149 // unless the caller is careful. 148 // unless the caller is careful.
150 int Release(); 149 int Release();
151 #endif 150 #endif
152 151
153 private: 152 private:
154 #if defined(OS_MACOSX) && !defined(OS_IOS) 153 #if defined(OS_MACOSX) && !defined(OS_IOS)
155 friend class SharedMemory; 154 friend class SharedMemory;
156 155
157 Type type_; 156 Type type_;
158 157
159 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a 158 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a
160 // mach port. |type_| determines the backing member. 159 // mach port. |type_| determines the backing member.
161 union { 160 union {
162 FileDescriptor file_descriptor_; 161 FileDescriptor file_descriptor_;
163 162
164 struct { 163 struct {
165 mach_port_t memory_object_; 164 mach_port_t memory_object_;
166 165
167 // The size of the shared memory region when |type_| is MACH. Only
168 // relevant if |memory_object_| is not |MACH_PORT_NULL|.
169 mach_vm_size_t size_;
170
171 // Whether passing this object as a parameter to an IPC message passes 166 // Whether passing this object as a parameter to an IPC message passes
172 // ownership of |memory_object_| to the IPC stack. This is meant to mimic 167 // ownership of |memory_object_| to the IPC stack. This is meant to mimic
173 // the behavior of the |auto_close| parameter of FileDescriptor. 168 // the behavior of the |auto_close| parameter of FileDescriptor.
174 // Defaults to |false|. 169 // Defaults to |false|.
175 bool ownership_passes_to_ipc_; 170 bool ownership_passes_to_ipc_;
176 }; 171 };
177 }; 172 };
178 #elif defined(OS_WIN) 173 #elif defined(OS_WIN)
179 HANDLE handle_; 174 HANDLE handle_;
180 175
181 // Whether passing this object as a parameter to an IPC message passes 176 // Whether passing this object as a parameter to an IPC message passes
182 // ownership of |handle_| to the IPC stack. This is meant to mimic the 177 // ownership of |handle_| to the IPC stack. This is meant to mimic the
183 // behavior of the |auto_close| parameter of FileDescriptor. This member only 178 // behavior of the |auto_close| parameter of FileDescriptor. This member only
184 // affects attachment-brokered SharedMemoryHandles. 179 // affects attachment-brokered SharedMemoryHandles.
185 // Defaults to |false|. 180 // Defaults to |false|.
186 bool ownership_passes_to_ipc_; 181 bool ownership_passes_to_ipc_;
187 #else 182 #else
188 FileDescriptor file_descriptor_; 183 FileDescriptor file_descriptor_;
189 #endif 184 #endif
190 185
191 base::UnguessableToken guid_; 186 base::UnguessableToken guid_;
187
188 // The size of the region referenced by the SharedMemoryHandle.
189 size_t size_ = 0;
Nico 2017/05/15 22:13:36 Either use in-class initialization for all fields
erikchen 2017/05/15 22:57:18 all of them, it is.
192 }; 190 };
193 191
194 } // namespace base 192 } // namespace base
195 193
196 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 194 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698