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

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

Issue 27265002: Implement SharedMemory::NewAnonymousReadOnly(contents). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Try to fix Android and Windows Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | base/memory/shared_memory_nacl.cc » ('j') | base/memory/shared_memory_posix.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_H_ 5 #ifndef BASE_MEMORY_SHARED_MEMORY_H_
6 #define BASE_MEMORY_SHARED_MEMORY_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_H_
7 7
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 9
10 #include <string> 10 #include <string>
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 73
74 #if defined(OS_WIN) 74 #if defined(OS_WIN)
75 // Similar to the default constructor, except that this allows for 75 // Similar to the default constructor, except that this allows for
76 // calling Lock() to acquire the named mutex before either Create or Open 76 // calling Lock() to acquire the named mutex before either Create or Open
77 // are called on Windows. 77 // are called on Windows.
78 explicit SharedMemory(const std::wstring& name); 78 explicit SharedMemory(const std::wstring& name);
79 #endif 79 #endif
80 80
81 // Create a new SharedMemory object from an existing, open 81 // Create a new SharedMemory object from an existing, open
82 // shared memory file. 82 // shared memory file.
83 //
84 // WARNING: This does not reduce the OS-level permissions on the handle; it
85 // only affects how the SharedMemory will be mmapped. Use
86 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK
87 // that |read_only| matches the permissions of the handle.
83 SharedMemory(SharedMemoryHandle handle, bool read_only); 88 SharedMemory(SharedMemoryHandle handle, bool read_only);
84 89
85 // Create a new SharedMemory object from an existing, open 90 // Create a new SharedMemory object from an existing, open
86 // shared memory file that was created by a remote process and not shared 91 // shared memory file that was created by a remote process and not shared
87 // to the current process. 92 // to the current process.
88 SharedMemory(SharedMemoryHandle handle, bool read_only, 93 SharedMemory(SharedMemoryHandle handle, bool read_only,
89 ProcessHandle process); 94 ProcessHandle process);
90 95
91 // Closes any open files. 96 // Closes any open files.
92 ~SharedMemory(); 97 ~SharedMemory();
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 // up on the same filesystem. 189 // up on the same filesystem.
185 SharedMemoryId id() const { return inode_; } 190 SharedMemoryId id() const { return inode_; }
186 #endif 191 #endif
187 192
188 // Closes the open shared memory segment. 193 // Closes the open shared memory segment.
189 // It is safe to call Close repeatedly. 194 // It is safe to call Close repeatedly.
190 void Close(); 195 void Close();
191 196
192 // Shares the shared memory to another process. Attempts 197 // Shares the shared memory to another process. Attempts
193 // to create a platform-specific new_handle which can be 198 // to create a platform-specific new_handle which can be
199 // used in a remote process to read the shared memory
200 // file. new_handle is an output parameter to receive
201 // the handle for use in the remote process.
202 // Returns true on success, false otherwise.
203 bool ShareReadOnlyToProcess(ProcessHandle process,
204 SharedMemoryHandle* new_handle) {
205 return ShareToProcessCommon(process, new_handle, false, SHARE_READONLY);
206 }
207
208 // Logically equivalent to:
209 // bool ok = ShareReadOnlyToProcess(process, new_handle);
210 // Close();
211 // return ok;
212 // Note that the memory is unmapped by calling this method, regardless of the
213 // return value.
214 bool GiveReadOnlyToProcess(ProcessHandle process,
215 SharedMemoryHandle* new_handle) {
216 return ShareToProcessCommon(process, new_handle, true, SHARE_READONLY);
217 }
218
219 // Shares the shared memory to another process. Attempts
220 // to create a platform-specific new_handle which can be
194 // used in a remote process to access the shared memory 221 // used in a remote process to access the shared memory
195 // file. new_handle is an ouput parameter to receive 222 // file. new_handle is an output parameter to receive
196 // the handle for use in the remote process. 223 // the handle for use in the remote process.
197 // Returns true on success, false otherwise. 224 // Returns true on success, false otherwise.
198 bool ShareToProcess(ProcessHandle process, 225 bool ShareToProcess(ProcessHandle process,
199 SharedMemoryHandle* new_handle) { 226 SharedMemoryHandle* new_handle) {
200 return ShareToProcessCommon(process, new_handle, false); 227 return ShareToProcessCommon(process, new_handle, false, SHARE_CURRENT_MODE);
201 } 228 }
202 229
203 // Logically equivalent to: 230 // Logically equivalent to:
204 // bool ok = ShareToProcess(process, new_handle); 231 // bool ok = ShareToProcess(process, new_handle);
205 // Close(); 232 // Close();
206 // return ok; 233 // return ok;
207 // Note that the memory is unmapped by calling this method, regardless of the 234 // Note that the memory is unmapped by calling this method, regardless of the
208 // return value. 235 // return value.
209 bool GiveToProcess(ProcessHandle process, 236 bool GiveToProcess(ProcessHandle process,
210 SharedMemoryHandle* new_handle) { 237 SharedMemoryHandle* new_handle) {
211 return ShareToProcessCommon(process, new_handle, true); 238 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE);
212 } 239 }
213 240
214 // Locks the shared memory. 241 // Locks the shared memory.
215 // 242 //
216 // WARNING: on POSIX the memory locking primitive only works across 243 // WARNING: on POSIX the memory locking primitive only works across
217 // processes, not across threads. The Lock method is not currently 244 // processes, not across threads. The Lock method is not currently
218 // used in inner loops, so we protect against multiple threads in a 245 // used in inner loops, so we protect against multiple threads in a
219 // critical section using a class global lock. 246 // critical section using a class global lock.
220 void Lock(); 247 void Lock();
221 248
222 #if defined(OS_WIN) 249 #if defined(OS_WIN)
223 // A Lock() implementation with a timeout that also allows setting 250 // A Lock() implementation with a timeout that also allows setting
224 // security attributes on the mutex. sec_attr may be NULL. 251 // security attributes on the mutex. sec_attr may be NULL.
225 // Returns true if the Lock() has been acquired, false if the timeout was 252 // Returns true if the Lock() has been acquired, false if the timeout was
226 // reached. 253 // reached.
227 bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr); 254 bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr);
228 #endif 255 #endif
229 256
230 // Releases the shared memory lock. 257 // Releases the shared memory lock.
231 void Unlock(); 258 void Unlock();
232 259
233 private: 260 private:
234 #if defined(OS_POSIX) && !defined(OS_NACL) 261 #if defined(OS_POSIX) && !defined(OS_NACL)
235 bool PrepareMapFile(FILE *fp); 262 bool PrepareMapFile(FILE *fp);
236 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); 263 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path);
237 void LockOrUnlockCommon(int function); 264 void LockOrUnlockCommon(int function);
238 #endif 265 #endif
266 enum ShareMode {
267 SHARE_READONLY,
268 SHARE_CURRENT_MODE,
269 };
239 bool ShareToProcessCommon(ProcessHandle process, 270 bool ShareToProcessCommon(ProcessHandle process,
240 SharedMemoryHandle* new_handle, 271 SharedMemoryHandle* new_handle,
241 bool close_self); 272 bool close_self,
273 ShareMode);
242 274
243 #if defined(OS_WIN) 275 #if defined(OS_WIN)
244 std::wstring name_; 276 std::wstring name_;
245 HANDLE mapped_file_; 277 HANDLE mapped_file_;
246 #elif defined(OS_POSIX) 278 #elif defined(OS_POSIX)
247 int mapped_file_; 279 int mapped_file_;
248 ino_t inode_; 280 ino_t inode_;
249 #endif 281 #endif
250 size_t mapped_size_; 282 size_t mapped_size_;
251 void* memory_; 283 void* memory_;
(...skipping 20 matching lines...) Expand all
272 } 304 }
273 305
274 private: 306 private:
275 SharedMemory* shared_memory_; 307 SharedMemory* shared_memory_;
276 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); 308 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock);
277 }; 309 };
278 310
279 } // namespace base 311 } // namespace base
280 312
281 #endif // BASE_MEMORY_SHARED_MEMORY_H_ 313 #endif // BASE_MEMORY_SHARED_MEMORY_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/shared_memory_nacl.cc » ('j') | base/memory/shared_memory_posix.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698