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 | 8 |
9 namespace mojo { | 9 namespace mojo { |
10 namespace system { | 10 namespace system { |
11 | 11 |
12 // static | 12 // static |
13 RawSharedBuffer* RawSharedBuffer::Create(size_t num_bytes) { | 13 RawSharedBuffer* RawSharedBuffer::Create(size_t num_bytes) { |
14 DCHECK_GT(num_bytes, 0u); | 14 DCHECK_GT(num_bytes, 0u); |
15 | 15 |
16 RawSharedBuffer* rv = new RawSharedBuffer(num_bytes); | 16 RawSharedBuffer* rv = new RawSharedBuffer(num_bytes); |
17 // No need to take the lock since we haven't given the object to anyone yet. | 17 // No need to take the lock since we haven't given the object to anyone yet. |
18 if (!rv->InitNoLock()) | 18 if (!rv->InitNoLock()) |
19 return NULL; | 19 return NULL; |
20 | 20 |
21 return rv; | 21 return rv; |
22 } | 22 } |
23 | 23 |
24 scoped_ptr<RawSharedBuffer::Mapping> RawSharedBuffer::Map(size_t offset, | 24 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::Map(size_t offset, |
25 size_t length) { | 25 size_t length) { |
26 if (!IsValidMap(offset, length)) | 26 if (!IsValidMap(offset, length)) |
27 return scoped_ptr<Mapping>(); | 27 return scoped_ptr<RawSharedBufferMapping>(); |
28 | 28 |
29 return MapNoCheck(offset, length); | 29 return MapNoCheck(offset, length); |
30 } | 30 } |
31 | 31 |
32 bool RawSharedBuffer::IsValidMap(size_t offset, size_t length) { | 32 bool RawSharedBuffer::IsValidMap(size_t offset, size_t length) { |
33 if (offset > num_bytes_ || length == 0) | 33 if (offset > num_bytes_ || length == 0) |
34 return false; | 34 return false; |
35 | 35 |
36 // Note: This is an overflow-safe check of |offset + length > num_bytes_| | 36 // Note: This is an overflow-safe check of |offset + length > num_bytes_| |
37 // (that |num_bytes >= offset| is verified above). | 37 // (that |num_bytes >= offset| is verified above). |
38 if (length > num_bytes_ - offset) | 38 if (length > num_bytes_ - offset) |
39 return false; | 39 return false; |
40 | 40 |
41 return true; | 41 return true; |
42 } | 42 } |
43 | 43 |
44 scoped_ptr<RawSharedBuffer::Mapping> RawSharedBuffer::MapNoCheck( | 44 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapNoCheck(size_t offset, |
45 size_t offset, | 45 size_t length) { |
46 size_t length) { | |
47 DCHECK(IsValidMap(offset, length)); | 46 DCHECK(IsValidMap(offset, length)); |
48 | 47 |
49 base::AutoLock locker(lock_); | 48 base::AutoLock locker(lock_); |
50 return MapImplNoLock(offset, length); | 49 return MapImplNoLock(offset, length); |
51 } | 50 } |
52 | 51 |
53 RawSharedBuffer::RawSharedBuffer(size_t num_bytes) : num_bytes_(num_bytes) { | 52 RawSharedBuffer::RawSharedBuffer(size_t num_bytes) : num_bytes_(num_bytes) { |
54 } | 53 } |
55 | 54 |
56 RawSharedBuffer::~RawSharedBuffer() { | 55 RawSharedBuffer::~RawSharedBuffer() { |
57 } | 56 } |
58 | 57 |
59 } // namespace system | 58 } // namespace system |
60 } // namespace mojo | 59 } // namespace mojo |
OLD | NEW |