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.
5 #include "mojo/embedder/simple_platform_shared_buffer.h"
7 #include "base/logging.h"
8 #include "mojo/embedder/platform_handle_utils.h"
14 SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create(
16 DCHECK_GT(num_bytes, 0u);
18 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes);
20 // We can't just delete it directly, due to the "in destructor" (debug)
22 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv);
30 SimplePlatformSharedBuffer*
31 SimplePlatformSharedBuffer::CreateFromPlatformHandle(
33 ScopedPlatformHandle platform_handle) {
34 DCHECK_GT(num_bytes, 0u);
36 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes);
37 if (!rv->InitFromPlatformHandle(platform_handle.Pass())) {
38 // We can't just delete it directly, due to the "in destructor" (debug)
40 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv);
47 size_t SimplePlatformSharedBuffer::GetNumBytes() const {
51 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::Map(
54 if (!IsValidMap(offset, length))
55 return scoped_ptr<PlatformSharedBufferMapping>();
57 return MapNoCheck(offset, length);
60 bool SimplePlatformSharedBuffer::IsValidMap(size_t offset, size_t length) {
61 if (offset > num_bytes_ || length == 0)
64 // Note: This is an overflow-safe check of |offset + length > num_bytes_|
65 // (that |num_bytes >= offset| is verified above).
66 if (length > num_bytes_ - offset)
72 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck(
75 DCHECK(IsValidMap(offset, length));
76 return MapImpl(offset, length);
79 ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() {
80 return mojo::embedder::DuplicatePlatformHandle(handle_.get());
83 ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() {
85 return handle_.Pass();
88 SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes)
89 : num_bytes_(num_bytes) {
92 SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() {
95 SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() {
99 void* SimplePlatformSharedBufferMapping::GetBase() const {
103 size_t SimplePlatformSharedBufferMapping::GetLength() const {
107 } // namespace embedder