| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 | |
| 9 #include <limits> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/sys_info.h" | |
| 13 #include "mojo/edk/embedder/platform_handle.h" | |
| 14 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace embedder { | |
| 18 | |
| 19 // SimplePlatformSharedBuffer -------------------------------------------------- | |
| 20 | |
| 21 bool SimplePlatformSharedBuffer::Init() { | |
| 22 DCHECK(!handle_.is_valid()); | |
| 23 | |
| 24 // TODO(vtl): Currently, we only support mapping up to 2^32-1 bytes. | |
| 25 if (static_cast<uint64_t>(num_bytes_) > | |
| 26 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) { | |
| 27 return false; | |
| 28 } | |
| 29 | |
| 30 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to | |
| 31 // share read-only to other processes, we'll have to name our file mapping | |
| 32 // object. | |
| 33 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a | |
| 34 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge | |
| 35 // when we get there. crbug.com/210609 | |
| 36 handle_.reset(PlatformHandle( | |
| 37 CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, | |
| 38 static_cast<DWORD>(num_bytes_), nullptr))); | |
| 39 if (!handle_.is_valid()) { | |
| 40 PLOG(ERROR) << "CreateFileMapping"; | |
| 41 return false; | |
| 42 } | |
| 43 | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 bool SimplePlatformSharedBuffer::InitFromPlatformHandle( | |
| 48 ScopedPlatformHandle platform_handle) { | |
| 49 DCHECK(!handle_.is_valid()); | |
| 50 | |
| 51 handle_ = platform_handle.Pass(); | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( | |
| 56 size_t offset, | |
| 57 size_t length) { | |
| 58 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | |
| 59 size_t real_offset = offset - offset_rounding; | |
| 60 size_t real_length = length + offset_rounding; | |
| 61 | |
| 62 // This should hold (since we checked |num_bytes| versus the maximum value of | |
| 63 // |off_t| on creation, but it never hurts to be paranoid. | |
| 64 DCHECK_LE(static_cast<uint64_t>(real_offset), | |
| 65 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())); | |
| 66 | |
| 67 void* real_base = | |
| 68 MapViewOfFile(handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, | |
| 69 static_cast<DWORD>(real_offset), real_length); | |
| 70 if (!real_base) { | |
| 71 PLOG(ERROR) << "MapViewOfFile"; | |
| 72 return nullptr; | |
| 73 } | |
| 74 | |
| 75 void* base = static_cast<char*>(real_base) + offset_rounding; | |
| 76 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( | |
| 77 base, length, real_base, real_length)); | |
| 78 } | |
| 79 | |
| 80 // SimplePlatformSharedBufferMapping ------------------------------------------- | |
| 81 | |
| 82 void SimplePlatformSharedBufferMapping::Unmap() { | |
| 83 BOOL result = UnmapViewOfFile(real_base_); | |
| 84 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; | |
| 85 } | |
| 86 | |
| 87 } // namespace embedder | |
| 88 } // namespace mojo | |
| OLD | NEW |