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/types.h> // For |off_t|. | 10 #include <sys/types.h> // For |off_t|. |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "mojo/embedder/platform_handle.h" | 23 #include "mojo/embedder/platform_handle.h" |
24 | 24 |
25 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a | 25 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a |
26 // |uint64_t|. | 26 // |uint64_t|. |
27 COMPILE_ASSERT(sizeof(size_t) <= sizeof(uint64_t), size_t_too_big); | 27 COMPILE_ASSERT(sizeof(size_t) <= sizeof(uint64_t), size_t_too_big); |
28 COMPILE_ASSERT(sizeof(off_t) <= sizeof(uint64_t), off_t_too_big); | 28 COMPILE_ASSERT(sizeof(off_t) <= sizeof(uint64_t), off_t_too_big); |
29 | 29 |
30 namespace mojo { | 30 namespace mojo { |
31 namespace system { | 31 namespace system { |
32 | 32 |
33 // RawSharedBuffer::Mapping ---------------------------------------------------- | |
34 | |
35 void RawSharedBuffer::Mapping::Unmap() { | |
36 int result = munmap(real_base_, real_length_); | |
37 PLOG_IF(ERROR, result != 0) << "munmap"; | |
38 } | |
39 | |
40 // RawSharedBuffer ------------------------------------------------------------- | 33 // RawSharedBuffer ------------------------------------------------------------- |
41 | 34 |
42 bool RawSharedBuffer::InitNoLock() { | 35 bool RawSharedBuffer::InitNoLock() { |
43 DCHECK(!handle_.is_valid()); | 36 DCHECK(!handle_.is_valid()); |
44 | 37 |
45 base::ThreadRestrictions::ScopedAllowIO allow_io; | 38 base::ThreadRestrictions::ScopedAllowIO allow_io; |
46 | 39 |
47 if (static_cast<uint64_t>(num_bytes_) > | 40 if (static_cast<uint64_t>(num_bytes_) > |
48 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { | 41 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { |
49 return false; | 42 return false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 75 |
83 if (HANDLE_EINTR(ftruncate(fd.get(), static_cast<off_t>(num_bytes_))) != 0) { | 76 if (HANDLE_EINTR(ftruncate(fd.get(), static_cast<off_t>(num_bytes_))) != 0) { |
84 PLOG(ERROR) << "ftruncate"; | 77 PLOG(ERROR) << "ftruncate"; |
85 return false; | 78 return false; |
86 } | 79 } |
87 | 80 |
88 handle_.reset(embedder::PlatformHandle(fd.release())); | 81 handle_.reset(embedder::PlatformHandle(fd.release())); |
89 return true; | 82 return true; |
90 } | 83 } |
91 | 84 |
92 scoped_ptr<RawSharedBuffer::Mapping> RawSharedBuffer::MapImplNoLock( | 85 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImplNoLock( |
93 size_t offset, | 86 size_t offset, |
94 size_t length) { | 87 size_t length) { |
95 lock_.AssertAcquired(); | 88 lock_.AssertAcquired(); |
96 | 89 |
97 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 90 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
98 size_t real_offset = offset - offset_rounding; | 91 size_t real_offset = offset - offset_rounding; |
99 size_t real_length = length + offset_rounding; | 92 size_t real_length = length + offset_rounding; |
100 | 93 |
101 // This should hold (since we checked |num_bytes| versus the maximum value of | 94 // This should hold (since we checked |num_bytes| versus the maximum value of |
102 // |off_t| on creation, but it never hurts to be paranoid. | 95 // |off_t| on creation, but it never hurts to be paranoid. |
103 DCHECK_LE(static_cast<uint64_t>(real_offset), | 96 DCHECK_LE(static_cast<uint64_t>(real_offset), |
104 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); | 97 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); |
105 | 98 |
106 void* real_base = mmap(NULL, real_length, PROT_READ | PROT_WRITE, MAP_SHARED, | 99 void* real_base = mmap(NULL, real_length, PROT_READ | PROT_WRITE, MAP_SHARED, |
107 handle_.get().fd, static_cast<off_t>(real_offset)); | 100 handle_.get().fd, static_cast<off_t>(real_offset)); |
108 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't | 101 // |mmap()| should return |MAP_FAILED| (a.k.a. -1) on error. But it shouldn't |
109 // return null either. | 102 // return null either. |
110 if (real_base == MAP_FAILED || !real_base) { | 103 if (real_base == MAP_FAILED || !real_base) { |
111 PLOG(ERROR) << "mmap"; | 104 PLOG(ERROR) << "mmap"; |
112 return scoped_ptr<Mapping>(); | 105 return scoped_ptr<RawSharedBufferMapping>(); |
113 } | 106 } |
114 | 107 |
115 void* base = static_cast<char*>(real_base) + offset_rounding; | 108 void* base = static_cast<char*>(real_base) + offset_rounding; |
116 return make_scoped_ptr(new Mapping(base, length, real_base, real_length)); | 109 return make_scoped_ptr( |
| 110 new RawSharedBufferMapping(base, length, real_base, real_length)); |
| 111 } |
| 112 |
| 113 // RawSharedBufferMapping ------------------------------------------------------ |
| 114 |
| 115 void RawSharedBufferMapping::Unmap() { |
| 116 int result = munmap(real_base_, real_length_); |
| 117 PLOG_IF(ERROR, result != 0) << "munmap"; |
117 } | 118 } |
118 | 119 |
119 } // namespace system | 120 } // namespace system |
120 } // namespace mojo | 121 } // namespace mojo |
OLD | NEW |