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/edk/embedder/simple_platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdio.h> // For |fileno()|. | 8 #include <stdio.h> // For |fileno()|. |
9 #include <sys/mman.h> // For |mmap()|/|munmap()|. | 9 #include <sys/mman.h> // For |mmap()|/|munmap()|. |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } | 113 } |
114 | 114 |
115 // TODO(vtl): More checks? | 115 // TODO(vtl): More checks? |
116 | 116 |
117 handle_ = platform_handle.Pass(); | 117 handle_ = platform_handle.Pass(); |
118 return true; | 118 return true; |
119 } | 119 } |
120 | 120 |
121 #endif // !defined(OS_ANDROID) | 121 #endif // !defined(OS_ANDROID) |
122 | 122 |
123 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( | 123 std::unique_ptr<PlatformSharedBufferMapping> |
124 size_t offset, | 124 SimplePlatformSharedBuffer::MapImpl(size_t offset, size_t length) { |
125 size_t length) { | |
126 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 125 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
127 size_t real_offset = offset - offset_rounding; | 126 size_t real_offset = offset - offset_rounding; |
128 size_t real_length = length + offset_rounding; | 127 size_t real_length = length + offset_rounding; |
129 | 128 |
130 // This should hold (since we checked |num_bytes| versus the maximum value of | 129 // This should hold (since we checked |num_bytes| versus the maximum value of |
131 // |off_t| on creation, but it never hurts to be paranoid. | 130 // |off_t| on creation, but it never hurts to be paranoid. |
132 DCHECK_LE(static_cast<uint64_t>(real_offset), | 131 DCHECK_LE(static_cast<uint64_t>(real_offset), |
133 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); | 132 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); |
134 | 133 |
135 void* real_base = | 134 void* real_base = |
136 mmap(nullptr, real_length, PROT_READ | PROT_WRITE, MAP_SHARED, | 135 mmap(nullptr, real_length, PROT_READ | PROT_WRITE, MAP_SHARED, |
137 handle_.get().fd, static_cast<off_t>(real_offset)); | 136 handle_.get().fd, static_cast<off_t>(real_offset)); |
138 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't | 137 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't |
139 // return null either. | 138 // return null either. |
140 if (real_base == MAP_FAILED || !real_base) { | 139 if (real_base == MAP_FAILED || !real_base) { |
141 PLOG(ERROR) << "mmap"; | 140 PLOG(ERROR) << "mmap"; |
142 return nullptr; | 141 return nullptr; |
143 } | 142 } |
144 | 143 |
145 void* base = static_cast<char*>(real_base) + offset_rounding; | 144 void* base = static_cast<char*>(real_base) + offset_rounding; |
146 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( | 145 // Note: We can't use |MakeUnique| here, since it's not a friend of |
147 base, length, real_base, real_length)); | 146 // |SimplePlatformSharedBufferMapping| (only we are). |
| 147 return std::unique_ptr<SimplePlatformSharedBufferMapping>( |
| 148 new SimplePlatformSharedBufferMapping(base, length, real_base, |
| 149 real_length)); |
148 } | 150 } |
149 | 151 |
150 // SimplePlatformSharedBufferMapping ------------------------------------------- | 152 // SimplePlatformSharedBufferMapping ------------------------------------------- |
151 | 153 |
152 void SimplePlatformSharedBufferMapping::Unmap() { | 154 void SimplePlatformSharedBufferMapping::Unmap() { |
153 int result = munmap(real_base_, real_length_); | 155 int result = munmap(real_base_, real_length_); |
154 PLOG_IF(ERROR, result != 0) << "munmap"; | 156 PLOG_IF(ERROR, result != 0) << "munmap"; |
155 } | 157 } |
156 | 158 |
157 } // namespace embedder | 159 } // namespace embedder |
158 } // namespace mojo | 160 } // namespace mojo |
OLD | NEW |