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