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

Side by Side Diff: base/shared_memory.h

Issue 1689012: Move common code into process_util.cc (Closed)
Patch Set: More fixes Created 10 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
« no previous file with comments | « base/process_util_win.cc ('k') | chrome/browser/memory_details_mac.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 7
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 9
10 #if defined(OS_POSIX) 10 #if defined(OS_POSIX)
(...skipping 22 matching lines...) Expand all
33 typedef ino_t SharedMemoryId; 33 typedef ino_t SharedMemoryId;
34 // On POSIX, the lock is implemented as a lockf() on the mapped file, 34 // On POSIX, the lock is implemented as a lockf() on the mapped file,
35 // so no additional member (or definition of SharedMemoryLock) is 35 // so no additional member (or definition of SharedMemoryLock) is
36 // needed. 36 // needed.
37 #endif 37 #endif
38 38
39 // Platform abstraction for shared memory. Provides a C++ wrapper 39 // Platform abstraction for shared memory. Provides a C++ wrapper
40 // around the OS primitive for a memory mapped file. 40 // around the OS primitive for a memory mapped file.
41 class SharedMemory { 41 class SharedMemory {
42 public: 42 public:
43 // Create a new SharedMemory object.
44 SharedMemory(); 43 SharedMemory();
45 44
46 // Create a new SharedMemory object from an existing, open 45 // Create a new SharedMemory object from an existing, open
47 // shared memory file. 46 // shared memory file.
48 SharedMemory(SharedMemoryHandle handle, bool read_only); 47 SharedMemory(SharedMemoryHandle handle, bool read_only);
49 48
50 // Create a new SharedMemory object from an existing, open 49 // Create a new SharedMemory object from an existing, open
51 // shared memory file that was created by a remote process and not shared 50 // shared memory file that was created by a remote process and not shared
52 // to the current process. 51 // to the current process.
53 SharedMemory(SharedMemoryHandle handle, bool read_only, 52 SharedMemory(SharedMemoryHandle handle, bool read_only,
54 base::ProcessHandle process); 53 ProcessHandle process);
55 54
56 // Destructor. Will close any open files. 55 // Closes any open files.
57 ~SharedMemory(); 56 ~SharedMemory();
58 57
59 // Return true iff the given handle is valid (i.e. not the distingished 58 // Return true iff the given handle is valid (i.e. not the distingished
60 // invalid value; NULL for a HANDLE and -1 for a file descriptor) 59 // invalid value; NULL for a HANDLE and -1 for a file descriptor)
61 static bool IsHandleValid(const SharedMemoryHandle& handle); 60 static bool IsHandleValid(const SharedMemoryHandle& handle);
62 61
63 // Return invalid handle (see comment above for exact definition). 62 // Returns invalid handle (see comment above for exact definition).
64 static SharedMemoryHandle NULLHandle(); 63 static SharedMemoryHandle NULLHandle();
65 64
66 // Close a shared memory handle. 65 // Closes a shared memory handle.
67 static void CloseHandle(const SharedMemoryHandle& handle); 66 static void CloseHandle(const SharedMemoryHandle& handle);
68 67
69 // Creates or opens a shared memory segment based on a name. 68 // Creates or opens a shared memory segment based on a name.
70 // If read_only is true, opens the memory as read-only. 69 // If read_only is true, opens the memory as read-only.
71 // If open_existing is true, and the shared memory already exists, 70 // If open_existing is true, and the shared memory already exists,
72 // opens the existing shared memory and ignores the size parameter. 71 // opens the existing shared memory and ignores the size parameter.
73 // If name is the empty string, use a unique name. 72 // If name is the empty string, use a unique name.
74 // Returns true on success, false on failure. 73 // Returns true on success, false on failure.
75 bool Create(const std::wstring& name, bool read_only, bool open_existing, 74 bool Create(const std::wstring& name, bool read_only, bool open_existing,
76 uint32 size); 75 uint32 size);
(...skipping 22 matching lines...) Expand all
99 // Note: This size is only available to the creator of the 98 // Note: This size is only available to the creator of the
100 // shared memory, and not to those that opened shared memory 99 // shared memory, and not to those that opened shared memory
101 // created externally. 100 // created externally.
102 // Returns 0 if not opened or unknown. 101 // Returns 0 if not opened or unknown.
103 uint32 max_size() const { return max_size_; } 102 uint32 max_size() const { return max_size_; }
104 103
105 // Gets a pointer to the opened memory space if it has been 104 // Gets a pointer to the opened memory space if it has been
106 // Mapped via Map(). Returns NULL if it is not mapped. 105 // Mapped via Map(). Returns NULL if it is not mapped.
107 void *memory() const { return memory_; } 106 void *memory() const { return memory_; }
108 107
109 // Get access to the underlying OS handle for this segment. 108 // Returns the underlying OS handle for this segment.
110 // Use of this handle for anything other than an opaque 109 // Use of this handle for anything other than an opaque
111 // identifier is not portable. 110 // identifier is not portable.
112 SharedMemoryHandle handle() const; 111 SharedMemoryHandle handle() const;
113 112
114 #if defined(OS_POSIX) 113 #if defined(OS_POSIX)
115 // Return a unique identifier for this shared memory segment. Inode numbers 114 // Returns a unique identifier for this shared memory segment. Inode numbers
116 // are technically only unique to a single filesystem. However, we always 115 // are technically only unique to a single filesystem. However, we always
117 // allocate shared memory backing files from the same directory, so will end 116 // allocate shared memory backing files from the same directory, so will end
118 // up on the same filesystem. 117 // up on the same filesystem.
119 SharedMemoryId id() const { return inode_; } 118 SharedMemoryId id() const { return inode_; }
120 #endif 119 #endif
121 120
122 // Closes the open shared memory segment. 121 // Closes the open shared memory segment.
123 // It is safe to call Close repeatedly. 122 // It is safe to call Close repeatedly.
124 void Close(); 123 void Close();
125 124
126 // Share the shared memory to another process. Attempts 125 // Shares the shared memory to another process. Attempts
127 // to create a platform-specific new_handle which can be 126 // to create a platform-specific new_handle which can be
128 // used in a remote process to access the shared memory 127 // used in a remote process to access the shared memory
129 // file. new_handle is an ouput parameter to receive 128 // file. new_handle is an ouput parameter to receive
130 // the handle for use in the remote process. 129 // the handle for use in the remote process.
131 // Returns true on success, false otherwise. 130 // Returns true on success, false otherwise.
132 bool ShareToProcess(base::ProcessHandle process, 131 bool ShareToProcess(ProcessHandle process,
133 SharedMemoryHandle* new_handle) { 132 SharedMemoryHandle* new_handle) {
134 return ShareToProcessCommon(process, new_handle, false); 133 return ShareToProcessCommon(process, new_handle, false);
135 } 134 }
136 135
137 // Logically equivalent to: 136 // Logically equivalent to:
138 // bool ok = ShareToProcess(process, new_handle); 137 // bool ok = ShareToProcess(process, new_handle);
139 // Close(); 138 // Close();
140 // return ok; 139 // return ok;
141 // Note that the memory is unmapped by calling this method, regardless of the 140 // Note that the memory is unmapped by calling this method, regardless of the
142 // return value. 141 // return value.
143 bool GiveToProcess(ProcessHandle process, 142 bool GiveToProcess(ProcessHandle process,
144 SharedMemoryHandle* new_handle) { 143 SharedMemoryHandle* new_handle) {
145 return ShareToProcessCommon(process, new_handle, true); 144 return ShareToProcessCommon(process, new_handle, true);
146 } 145 }
147 146
148 // Lock the shared memory. 147 // Locks the shared memory.
149 // This is a cross-process lock which may be recursively 148 // This is a cross-process lock which may be recursively
150 // locked by the same thread. 149 // locked by the same thread.
151 // TODO(port): 150 // TODO(port):
152 // WARNING: on POSIX the lock only works across processes, not 151 // WARNING: on POSIX the lock only works across processes, not
153 // across threads. 2 threads in the same process can both grab the 152 // across threads. 2 threads in the same process can both grab the
154 // lock at the same time. There are several solutions for this 153 // lock at the same time. There are several solutions for this
155 // (futex, lockf+anon_semaphore) but none are both clean and common 154 // (futex, lockf+anon_semaphore) but none are both clean and common
156 // across Mac and Linux. 155 // across Mac and Linux.
157 void Lock(); 156 void Lock();
158 157
159 // Release the shared memory lock. 158 // Releases the shared memory lock.
160 void Unlock(); 159 void Unlock();
161 160
162 private: 161 private:
163 #if defined(OS_POSIX) 162 #if defined(OS_POSIX)
164 bool CreateOrOpen(const std::wstring &name, int posix_flags, uint32 size); 163 bool CreateOrOpen(const std::wstring &name, int posix_flags, uint32 size);
165 bool FilePathForMemoryName(const std::wstring& memname, FilePath* path); 164 bool FilePathForMemoryName(const std::wstring& memname, FilePath* path);
166 void LockOrUnlockCommon(int function); 165 void LockOrUnlockCommon(int function);
167 166
168 #endif 167 #endif
169 bool ShareToProcessCommon(ProcessHandle process, 168 bool ShareToProcessCommon(ProcessHandle process,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 } 200 }
202 201
203 private: 202 private:
204 SharedMemory* shared_memory_; 203 SharedMemory* shared_memory_;
205 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); 204 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock);
206 }; 205 };
207 206
208 } // namespace base 207 } // namespace base
209 208
210 #endif // BASE_SHARED_MEMORY_H_ 209 #endif // BASE_SHARED_MEMORY_H_
OLDNEW
« no previous file with comments | « base/process_util_win.cc ('k') | chrome/browser/memory_details_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698