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 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
7 #include <aclapi.h> | 7 #include <aclapi.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 return memory_info.RegionSize - (static_cast<char*>(address) - | 22 return memory_info.RegionSize - (static_cast<char*>(address) - |
23 static_cast<char*>(memory_info.AllocationBase)); | 23 static_cast<char*>(memory_info.AllocationBase)); |
24 } | 24 } |
25 | 25 |
26 } // namespace. | 26 } // namespace. |
27 | 27 |
28 namespace base { | 28 namespace base { |
29 | 29 |
30 SharedMemory::SharedMemory() | 30 SharedMemory::SharedMemory() |
31 : mapped_file_(NULL), | 31 : mapped_file_(NULL), |
| 32 mapped_size_(0), |
32 memory_(NULL), | 33 memory_(NULL), |
33 read_only_(false), | 34 read_only_(false), |
34 mapped_size_(0), | 35 requested_size_(0) {} |
35 requested_size_(0), | |
36 lock_(NULL) { | |
37 } | |
38 | 36 |
39 SharedMemory::SharedMemory(const std::wstring& name) | 37 SharedMemory::SharedMemory(const std::wstring& name) |
40 : mapped_file_(NULL), | 38 : name_(name), |
| 39 mapped_file_(NULL), |
| 40 mapped_size_(0), |
41 memory_(NULL), | 41 memory_(NULL), |
42 read_only_(false), | 42 read_only_(false), |
43 requested_size_(0), | 43 requested_size_(0) {} |
| 44 |
| 45 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
| 46 : mapped_file_(handle), |
44 mapped_size_(0), | 47 mapped_size_(0), |
45 lock_(NULL), | |
46 name_(name) { | |
47 } | |
48 | |
49 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) | |
50 : mapped_file_(handle), | |
51 memory_(NULL), | 48 memory_(NULL), |
52 read_only_(read_only), | 49 read_only_(read_only), |
53 requested_size_(0), | 50 requested_size_(0) {} |
54 mapped_size_(0), | |
55 lock_(NULL) { | |
56 } | |
57 | 51 |
58 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, | 52 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, |
| 53 bool read_only, |
59 ProcessHandle process) | 54 ProcessHandle process) |
60 : mapped_file_(NULL), | 55 : mapped_file_(NULL), |
| 56 mapped_size_(0), |
61 memory_(NULL), | 57 memory_(NULL), |
62 read_only_(read_only), | 58 read_only_(read_only), |
63 requested_size_(0), | 59 requested_size_(0) { |
64 mapped_size_(0), | |
65 lock_(NULL) { | |
66 ::DuplicateHandle(process, handle, | 60 ::DuplicateHandle(process, handle, |
67 GetCurrentProcess(), &mapped_file_, | 61 GetCurrentProcess(), &mapped_file_, |
68 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | | 62 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
69 FILE_MAP_WRITE, | 63 FILE_MAP_WRITE, |
70 FALSE, 0); | 64 FALSE, 0); |
71 } | 65 } |
72 | 66 |
73 SharedMemory::~SharedMemory() { | 67 SharedMemory::~SharedMemory() { |
74 Unmap(); | 68 Unmap(); |
75 Close(); | 69 Close(); |
76 if (lock_ != NULL) | |
77 CloseHandle(lock_); | |
78 } | 70 } |
79 | 71 |
80 // static | 72 // static |
81 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 73 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
82 return handle != NULL; | 74 return handle != NULL; |
83 } | 75 } |
84 | 76 |
85 // static | 77 // static |
86 SharedMemoryHandle SharedMemory::NULLHandle() { | 78 SharedMemoryHandle SharedMemory::NULLHandle() { |
87 return NULL; | 79 return NULL; |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 } | 255 } |
264 | 256 |
265 | 257 |
266 void SharedMemory::Close() { | 258 void SharedMemory::Close() { |
267 if (mapped_file_ != NULL) { | 259 if (mapped_file_ != NULL) { |
268 CloseHandle(mapped_file_); | 260 CloseHandle(mapped_file_); |
269 mapped_file_ = NULL; | 261 mapped_file_ = NULL; |
270 } | 262 } |
271 } | 263 } |
272 | 264 |
273 void SharedMemory::LockDeprecated() { | |
274 if (lock_ == NULL) { | |
275 std::wstring name = name_; | |
276 name.append(L"lock"); | |
277 lock_ = CreateMutex(NULL, FALSE, name.c_str()); | |
278 if (lock_ == NULL) { | |
279 DPLOG(ERROR) << "Could not create mutex."; | |
280 NOTREACHED(); | |
281 return; // There is nothing good we can do here. | |
282 } | |
283 } | |
284 DWORD result = WaitForSingleObject(lock_, INFINITE); | |
285 DCHECK_EQ(result, WAIT_OBJECT_0); | |
286 } | |
287 | |
288 void SharedMemory::UnlockDeprecated() { | |
289 DCHECK(lock_ != NULL); | |
290 ReleaseMutex(lock_); | |
291 } | |
292 | |
293 SharedMemoryHandle SharedMemory::handle() const { | 265 SharedMemoryHandle SharedMemory::handle() const { |
294 return mapped_file_; | 266 return mapped_file_; |
295 } | 267 } |
296 | 268 |
297 } // namespace base | 269 } // namespace base |
OLD | NEW |