OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SHARED_MEMORY_H_ | 5 #ifndef BASE_SHARED_MEMORY_H_ |
6 #define BASE_SHARED_MEMORY_H_ | 6 #define BASE_SHARED_MEMORY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... | |
31 #elif defined(OS_POSIX) | 31 #elif defined(OS_POSIX) |
32 // A SharedMemoryId is sufficient to identify a given shared memory segment on a | 32 // A SharedMemoryId is sufficient to identify a given shared memory segment on a |
33 // system, but insufficient to map it. | 33 // system, but insufficient to map it. |
34 typedef FileDescriptor SharedMemoryHandle; | 34 typedef FileDescriptor SharedMemoryHandle; |
35 typedef ino_t SharedMemoryId; | 35 typedef ino_t SharedMemoryId; |
36 // On POSIX, the lock is implemented as a lockf() on the mapped file, | 36 // On POSIX, the lock is implemented as a lockf() on the mapped file, |
37 // so no additional member (or definition of SharedMemoryLock) is | 37 // so no additional member (or definition of SharedMemoryLock) is |
38 // needed. | 38 // needed. |
39 #endif | 39 #endif |
40 | 40 |
41 // Options for creating a shared memory object. | |
42 struct SharedMemoryCreateOptions { | |
Mark Mentovai
2011/12/01 21:26:01
To promote more efficient memory use, please order
| |
43 SharedMemoryCreateOptions() : name(NULL), open_existing(false), | |
44 size(0), executable(false) {} | |
45 | |
46 // If NULL, the object is anonymous. This pointer is owned by the caller | |
47 // and must live through the call to Create(). | |
48 const std::string* name; | |
49 | |
50 // If true, and the shared memory already exists, Create() will open the | |
51 // existing shared memory and ignore the size parameter. If false, | |
52 // shared memory must not exist. This flag is meaningless unless name is | |
53 // non-NULL. | |
54 bool open_existing; | |
55 | |
56 // Size of the shared memory object to be created. | |
57 // When opening an existing object, this has no effect. | |
58 uint32 size; | |
59 | |
60 // If true, mappings might need to be made executable later. | |
61 bool executable; | |
62 }; | |
63 | |
41 // Platform abstraction for shared memory. Provides a C++ wrapper | 64 // Platform abstraction for shared memory. Provides a C++ wrapper |
42 // around the OS primitive for a memory mapped file. | 65 // around the OS primitive for a memory mapped file. |
43 class BASE_EXPORT SharedMemory { | 66 class BASE_EXPORT SharedMemory { |
44 public: | 67 public: |
45 SharedMemory(); | 68 SharedMemory(); |
46 | 69 |
47 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
48 // Similar to the default constructor, except that this allows for | 71 // Similar to the default constructor, except that this allows for |
49 // calling Lock() to acquire the named mutex before either Create or Open | 72 // calling Lock() to acquire the named mutex before either Create or Open |
50 // are called on Windows. | 73 // are called on Windows. |
(...skipping 16 matching lines...) Expand all Loading... | |
67 // Return true iff the given handle is valid (i.e. not the distingished | 90 // Return true iff the given handle is valid (i.e. not the distingished |
68 // invalid value; NULL for a HANDLE and -1 for a file descriptor) | 91 // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
69 static bool IsHandleValid(const SharedMemoryHandle& handle); | 92 static bool IsHandleValid(const SharedMemoryHandle& handle); |
70 | 93 |
71 // Returns invalid handle (see comment above for exact definition). | 94 // Returns invalid handle (see comment above for exact definition). |
72 static SharedMemoryHandle NULLHandle(); | 95 static SharedMemoryHandle NULLHandle(); |
73 | 96 |
74 // Closes a shared memory handle. | 97 // Closes a shared memory handle. |
75 static void CloseHandle(const SharedMemoryHandle& handle); | 98 static void CloseHandle(const SharedMemoryHandle& handle); |
76 | 99 |
100 // Creates a shared memory object as described by the options struct. | |
101 // Returns true on success and false on failure. | |
102 bool Create(const SharedMemoryCreateOptions& options); | |
103 | |
77 // Creates and maps an anonymous shared memory segment of size size. | 104 // Creates and maps an anonymous shared memory segment of size size. |
78 // Returns true on success and false on failure. | 105 // Returns true on success and false on failure. |
79 bool CreateAndMapAnonymous(uint32 size); | 106 bool CreateAndMapAnonymous(uint32 size); |
80 | 107 |
81 // Creates an anonymous shared memory segment of size size. | 108 // Creates an anonymous shared memory segment of size size. |
82 // Returns true on success and false on failure. | 109 // Returns true on success and false on failure. |
83 bool CreateAnonymous(uint32 size); | 110 bool CreateAnonymous(uint32 size) { |
111 SharedMemoryCreateOptions options; | |
112 options.size = size; | |
113 return Create(options); | |
114 } | |
84 | 115 |
85 // Creates or opens a shared memory segment based on a name. | 116 // Creates or opens a shared memory segment based on a name. |
86 // If open_existing is true, and the shared memory already exists, | 117 // If open_existing is true, and the shared memory already exists, |
87 // opens the existing shared memory and ignores the size parameter. | 118 // opens the existing shared memory and ignores the size parameter. |
88 // If open_existing is false, shared memory must not exist. | 119 // If open_existing is false, shared memory must not exist. |
89 // size is the size of the block to be created. | 120 // size is the size of the block to be created. |
90 // Returns true on success, false on failure. | 121 // Returns true on success, false on failure. |
91 bool CreateNamed(const std::string& name, bool open_existing, uint32 size); | 122 bool CreateNamed(const std::string& name, bool open_existing, uint32 size) { |
123 SharedMemoryCreateOptions options; | |
124 options.name = &name; | |
125 options.open_existing = open_existing; | |
126 options.size = size; | |
127 return Create(options); | |
128 } | |
92 | 129 |
93 // Deletes resources associated with a shared memory segment based on name. | 130 // Deletes resources associated with a shared memory segment based on name. |
94 // Not all platforms require this call. | 131 // Not all platforms require this call. |
95 bool Delete(const std::string& name); | 132 bool Delete(const std::string& name); |
96 | 133 |
97 // Opens a shared memory segment based on a name. | 134 // Opens a shared memory segment based on a name. |
98 // If read_only is true, opens for read-only access. | 135 // If read_only is true, opens for read-only access. |
99 // Returns true on success, false on failure. | 136 // Returns true on success, false on failure. |
100 bool Open(const std::string& name, bool read_only); | 137 bool Open(const std::string& name, bool read_only); |
101 | 138 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 263 } |
227 | 264 |
228 private: | 265 private: |
229 SharedMemory* shared_memory_; | 266 SharedMemory* shared_memory_; |
230 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); | 267 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); |
231 }; | 268 }; |
232 | 269 |
233 } // namespace base | 270 } // namespace base |
234 | 271 |
235 #endif // BASE_SHARED_MEMORY_H_ | 272 #endif // BASE_SHARED_MEMORY_H_ |
OLD | NEW |