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/system/raw_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/embedder/platform_handle.h" 14 #include "mojo/embedder/scoped_platform_handle.h" 15 16 namespace mojo { 17 namespace system { 18 19 // RawSharedBuffer ------------------------------------------------------------- 20 21 bool RawSharedBuffer::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(embedder::PlatformHandle(CreateFileMapping( 37 INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 38 static_cast<DWORD>(num_bytes_), NULL))); 39 if (!handle_.is_valid()) { 40 PLOG(ERROR) << "CreateFileMapping"; 41 return false; 42 } 43 44 return true; 45 } 46 47 bool RawSharedBuffer::InitFromPlatformHandle( 48 embedder::ScopedPlatformHandle platform_handle) { 49 DCHECK(!handle_.is_valid()); 50 51 // TODO(vtl): Implement. 52 NOTIMPLEMENTED(); 53 return false; 54 } 55 56 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImpl(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 = MapViewOfFile( 68 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 scoped_ptr<RawSharedBufferMapping>(); 73 } 74 75 void* base = static_cast<char*>(real_base) + offset_rounding; 76 return make_scoped_ptr( 77 new RawSharedBufferMapping(base, length, real_base, real_length)); 78 } 79 80 // RawSharedBufferMapping ------------------------------------------------------ 81 82 void RawSharedBufferMapping::Unmap() { 83 BOOL result = UnmapViewOfFile(real_base_); 84 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; 85 } 86 87 } // namespace system 88 } // namespace mojo 89