| 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 14 matching lines...) Expand all Loading... |
| 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 memory_(NULL), | 32 memory_(NULL), |
| 33 read_only_(false), | 33 read_only_(false), |
| 34 mapped_size_(0), | 34 mapped_size_(0), |
| 35 requested_size_(0), | 35 requested_size_(0) { |
| 36 lock_(NULL) { | |
| 37 } | 36 } |
| 38 | 37 |
| 39 SharedMemory::SharedMemory(const std::wstring& name) | 38 SharedMemory::SharedMemory(const std::wstring& name) |
| 40 : mapped_file_(NULL), | 39 : mapped_file_(NULL), |
| 41 memory_(NULL), | 40 memory_(NULL), |
| 42 read_only_(false), | 41 read_only_(false), |
| 43 requested_size_(0), | 42 requested_size_(0), |
| 44 mapped_size_(0), | 43 mapped_size_(0), |
| 45 lock_(NULL), | |
| 46 name_(name) { | 44 name_(name) { |
| 47 } | 45 } |
| 48 | 46 |
| 49 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) | 47 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 50 : mapped_file_(handle), | 48 : mapped_file_(handle), |
| 51 memory_(NULL), | 49 memory_(NULL), |
| 52 read_only_(read_only), | 50 read_only_(read_only), |
| 53 requested_size_(0), | 51 requested_size_(0), |
| 54 mapped_size_(0), | 52 mapped_size_(0) { |
| 55 lock_(NULL) { | |
| 56 } | 53 } |
| 57 | 54 |
| 58 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, | 55 SharedMemory::SharedMemory(SharedMemoryHandle handle, |
| 56 bool read_only, |
| 59 ProcessHandle process) | 57 ProcessHandle process) |
| 60 : mapped_file_(NULL), | 58 : mapped_file_(NULL), |
| 61 memory_(NULL), | 59 memory_(NULL), |
| 62 read_only_(read_only), | 60 read_only_(read_only), |
| 63 requested_size_(0), | 61 requested_size_(0), |
| 64 mapped_size_(0), | 62 mapped_size_(0) { |
| 65 lock_(NULL) { | |
| 66 ::DuplicateHandle(process, handle, | 63 ::DuplicateHandle(process, handle, |
| 67 GetCurrentProcess(), &mapped_file_, | 64 GetCurrentProcess(), &mapped_file_, |
| 68 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | | 65 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
| 69 FILE_MAP_WRITE, | 66 FILE_MAP_WRITE, |
| 70 FALSE, 0); | 67 FALSE, 0); |
| 71 } | 68 } |
| 72 | 69 |
| 73 SharedMemory::~SharedMemory() { | 70 SharedMemory::~SharedMemory() { |
| 74 Unmap(); | 71 Unmap(); |
| 75 Close(); | 72 Close(); |
| 76 if (lock_ != NULL) | |
| 77 CloseHandle(lock_); | |
| 78 } | 73 } |
| 79 | 74 |
| 80 // static | 75 // static |
| 81 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 76 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 82 return handle != NULL; | 77 return handle != NULL; |
| 83 } | 78 } |
| 84 | 79 |
| 85 // static | 80 // static |
| 86 SharedMemoryHandle SharedMemory::NULLHandle() { | 81 SharedMemoryHandle SharedMemory::NULLHandle() { |
| 87 return NULL; | 82 return NULL; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 } | 258 } |
| 264 | 259 |
| 265 | 260 |
| 266 void SharedMemory::Close() { | 261 void SharedMemory::Close() { |
| 267 if (mapped_file_ != NULL) { | 262 if (mapped_file_ != NULL) { |
| 268 CloseHandle(mapped_file_); | 263 CloseHandle(mapped_file_); |
| 269 mapped_file_ = NULL; | 264 mapped_file_ = NULL; |
| 270 } | 265 } |
| 271 } | 266 } |
| 272 | 267 |
| 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 { | 268 SharedMemoryHandle SharedMemory::handle() const { |
| 294 return mapped_file_; | 269 return mapped_file_; |
| 295 } | 270 } |
| 296 | 271 |
| 297 } // namespace base | 272 } // namespace base |
| OLD | NEW |