OLD | NEW |
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> |
11 | 11 |
12 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 #include <semaphore.h> | 15 #include <semaphore.h> |
16 #endif | 16 #endif |
17 | 17 |
18 #include "base/base_export.h" | 18 #include "base/base_export.h" |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
20 #include "base/memory/shared_memory_handle.h" | |
21 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
22 | 21 |
23 #if defined(OS_POSIX) | 22 #if defined(OS_POSIX) |
24 #include "base/file_descriptor_posix.h" | 23 #include "base/file_descriptor_posix.h" |
25 #include "base/files/file_util.h" | 24 #include "base/files/file_util.h" |
26 #include "base/files/scoped_file.h" | 25 #include "base/files/scoped_file.h" |
27 #endif | 26 #endif |
28 | 27 |
29 namespace base { | 28 namespace base { |
30 | 29 |
31 class FilePath; | 30 class FilePath; |
32 | 31 |
| 32 // SharedMemoryHandle is a platform specific type which represents |
| 33 // the underlying OS handle to a shared memory segment. |
| 34 #if defined(OS_WIN) |
| 35 typedef HANDLE SharedMemoryHandle; |
| 36 #elif defined(OS_POSIX) |
| 37 typedef FileDescriptor SharedMemoryHandle; |
| 38 #endif |
| 39 |
33 // Options for creating a shared memory object. | 40 // Options for creating a shared memory object. |
34 struct SharedMemoryCreateOptions { | 41 struct SharedMemoryCreateOptions { |
35 SharedMemoryCreateOptions() | 42 SharedMemoryCreateOptions() |
36 : name_deprecated(NULL), | 43 : name_deprecated(NULL), |
37 size(0), | 44 size(0), |
38 open_existing_deprecated(false), | 45 open_existing_deprecated(false), |
39 executable(false), | 46 executable(false), |
40 share_read_only(false) {} | 47 share_read_only(false) {} |
41 | 48 |
42 // DEPRECATED (crbug.com/345734): | 49 // DEPRECATED (crbug.com/345734): |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 explicit SharedMemory(const std::wstring& name); | 82 explicit SharedMemory(const std::wstring& name); |
76 #endif | 83 #endif |
77 | 84 |
78 // Create a new SharedMemory object from an existing, open | 85 // Create a new SharedMemory object from an existing, open |
79 // shared memory file. | 86 // shared memory file. |
80 // | 87 // |
81 // WARNING: This does not reduce the OS-level permissions on the handle; it | 88 // WARNING: This does not reduce the OS-level permissions on the handle; it |
82 // only affects how the SharedMemory will be mmapped. Use | 89 // only affects how the SharedMemory will be mmapped. Use |
83 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK | 90 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK |
84 // that |read_only| matches the permissions of the handle. | 91 // that |read_only| matches the permissions of the handle. |
85 SharedMemory(const SharedMemoryHandle& handle, bool read_only); | 92 SharedMemory(SharedMemoryHandle handle, bool read_only); |
86 | 93 |
87 // Create a new SharedMemory object from an existing, open | 94 // Create a new SharedMemory object from an existing, open |
88 // shared memory file that was created by a remote process and not shared | 95 // shared memory file that was created by a remote process and not shared |
89 // to the current process. | 96 // to the current process. |
90 SharedMemory(const SharedMemoryHandle& handle, | 97 SharedMemory(SharedMemoryHandle handle, bool read_only, |
91 bool read_only, | |
92 ProcessHandle process); | 98 ProcessHandle process); |
93 | 99 |
94 // Closes any open files. | 100 // Closes any open files. |
95 ~SharedMemory(); | 101 ~SharedMemory(); |
96 | 102 |
97 // Return true iff the given handle is valid (i.e. not the distingished | 103 // Return true iff the given handle is valid (i.e. not the distingished |
98 // invalid value; NULL for a HANDLE and -1 for a file descriptor) | 104 // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
99 static bool IsHandleValid(const SharedMemoryHandle& handle); | 105 static bool IsHandleValid(const SharedMemoryHandle& handle); |
100 | 106 |
101 // Returns invalid handle (see comment above for exact definition). | 107 // Returns invalid handle (see comment above for exact definition). |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 size_t mapped_size_; | 281 size_t mapped_size_; |
276 void* memory_; | 282 void* memory_; |
277 bool read_only_; | 283 bool read_only_; |
278 size_t requested_size_; | 284 size_t requested_size_; |
279 | 285 |
280 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 286 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
281 }; | 287 }; |
282 } // namespace base | 288 } // namespace base |
283 | 289 |
284 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 290 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
OLD | NEW |