| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/edk/embedder/platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 14 #include "base/process/process_handle.h" | 14 #include "base/process/process_handle.h" |
| 15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "mojo/edk/embedder/platform_handle_utils.h" | 16 #include "mojo/edk/embedder/platform_handle_utils.h" |
| 17 | 17 |
| 18 #if defined(OS_NACL) |
| 19 // For getpagesize() on NaCl. |
| 20 #include <unistd.h> |
| 21 #endif |
| 22 |
| 18 namespace mojo { | 23 namespace mojo { |
| 19 namespace edk { | 24 namespace edk { |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 // Takes ownership of |memory_handle|. | 28 // Takes ownership of |memory_handle|. |
| 24 ScopedPlatformHandle SharedMemoryToPlatformHandle( | 29 ScopedPlatformHandle SharedMemoryToPlatformHandle( |
| 25 base::SharedMemoryHandle memory_handle) { | 30 base::SharedMemoryHandle memory_handle) { |
| 26 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | 31 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) |
| 27 return ScopedPlatformHandle(PlatformHandle(memory_handle.fd)); | 32 return ScopedPlatformHandle(PlatformHandle(memory_handle.fd)); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } | 286 } |
| 282 | 287 |
| 283 size_t PlatformSharedBufferMapping::GetLength() const { | 288 size_t PlatformSharedBufferMapping::GetLength() const { |
| 284 return length_; | 289 return length_; |
| 285 } | 290 } |
| 286 | 291 |
| 287 bool PlatformSharedBufferMapping::Map() { | 292 bool PlatformSharedBufferMapping::Map() { |
| 288 // Mojo shared buffers can be mapped at any offset. However, | 293 // Mojo shared buffers can be mapped at any offset. However, |
| 289 // base::SharedMemory must be mapped at a page boundary. So calculate what the | 294 // base::SharedMemory must be mapped at a page boundary. So calculate what the |
| 290 // nearest whole page offset is, and build a mapping that's offset from that. | 295 // nearest whole page offset is, and build a mapping that's offset from that. |
| 291 size_t offset_rounding = offset_ % base::SysInfo::VMAllocationGranularity(); | 296 #if defined(OS_NACL) |
| 297 // base::SysInfo isn't available under NaCl. |
| 298 size_t page_size = getpagesize(); |
| 299 #else |
| 300 size_t page_size = base::SysInfo::VMAllocationGranularity(); |
| 301 #endif |
| 302 size_t offset_rounding = offset_ % page_size; |
| 292 size_t real_offset = offset_ - offset_rounding; | 303 size_t real_offset = offset_ - offset_rounding; |
| 293 size_t real_length = length_ + offset_rounding; | 304 size_t real_length = length_ + offset_rounding; |
| 294 | 305 |
| 295 if (!shared_memory_.MapAt(static_cast<off_t>(real_offset), real_length)) | 306 if (!shared_memory_.MapAt(static_cast<off_t>(real_offset), real_length)) |
| 296 return false; | 307 return false; |
| 297 | 308 |
| 298 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding; | 309 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding; |
| 299 return true; | 310 return true; |
| 300 } | 311 } |
| 301 | 312 |
| 302 void PlatformSharedBufferMapping::Unmap() { | 313 void PlatformSharedBufferMapping::Unmap() { |
| 303 shared_memory_.Unmap(); | 314 shared_memory_.Unmap(); |
| 304 } | 315 } |
| 305 | 316 |
| 306 } // namespace edk | 317 } // namespace edk |
| 307 } // namespace mojo | 318 } // namespace mojo |
| OLD | NEW |