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 <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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 LOG(ERROR) << "Shared memory file has the wrong size"; | 108 LOG(ERROR) << "Shared memory file has the wrong size"; |
109 return false; | 109 return false; |
110 } | 110 } |
111 | 111 |
112 // TODO(vtl): More checks? | 112 // TODO(vtl): More checks? |
113 | 113 |
114 handle_ = platform_handle.Pass(); | 114 handle_ = platform_handle.Pass(); |
115 return true; | 115 return true; |
116 } | 116 } |
117 | 117 |
118 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImpl(size_t offset, | 118 scoped_ptr<embedder::PlatformSharedBufferMapping> RawSharedBuffer::MapImpl( |
119 size_t length) { | 119 size_t offset, |
| 120 size_t length) { |
120 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 121 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
121 size_t real_offset = offset - offset_rounding; | 122 size_t real_offset = offset - offset_rounding; |
122 size_t real_length = length + offset_rounding; | 123 size_t real_length = length + offset_rounding; |
123 | 124 |
124 // 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 |
125 // |off_t| on creation, but it never hurts to be paranoid. | 126 // |off_t| on creation, but it never hurts to be paranoid. |
126 DCHECK_LE(static_cast<uint64_t>(real_offset), | 127 DCHECK_LE(static_cast<uint64_t>(real_offset), |
127 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); | 128 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); |
128 | 129 |
129 void* real_base = mmap(NULL, | 130 void* real_base = mmap(NULL, |
130 real_length, | 131 real_length, |
131 PROT_READ | PROT_WRITE, | 132 PROT_READ | PROT_WRITE, |
132 MAP_SHARED, | 133 MAP_SHARED, |
133 handle_.get().fd, | 134 handle_.get().fd, |
134 static_cast<off_t>(real_offset)); | 135 static_cast<off_t>(real_offset)); |
135 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't | 136 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't |
136 // return null either. | 137 // return null either. |
137 if (real_base == MAP_FAILED || !real_base) { | 138 if (real_base == MAP_FAILED || !real_base) { |
138 PLOG(ERROR) << "mmap"; | 139 PLOG(ERROR) << "mmap"; |
139 return scoped_ptr<RawSharedBufferMapping>(); | 140 return scoped_ptr<embedder::PlatformSharedBufferMapping>(); |
140 } | 141 } |
141 | 142 |
142 void* base = static_cast<char*>(real_base) + offset_rounding; | 143 void* base = static_cast<char*>(real_base) + offset_rounding; |
143 return make_scoped_ptr( | 144 return scoped_ptr<embedder::PlatformSharedBufferMapping>( |
144 new RawSharedBufferMapping(base, length, real_base, real_length)); | 145 new RawSharedBufferMapping(base, length, real_base, real_length)); |
145 } | 146 } |
146 | 147 |
147 // RawSharedBufferMapping ------------------------------------------------------ | 148 // RawSharedBufferMapping ------------------------------------------------------ |
148 | 149 |
149 void RawSharedBufferMapping::Unmap() { | 150 void RawSharedBufferMapping::Unmap() { |
150 int result = munmap(real_base_, real_length_); | 151 int result = munmap(real_base_, real_length_); |
151 PLOG_IF(ERROR, result != 0) << "munmap"; | 152 PLOG_IF(ERROR, result != 0) << "munmap"; |
152 } | 153 } |
153 | 154 |
154 } // namespace system | 155 } // namespace system |
155 } // namespace mojo | 156 } // namespace mojo |
OLD | NEW |