Chromium Code Reviews| 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/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 { | |
| 11 | |
| 12 // Returns the length of the memory section starting at the supplied address. | |
| 13 size_t GetMemorySectionSize(void* address) { | |
| 14 MEMORY_BASIC_INFORMATION memory_info; | |
| 15 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) | |
| 16 return 0; | |
| 17 return memory_info.RegionSize - (static_cast<char*>(address) - | |
| 18 static_cast<char*>(memory_info.AllocationBase)); | |
| 19 } | |
|
cpu_(ooo_6.6-7.5)
2013/03/21 17:14:22
I only see one usage and it that case address == a
jschuh
2013/03/21 19:32:06
Oh, there is only one use at this point so I can m
cpu_(ooo_6.6-7.5)
2013/03/21 20:06:15
standalone is fine.
On 2013/03/21 19:32:06, Justi
| |
| 20 | |
| 21 } // namespace. | |
| 22 | |
| 10 namespace base { | 23 namespace base { |
| 11 | 24 |
| 12 SharedMemory::SharedMemory() | 25 SharedMemory::SharedMemory() |
| 13 : mapped_file_(NULL), | 26 : mapped_file_(NULL), |
| 14 memory_(NULL), | 27 memory_(NULL), |
| 15 read_only_(false), | 28 read_only_(false), |
| 16 created_size_(0), | 29 mapped_size_(0), |
| 30 requested_size_(0), | |
| 17 lock_(NULL) { | 31 lock_(NULL) { |
| 18 } | 32 } |
| 19 | 33 |
| 20 SharedMemory::SharedMemory(const std::wstring& name) | 34 SharedMemory::SharedMemory(const std::wstring& name) |
| 21 : mapped_file_(NULL), | 35 : mapped_file_(NULL), |
| 22 memory_(NULL), | 36 memory_(NULL), |
| 23 read_only_(false), | 37 read_only_(false), |
| 24 created_size_(0), | 38 requested_size_(0), |
| 39 mapped_size_(0), | |
| 25 lock_(NULL), | 40 lock_(NULL), |
| 26 name_(name) { | 41 name_(name) { |
| 27 } | 42 } |
| 28 | 43 |
| 29 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) | 44 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 30 : mapped_file_(handle), | 45 : mapped_file_(handle), |
| 31 memory_(NULL), | 46 memory_(NULL), |
| 32 read_only_(read_only), | 47 read_only_(read_only), |
| 33 created_size_(0), | 48 requested_size_(0), |
| 49 mapped_size_(0), | |
| 34 lock_(NULL) { | 50 lock_(NULL) { |
| 35 } | 51 } |
| 36 | 52 |
| 37 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, | 53 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 38 ProcessHandle process) | 54 ProcessHandle process) |
| 39 : mapped_file_(NULL), | 55 : mapped_file_(NULL), |
| 40 memory_(NULL), | 56 memory_(NULL), |
| 41 read_only_(read_only), | 57 read_only_(read_only), |
| 42 created_size_(0), | 58 requested_size_(0), |
| 59 mapped_size_(0), | |
| 43 lock_(NULL) { | 60 lock_(NULL) { |
| 44 ::DuplicateHandle(process, handle, | 61 ::DuplicateHandle(process, handle, |
| 45 GetCurrentProcess(), &mapped_file_, | 62 GetCurrentProcess(), &mapped_file_, |
| 46 STANDARD_RIGHTS_REQUIRED | | 63 STANDARD_RIGHTS_REQUIRED | |
| 47 (read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), | 64 (read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), |
| 48 FALSE, 0); | 65 FALSE, 0); |
| 49 } | 66 } |
| 50 | 67 |
| 51 SharedMemory::~SharedMemory() { | 68 SharedMemory::~SharedMemory() { |
| 52 Close(); | 69 Close(); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 68 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 85 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 69 DCHECK(handle != NULL); | 86 DCHECK(handle != NULL); |
| 70 ::CloseHandle(handle); | 87 ::CloseHandle(handle); |
| 71 } | 88 } |
| 72 | 89 |
| 73 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 90 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
| 74 return CreateAnonymous(size) && Map(size); | 91 return CreateAnonymous(size) && Map(size); |
| 75 } | 92 } |
| 76 | 93 |
| 77 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 94 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 95 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here, | |
| 96 // wasting 32k per mapping on average. | |
| 97 static const size_t kSectionMask = 65536 - 1; | |
| 78 DCHECK(!options.executable); | 98 DCHECK(!options.executable); |
| 79 DCHECK(!mapped_file_); | 99 DCHECK(!mapped_file_); |
| 80 if (options.size == 0) | 100 if (options.size == 0) |
| 81 return false; | 101 return false; |
| 82 | 102 |
| 83 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 103 // Check maximum accounting for overflow. |
| 104 if (options.size > | |
| 105 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) | |
| 84 return false; | 106 return false; |
| 85 | 107 |
| 86 // NaCl's memory allocator requires 0mod64K alignment and size for | 108 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; |
| 87 // shared memory objects. To allow passing shared memory to NaCl, | |
| 88 // therefore we round the size actually created to the nearest 64K unit. | |
| 89 // To avoid client impact, we continue to retain the size as the | |
| 90 // actual requested size. | |
| 91 uint32 rounded_size = (options.size + 0xffff) & ~0xffff; | |
| 92 if (rounded_size < options.size) | |
| 93 return false; | |
| 94 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); | 109 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); |
| 95 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, | 110 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
| 96 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), | 111 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), |
| 97 name_.empty() ? NULL : name_.c_str()); | 112 name_.empty() ? NULL : name_.c_str()); |
| 98 if (!mapped_file_) | 113 if (!mapped_file_) |
| 99 return false; | 114 return false; |
| 100 | 115 |
| 101 created_size_ = options.size; | 116 requested_size_ = options.size; |
| 102 | 117 |
| 103 // Check if the shared memory pre-exists. | 118 // Check if the shared memory pre-exists. |
| 104 if (GetLastError() == ERROR_ALREADY_EXISTS) { | 119 if (GetLastError() == ERROR_ALREADY_EXISTS) { |
| 105 // If the file already existed, set created_size_ to 0 to show that | 120 // If the file already existed, set requested_size_ to 0 to show that |
| 106 // we don't know the size. | 121 // we don't know the size. |
| 107 created_size_ = 0; | 122 requested_size_ = 0; |
| 108 if (!options.open_existing) { | 123 if (!options.open_existing) { |
| 109 Close(); | 124 Close(); |
| 110 return false; | 125 return false; |
| 111 } | 126 } |
| 112 } | 127 } |
| 113 | 128 |
| 114 return true; | 129 return true; |
| 115 } | 130 } |
| 116 | 131 |
| 117 bool SharedMemory::Delete(const std::string& name) { | 132 bool SharedMemory::Delete(const std::string& name) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 142 return false; | 157 return false; |
| 143 | 158 |
| 144 memory_ = MapViewOfFile(mapped_file_, | 159 memory_ = MapViewOfFile(mapped_file_, |
| 145 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, | 160 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, |
| 146 static_cast<uint64>(offset) >> 32, | 161 static_cast<uint64>(offset) >> 32, |
| 147 static_cast<DWORD>(offset), | 162 static_cast<DWORD>(offset), |
| 148 bytes); | 163 bytes); |
| 149 if (memory_ != NULL) { | 164 if (memory_ != NULL) { |
| 150 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 165 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 151 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 166 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 167 mapped_size_ = GetMemorySectionSize(memory_); | |
| 152 return true; | 168 return true; |
| 153 } | 169 } |
| 154 return false; | 170 return false; |
| 155 } | 171 } |
| 156 | 172 |
| 157 bool SharedMemory::Unmap() { | 173 bool SharedMemory::Unmap() { |
| 158 if (memory_ == NULL) | 174 if (memory_ == NULL) |
| 159 return false; | 175 return false; |
| 160 | 176 |
| 161 UnmapViewOfFile(memory_); | 177 UnmapViewOfFile(memory_); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 void SharedMemory::Unlock() { | 244 void SharedMemory::Unlock() { |
| 229 DCHECK(lock_ != NULL); | 245 DCHECK(lock_ != NULL); |
| 230 ReleaseMutex(lock_); | 246 ReleaseMutex(lock_); |
| 231 } | 247 } |
| 232 | 248 |
| 233 SharedMemoryHandle SharedMemory::handle() const { | 249 SharedMemoryHandle SharedMemory::handle() const { |
| 234 return mapped_file_; | 250 return mapped_file_; |
| 235 } | 251 } |
| 236 | 252 |
| 237 } // namespace base | 253 } // namespace base |
| OLD | NEW |