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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 size_t length) { | 120 size_t length) { |
121 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 121 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
122 size_t real_offset = offset - offset_rounding; | 122 size_t real_offset = offset - offset_rounding; |
123 size_t real_length = length + offset_rounding; | 123 size_t real_length = length + offset_rounding; |
124 | 124 |
125 // This should hold (since we checked |num_bytes| versus the maximum value of | 125 // This should hold (since we checked |num_bytes| versus the maximum value of |
126 // |off_t| on creation, but it never hurts to be paranoid. | 126 // |off_t| on creation, but it never hurts to be paranoid. |
127 DCHECK_LE(static_cast<uint64_t>(real_offset), | 127 DCHECK_LE(static_cast<uint64_t>(real_offset), |
128 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); | 128 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); |
129 | 129 |
130 void* real_base = mmap(nullptr, | 130 void* real_base = |
131 real_length, | 131 mmap(nullptr, real_length, PROT_READ | PROT_WRITE, MAP_SHARED, |
132 PROT_READ | PROT_WRITE, | 132 handle_.get().fd, static_cast<off_t>(real_offset)); |
133 MAP_SHARED, | |
134 handle_.get().fd, | |
135 static_cast<off_t>(real_offset)); | |
136 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't | 133 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't |
137 // return null either. | 134 // return null either. |
138 if (real_base == MAP_FAILED || !real_base) { | 135 if (real_base == MAP_FAILED || !real_base) { |
139 PLOG(ERROR) << "mmap"; | 136 PLOG(ERROR) << "mmap"; |
140 return nullptr; | 137 return nullptr; |
141 } | 138 } |
142 | 139 |
143 void* base = static_cast<char*>(real_base) + offset_rounding; | 140 void* base = static_cast<char*>(real_base) + offset_rounding; |
144 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( | 141 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( |
145 base, length, real_base, real_length)); | 142 base, length, real_base, real_length)); |
146 } | 143 } |
147 | 144 |
148 // SimplePlatformSharedBufferMapping ------------------------------------------- | 145 // SimplePlatformSharedBufferMapping ------------------------------------------- |
149 | 146 |
150 void SimplePlatformSharedBufferMapping::Unmap() { | 147 void SimplePlatformSharedBufferMapping::Unmap() { |
151 int result = munmap(real_base_, real_length_); | 148 int result = munmap(real_base_, real_length_); |
152 PLOG_IF(ERROR, result != 0) << "munmap"; | 149 PLOG_IF(ERROR, result != 0) << "munmap"; |
153 } | 150 } |
154 | 151 |
155 } // namespace embedder | 152 } // namespace embedder |
156 } // namespace mojo | 153 } // namespace mojo |
OLD | NEW |