OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/shared_memory.h" | 5 #include "base/shared_memory.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 212 |
213 read_only_ = read_only; | 213 read_only_ = read_only; |
214 | 214 |
215 const char *mode = read_only ? "r" : "r+"; | 215 const char *mode = read_only ? "r" : "r+"; |
216 FILE *fp = file_util::OpenFile(path, mode); | 216 FILE *fp = file_util::OpenFile(path, mode); |
217 return PrepareMapFile(fp); | 217 return PrepareMapFile(fp); |
218 } | 218 } |
219 | 219 |
220 #endif // !defined(OS_ANDROID) | 220 #endif // !defined(OS_ANDROID) |
221 | 221 |
222 bool SharedMemory::Map(size_t bytes) { | 222 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
223 if (mapped_file_ == -1) | 223 if (mapped_file_ == -1) |
224 return false; | 224 return false; |
225 | 225 |
226 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 226 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
227 return false; | 227 return false; |
228 | 228 |
229 #if defined(OS_ANDROID) | 229 #if defined(OS_ANDROID) |
230 if (bytes == 0) { | 230 if (bytes == 0) { |
231 int ashmem_bytes = ashmem_get_size_region(mapped_file_); | 231 int ashmem_bytes = ashmem_get_size_region(mapped_file_); |
232 if (ashmem_bytes < 0) | 232 if (ashmem_bytes < 0) |
233 return false; | 233 return false; |
234 | 234 |
235 DCHECK_GE(static_cast<uint32>(ashmem_bytes), bytes); | 235 DCHECK_GE(static_cast<uint32>(ashmem_bytes), bytes); |
236 // The caller wants to determine the map region size from ashmem. | 236 // The caller wants to determine the map region size from ashmem. |
237 bytes = ashmem_bytes; | 237 bytes = ashmem_bytes - offset; |
238 // TODO(port): we set the created size here so that it is available in | 238 // TODO(port): we set the created size here so that it is available in |
239 // transport_dib_android.cc. We should use ashmem_get_size_region() | 239 // transport_dib_android.cc. We should use ashmem_get_size_region() |
240 // in transport_dib_android.cc. | 240 // in transport_dib_android.cc. |
241 created_size_ = bytes; | 241 created_size_ = ashmem_bytes; |
242 } | 242 } |
243 #endif | 243 #endif |
244 | 244 |
245 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 245 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
246 MAP_SHARED, mapped_file_, 0); | 246 MAP_SHARED, mapped_file_, offset); |
247 | 247 |
248 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; | 248 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; |
249 if (mmap_succeeded) { | 249 if (mmap_succeeded) { |
250 mapped_size_ = bytes; | 250 mapped_size_ = bytes; |
251 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 251 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
252 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 252 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
253 } else { | 253 } else { |
254 memory_ = NULL; | 254 memory_ = NULL; |
255 } | 255 } |
256 | 256 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 new_handle->fd = new_fd; | 380 new_handle->fd = new_fd; |
381 new_handle->auto_close = true; | 381 new_handle->auto_close = true; |
382 | 382 |
383 if (close_self) | 383 if (close_self) |
384 Close(); | 384 Close(); |
385 | 385 |
386 return true; | 386 return true; |
387 } | 387 } |
388 | 388 |
389 } // namespace base | 389 } // namespace base |
OLD | NEW |