OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/system/raw_shared_buffer.h" | 5 #include "mojo/system/raw_shared_buffer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "mojo/embedder/platform_handle_utils.h" |
8 | 9 |
9 namespace mojo { | 10 namespace mojo { |
10 namespace system { | 11 namespace system { |
11 | 12 |
12 // static | 13 // static |
13 RawSharedBuffer* RawSharedBuffer::Create(size_t num_bytes) { | 14 RawSharedBuffer* RawSharedBuffer::Create(size_t num_bytes) { |
14 DCHECK_GT(num_bytes, 0u); | 15 DCHECK_GT(num_bytes, 0u); |
15 | 16 |
16 RawSharedBuffer* rv = new RawSharedBuffer(num_bytes); | 17 RawSharedBuffer* rv = new RawSharedBuffer(num_bytes); |
17 // No need to take the lock since we haven't given the object to anyone yet. | 18 if (!rv->Init()) { |
18 if (!rv->InitNoLock()) | 19 // We can't just delete it directly, due to the "in destructor" (debug) |
| 20 // check. |
| 21 scoped_refptr<RawSharedBuffer> deleter(rv); |
19 return NULL; | 22 return NULL; |
| 23 } |
20 | 24 |
21 return rv; | 25 return rv; |
22 } | 26 } |
| 27 |
| 28 RawSharedBuffer* RawSharedBuffer::CreateFromPlatformHandle( |
| 29 size_t num_bytes, |
| 30 embedder::ScopedPlatformHandle platform_handle) { |
| 31 DCHECK_GT(num_bytes, 0u); |
| 32 |
| 33 RawSharedBuffer* rv = new RawSharedBuffer(num_bytes); |
| 34 if (!rv->InitFromPlatformHandle(platform_handle.Pass())) { |
| 35 // We can't just delete it directly, due to the "in destructor" (debug) |
| 36 // check. |
| 37 scoped_refptr<RawSharedBuffer> deleter(rv); |
| 38 return NULL; |
| 39 } |
| 40 |
| 41 return rv; |
| 42 } |
23 | 43 |
24 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::Map(size_t offset, | 44 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::Map(size_t offset, |
25 size_t length) { | 45 size_t length) { |
26 if (!IsValidMap(offset, length)) | 46 if (!IsValidMap(offset, length)) |
27 return scoped_ptr<RawSharedBufferMapping>(); | 47 return scoped_ptr<RawSharedBufferMapping>(); |
28 | 48 |
29 return MapNoCheck(offset, length); | 49 return MapNoCheck(offset, length); |
30 } | 50 } |
31 | 51 |
32 bool RawSharedBuffer::IsValidMap(size_t offset, size_t length) { | 52 bool RawSharedBuffer::IsValidMap(size_t offset, size_t length) { |
33 if (offset > num_bytes_ || length == 0) | 53 if (offset > num_bytes_ || length == 0) |
34 return false; | 54 return false; |
35 | 55 |
36 // Note: This is an overflow-safe check of |offset + length > num_bytes_| | 56 // Note: This is an overflow-safe check of |offset + length > num_bytes_| |
37 // (that |num_bytes >= offset| is verified above). | 57 // (that |num_bytes >= offset| is verified above). |
38 if (length > num_bytes_ - offset) | 58 if (length > num_bytes_ - offset) |
39 return false; | 59 return false; |
40 | 60 |
41 return true; | 61 return true; |
42 } | 62 } |
43 | 63 |
44 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapNoCheck(size_t offset, | 64 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapNoCheck(size_t offset, |
45 size_t length) { | 65 size_t length) { |
46 DCHECK(IsValidMap(offset, length)); | 66 DCHECK(IsValidMap(offset, length)); |
| 67 return MapImpl(offset, length); |
| 68 } |
47 | 69 |
48 base::AutoLock locker(lock_); | 70 embedder::ScopedPlatformHandle RawSharedBuffer::DuplicatePlatformHandle() { |
49 return MapImplNoLock(offset, length); | 71 return embedder::DuplicatePlatformHandle(handle_.get()); |
| 72 } |
| 73 |
| 74 embedder::ScopedPlatformHandle RawSharedBuffer::PassPlatformHandle() { |
| 75 DCHECK(HasOneRef()); |
| 76 return handle_.Pass(); |
50 } | 77 } |
51 | 78 |
52 RawSharedBuffer::RawSharedBuffer(size_t num_bytes) : num_bytes_(num_bytes) { | 79 RawSharedBuffer::RawSharedBuffer(size_t num_bytes) : num_bytes_(num_bytes) { |
53 } | 80 } |
54 | 81 |
55 RawSharedBuffer::~RawSharedBuffer() { | 82 RawSharedBuffer::~RawSharedBuffer() { |
56 } | 83 } |
57 | 84 |
58 } // namespace system | 85 } // namespace system |
59 } // namespace mojo | 86 } // namespace mojo |
OLD | NEW |