OLD | NEW |
| (Empty) |
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/embedder/simple_platform_shared_buffer.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "mojo/embedder/platform_handle_utils.h" | |
9 | |
10 namespace mojo { | |
11 namespace embedder { | |
12 | |
13 // static | |
14 SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create( | |
15 size_t num_bytes) { | |
16 DCHECK_GT(num_bytes, 0u); | |
17 | |
18 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes); | |
19 if (!rv->Init()) { | |
20 // We can't just delete it directly, due to the "in destructor" (debug) | |
21 // check. | |
22 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv); | |
23 return nullptr; | |
24 } | |
25 | |
26 return rv; | |
27 } | |
28 | |
29 // static | |
30 SimplePlatformSharedBuffer* | |
31 SimplePlatformSharedBuffer::CreateFromPlatformHandle( | |
32 size_t num_bytes, | |
33 ScopedPlatformHandle platform_handle) { | |
34 DCHECK_GT(num_bytes, 0u); | |
35 | |
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) | |
39 // check. | |
40 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv); | |
41 return nullptr; | |
42 } | |
43 | |
44 return rv; | |
45 } | |
46 | |
47 size_t SimplePlatformSharedBuffer::GetNumBytes() const { | |
48 return num_bytes_; | |
49 } | |
50 | |
51 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::Map( | |
52 size_t offset, | |
53 size_t length) { | |
54 if (!IsValidMap(offset, length)) | |
55 return nullptr; | |
56 | |
57 return MapNoCheck(offset, length); | |
58 } | |
59 | |
60 bool SimplePlatformSharedBuffer::IsValidMap(size_t offset, size_t length) { | |
61 if (offset > num_bytes_ || length == 0) | |
62 return false; | |
63 | |
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) | |
67 return false; | |
68 | |
69 return true; | |
70 } | |
71 | |
72 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck( | |
73 size_t offset, | |
74 size_t length) { | |
75 DCHECK(IsValidMap(offset, length)); | |
76 return MapImpl(offset, length); | |
77 } | |
78 | |
79 ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() { | |
80 return mojo::embedder::DuplicatePlatformHandle(handle_.get()); | |
81 } | |
82 | |
83 ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() { | |
84 DCHECK(HasOneRef()); | |
85 return handle_.Pass(); | |
86 } | |
87 | |
88 SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes) | |
89 : num_bytes_(num_bytes) { | |
90 } | |
91 | |
92 SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() { | |
93 } | |
94 | |
95 SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() { | |
96 Unmap(); | |
97 } | |
98 | |
99 void* SimplePlatformSharedBufferMapping::GetBase() const { | |
100 return base_; | |
101 } | |
102 | |
103 size_t SimplePlatformSharedBufferMapping::GetLength() const { | |
104 return length_; | |
105 } | |
106 | |
107 } // namespace embedder | |
108 } // namespace mojo | |
OLD | NEW |