summaryrefslogtreecommitdiffstats
path: root/mojo/system/raw_shared_buffer.cc
blob: fb67cca71a77e8cfb3ddee8f13c7c3fbc50a45ec (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
// 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/system/raw_shared_buffer.h"

#include "base/logging.h"

namespace mojo {
namespace system {

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

  RawSharedBuffer* rv = new RawSharedBuffer(num_bytes);
  // No need to take the lock since we haven't given the object to anyone yet.
  if (!rv->InitNoLock())
    return NULL;

  return rv;
}

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

  return MapNoCheck(offset, length);
}

bool RawSharedBuffer::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<RawSharedBufferMapping> RawSharedBuffer::MapNoCheck(size_t offset,
                                                               size_t length) {
  DCHECK(IsValidMap(offset, length));

  base::AutoLock locker(lock_);
  return MapImplNoLock(offset, length);
}

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

RawSharedBuffer::~RawSharedBuffer() {
}

}  // namespace system
}  // namespace mojo