Chromium Code Reviews| 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 // No need to take the lock since we haven't given the object to anyone yet. |
| 18 if (!rv->InitNoLock()) | 19 if (!rv->InitNoLock()) { |
| 20 // We can't just delete it directly, due to the "in destructor" (debug) | |
| 21 // check. | |
| 22 scoped_refptr<RawSharedBuffer> deleter(rv); | |
|
yzshen1
2014/05/30 00:09:35
With the raw and zero-ref-count |rv|, if something
viettrungluu
2014/05/30 00:23:42
This is equivalent to the scoped_refptr<>.
| |
| 19 return NULL; | 23 return NULL; |
| 24 } | |
| 20 | 25 |
| 21 return rv; | 26 return rv; |
| 22 } | 27 } |
| 28 | |
| 29 RawSharedBuffer* RawSharedBuffer::CreateFromPlatformHandle( | |
| 30 size_t num_bytes, | |
| 31 embedder::ScopedPlatformHandle platform_handle) { | |
| 32 DCHECK_GT(num_bytes, 0u); | |
| 33 | |
| 34 RawSharedBuffer* rv = new RawSharedBuffer(num_bytes); | |
|
yzshen1
2014/05/30 00:09:35
ditto.
| |
| 35 // No need to take the lock since we haven't given the object to anyone yet. | |
| 36 if (!rv->InitFromPlatformHandleNoLock(platform_handle.Pass())) { | |
| 37 // We can't just delete it directly, due to the "in destructor" (debug) | |
| 38 // check. | |
| 39 scoped_refptr<RawSharedBuffer> deleter(rv); | |
| 40 return NULL; | |
| 41 } | |
| 42 | |
| 43 return rv; | |
| 44 } | |
| 23 | 45 |
| 24 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::Map(size_t offset, | 46 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::Map(size_t offset, |
| 25 size_t length) { | 47 size_t length) { |
| 26 if (!IsValidMap(offset, length)) | 48 if (!IsValidMap(offset, length)) |
| 27 return scoped_ptr<RawSharedBufferMapping>(); | 49 return scoped_ptr<RawSharedBufferMapping>(); |
| 28 | 50 |
| 29 return MapNoCheck(offset, length); | 51 return MapNoCheck(offset, length); |
| 30 } | 52 } |
| 31 | 53 |
| 32 bool RawSharedBuffer::IsValidMap(size_t offset, size_t length) { | 54 bool RawSharedBuffer::IsValidMap(size_t offset, size_t length) { |
| 33 if (offset > num_bytes_ || length == 0) | 55 if (offset > num_bytes_ || length == 0) |
| 34 return false; | 56 return false; |
| 35 | 57 |
| 36 // Note: This is an overflow-safe check of |offset + length > num_bytes_| | 58 // Note: This is an overflow-safe check of |offset + length > num_bytes_| |
| 37 // (that |num_bytes >= offset| is verified above). | 59 // (that |num_bytes >= offset| is verified above). |
| 38 if (length > num_bytes_ - offset) | 60 if (length > num_bytes_ - offset) |
| 39 return false; | 61 return false; |
| 40 | 62 |
| 41 return true; | 63 return true; |
| 42 } | 64 } |
| 43 | 65 |
| 44 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapNoCheck(size_t offset, | 66 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapNoCheck(size_t offset, |
| 45 size_t length) { | 67 size_t length) { |
| 46 DCHECK(IsValidMap(offset, length)); | 68 DCHECK(IsValidMap(offset, length)); |
| 47 | 69 |
| 48 base::AutoLock locker(lock_); | 70 base::AutoLock locker(lock_); |
| 49 return MapImplNoLock(offset, length); | 71 return MapImplNoLock(offset, length); |
| 50 } | 72 } |
| 51 | 73 |
| 74 embedder::ScopedPlatformHandle RawSharedBuffer::DuplicatePlatformHandle() { | |
|
yzshen1
2014/05/30 00:09:35
Is it necessary to protect the access to handle_ f
viettrungluu
2014/05/30 00:23:42
No and no.
In the case of DuplicatePlatformHandle
yzshen1
2014/05/30 00:27:48
Sounds good. Maybe adding your reply as comments?
| |
| 75 return embedder::DuplicatePlatformHandle(handle_.get()); | |
| 76 } | |
| 77 | |
| 78 embedder::ScopedPlatformHandle RawSharedBuffer::PassPlatformHandle() { | |
| 79 DCHECK(HasOneRef()); | |
| 80 return handle_.Pass(); | |
| 81 } | |
| 82 | |
| 52 RawSharedBuffer::RawSharedBuffer(size_t num_bytes) : num_bytes_(num_bytes) { | 83 RawSharedBuffer::RawSharedBuffer(size_t num_bytes) : num_bytes_(num_bytes) { |
| 53 } | 84 } |
| 54 | 85 |
| 55 RawSharedBuffer::~RawSharedBuffer() { | 86 RawSharedBuffer::~RawSharedBuffer() { |
| 56 } | 87 } |
| 57 | 88 |
| 58 } // namespace system | 89 } // namespace system |
| 59 } // namespace mojo | 90 } // namespace mojo |
| OLD | NEW |