summaryrefslogtreecommitdiffstats
path: root/mojo/embedder/simple_platform_shared_buffer.cc
blob: 278de00069f219c39e93fabbd09a7515936b9446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/embedder/simple_platform_shared_buffer.h"

#include "base/logging.h"
#include "mojo/embedder/platform_handle_utils.h"

namespace mojo {
namespace embedder {

// static
SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create(
    size_t num_bytes) {
  DCHECK_GT(num_bytes, 0u);

  SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes);
  if (!rv->Init()) {
    // We can't just delete it directly, due to the "in destructor" (debug)
    // check.
    scoped_refptr<SimplePlatformSharedBuffer> deleter(rv);
    return NULL;
  }

  return rv;
}

// static
SimplePlatformSharedBuffer*
SimplePlatformSharedBuffer::CreateFromPlatformHandle(
    size_t num_bytes,
    ScopedPlatformHandle platform_handle) {
  DCHECK_GT(num_bytes, 0u);

  SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes);
  if (!rv->InitFromPlatformHandle(platform_handle.Pass())) {
    // We can't just delete it directly, due to the "in destructor" (debug)
    // check.
    scoped_refptr<SimplePlatformSharedBuffer> deleter(rv);
    return NULL;
  }

  return rv;
}

size_t SimplePlatformSharedBuffer::GetNumBytes() const {
  return num_bytes_;
}

scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::Map(
    size_t offset,
    size_t length) {
  if (!IsValidMap(offset, length))
    return scoped_ptr<PlatformSharedBufferMapping>();

  return MapNoCheck(offset, length);
}

bool SimplePlatformSharedBuffer::IsValidMap(size_t offset, size_t length) {
  if (offset > num_bytes_ || length == 0)
    return false;

  // Note: This is an overflow-safe check of |offset + length > num_bytes_|
  // (that |num_bytes >= offset| is verified above).
  if (length > num_bytes_ - offset)
    return false;

  return true;
}

scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck(
    size_t offset,
    size_t length) {
  DCHECK(IsValidMap(offset, length));
  return MapImpl(offset, length);
}

ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() {
  return mojo::embedder::DuplicatePlatformHandle(handle_.get());
}

ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() {
  DCHECK(HasOneRef());
  return handle_.Pass();
}

SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes)
    : num_bytes_(num_bytes) {
}

SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() {
}

SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() {
  Unmap();
}

void* SimplePlatformSharedBufferMapping::GetBase() const {
  return base_;
}

size_t SimplePlatformSharedBufferMapping::GetLength() const {
  return length_;
}

}  // namespace embedder
}  // namespace mojo