| OLD | NEW |
| 1 // Copyright (c) 2006-2008 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/shared_memory.h" | 5 #include "base/shared_memory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 | 11 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 // static | 67 // static |
| 68 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 68 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 69 DCHECK(handle != NULL); | 69 DCHECK(handle != NULL); |
| 70 ::CloseHandle(handle); | 70 ::CloseHandle(handle); |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { | 73 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { |
| 74 return CreateAnonymous(size) && Map(size); | 74 return CreateAnonymous(size) && Map(size); |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool SharedMemory::CreateAnonymous(uint32 size) { | 77 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 78 return CreateNamed("", false, size); | 78 DCHECK(!options.executable); |
| 79 } | |
| 80 | |
| 81 bool SharedMemory::CreateNamed(const std::string& name, | |
| 82 bool open_existing, uint32 size) { | |
| 83 DCHECK(!mapped_file_); | 79 DCHECK(!mapped_file_); |
| 84 if (size == 0) | 80 if (options.size == 0) |
| 85 return false; | 81 return false; |
| 86 | 82 |
| 87 // NaCl's memory allocator requires 0mod64K alignment and size for | 83 // NaCl's memory allocator requires 0mod64K alignment and size for |
| 88 // shared memory objects. To allow passing shared memory to NaCl, | 84 // shared memory objects. To allow passing shared memory to NaCl, |
| 89 // therefore we round the size actually created to the nearest 64K unit. | 85 // therefore we round the size actually created to the nearest 64K unit. |
| 90 // To avoid client impact, we continue to retain the size as the | 86 // To avoid client impact, we continue to retain the size as the |
| 91 // actual requested size. | 87 // actual requested size. |
| 92 uint32 rounded_size = (size + 0xffff) & ~0xffff; | 88 uint32 rounded_size = (options.size + 0xffff) & ~0xffff; |
| 93 name_ = ASCIIToWide(name); | 89 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); |
| 94 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, | 90 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
| 95 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), | 91 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), |
| 96 name_.empty() ? NULL : name_.c_str()); | 92 name_.empty() ? NULL : name_.c_str()); |
| 97 if (!mapped_file_) | 93 if (!mapped_file_) |
| 98 return false; | 94 return false; |
| 99 | 95 |
| 100 created_size_ = size; | 96 created_size_ = options.size; |
| 101 | 97 |
| 102 // Check if the shared memory pre-exists. | 98 // Check if the shared memory pre-exists. |
| 103 if (GetLastError() == ERROR_ALREADY_EXISTS) { | 99 if (GetLastError() == ERROR_ALREADY_EXISTS) { |
| 104 // If the file already existed, set created_size_ to 0 to show that | 100 // If the file already existed, set created_size_ to 0 to show that |
| 105 // we don't know the size. | 101 // we don't know the size. |
| 106 created_size_ = 0; | 102 created_size_ = 0; |
| 107 if (!open_existing) { | 103 if (!options.open_existing) { |
| 108 Close(); | 104 Close(); |
| 109 return false; | 105 return false; |
| 110 } | 106 } |
| 111 } | 107 } |
| 112 | 108 |
| 113 return true; | 109 return true; |
| 114 } | 110 } |
| 115 | 111 |
| 116 bool SharedMemory::Delete(const std::string& name) { | 112 bool SharedMemory::Delete(const std::string& name) { |
| 117 // intentionally empty -- there is nothing for us to do on Windows. | 113 // intentionally empty -- there is nothing for us to do on Windows. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void SharedMemory::Unlock() { | 215 void SharedMemory::Unlock() { |
| 220 DCHECK(lock_ != NULL); | 216 DCHECK(lock_ != NULL); |
| 221 ReleaseMutex(lock_); | 217 ReleaseMutex(lock_); |
| 222 } | 218 } |
| 223 | 219 |
| 224 SharedMemoryHandle SharedMemory::handle() const { | 220 SharedMemoryHandle SharedMemory::handle() const { |
| 225 return mapped_file_; | 221 return mapped_file_; |
| 226 } | 222 } |
| 227 | 223 |
| 228 } // namespace base | 224 } // namespace base |
| OLD | NEW |