michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/sys_info.h" michael@0: #include "chrome/common/transport_dib.h" michael@0: michael@0: TransportDIB::TransportDIB() { michael@0: } michael@0: michael@0: TransportDIB::~TransportDIB() { michael@0: } michael@0: michael@0: TransportDIB::TransportDIB(HANDLE handle) michael@0: : shared_memory_(handle, false /* read write */) { michael@0: } michael@0: michael@0: // static michael@0: TransportDIB* TransportDIB::Create(size_t size, uint32_t sequence_num) { michael@0: size_t allocation_granularity = base::SysInfo::VMAllocationGranularity(); michael@0: size = size / allocation_granularity + 1; michael@0: size = size * allocation_granularity; michael@0: michael@0: TransportDIB* dib = new TransportDIB; michael@0: michael@0: if (!dib->shared_memory_.Create("", false /* read write */, michael@0: true /* open existing */, size)) { michael@0: delete dib; michael@0: return NULL; michael@0: } michael@0: michael@0: dib->size_ = size; michael@0: dib->sequence_num_ = sequence_num; michael@0: michael@0: return dib; michael@0: } michael@0: michael@0: // static michael@0: TransportDIB* TransportDIB::Map(TransportDIB::Handle handle) { michael@0: TransportDIB* dib = new TransportDIB(handle); michael@0: if (!dib->shared_memory_.Map(0 /* map whole shared memory segment */)) { michael@0: CHROMIUM_LOG(ERROR) << "Failed to map transport DIB" michael@0: << " handle:" << handle michael@0: << " error:" << GetLastError(); michael@0: delete dib; michael@0: return NULL; michael@0: } michael@0: michael@0: // There doesn't seem to be any way to find the size of the shared memory michael@0: // region! GetFileSize indicates that the handle is invalid. Thus, we michael@0: // conservatively set the size to the maximum and hope that the renderer michael@0: // isn't about to ask us to read off the end of the array. michael@0: dib->size_ = std::numeric_limits::max(); michael@0: michael@0: return dib; michael@0: } michael@0: michael@0: void* TransportDIB::memory() const { michael@0: return shared_memory_.memory(); michael@0: } michael@0: michael@0: TransportDIB::Handle TransportDIB::handle() const { michael@0: return shared_memory_.handle(); michael@0: } michael@0: michael@0: TransportDIB::Id TransportDIB::id() const { michael@0: return Id(shared_memory_.handle(), sequence_num_); michael@0: }