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 15 matching lines...) Expand all Loading... |
26 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a | 26 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a |
27 // |uint64_t|. | 27 // |uint64_t|. |
28 static_assert(sizeof(size_t) <= sizeof(uint64_t), "size_t too big"); | 28 static_assert(sizeof(size_t) <= sizeof(uint64_t), "size_t too big"); |
29 static_assert(sizeof(off_t) <= sizeof(uint64_t), "off_t too big"); | 29 static_assert(sizeof(off_t) <= sizeof(uint64_t), "off_t too big"); |
30 | 30 |
31 namespace mojo { | 31 namespace mojo { |
32 namespace embedder { | 32 namespace embedder { |
33 | 33 |
34 // SimplePlatformSharedBuffer -------------------------------------------------- | 34 // SimplePlatformSharedBuffer -------------------------------------------------- |
35 | 35 |
| 36 // The implementation for android uses ashmem to generate the file descriptor |
| 37 // for the shared memory. See simple_platform_shared_buffer_android.cc |
| 38 #if !defined(OS_ANDROID) |
| 39 |
36 bool SimplePlatformSharedBuffer::Init() { | 40 bool SimplePlatformSharedBuffer::Init() { |
37 DCHECK(!handle_.is_valid()); | 41 DCHECK(!handle_.is_valid()); |
38 | 42 |
39 base::ThreadRestrictions::ScopedAllowIO allow_io; | 43 base::ThreadRestrictions::ScopedAllowIO allow_io; |
40 | 44 |
41 if (static_cast<uint64_t>(num_bytes_) > | 45 if (static_cast<uint64_t>(num_bytes_) > |
42 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { | 46 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { |
43 return false; | 47 return false; |
44 } | 48 } |
45 | 49 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 LOG(ERROR) << "Shared memory file has the wrong size"; | 112 LOG(ERROR) << "Shared memory file has the wrong size"; |
109 return false; | 113 return false; |
110 } | 114 } |
111 | 115 |
112 // TODO(vtl): More checks? | 116 // TODO(vtl): More checks? |
113 | 117 |
114 handle_ = platform_handle.Pass(); | 118 handle_ = platform_handle.Pass(); |
115 return true; | 119 return true; |
116 } | 120 } |
117 | 121 |
| 122 #endif // !defined(OS_ANDROID) |
| 123 |
118 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( | 124 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( |
119 size_t offset, | 125 size_t offset, |
120 size_t length) { | 126 size_t length) { |
121 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 127 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
122 size_t real_offset = offset - offset_rounding; | 128 size_t real_offset = offset - offset_rounding; |
123 size_t real_length = length + offset_rounding; | 129 size_t real_length = length + offset_rounding; |
124 | 130 |
125 // This should hold (since we checked |num_bytes| versus the maximum value of | 131 // 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. | 132 // |off_t| on creation, but it never hurts to be paranoid. |
127 DCHECK_LE(static_cast<uint64_t>(real_offset), | 133 DCHECK_LE(static_cast<uint64_t>(real_offset), |
(...skipping 16 matching lines...) Expand all Loading... |
144 | 150 |
145 // SimplePlatformSharedBufferMapping ------------------------------------------- | 151 // SimplePlatformSharedBufferMapping ------------------------------------------- |
146 | 152 |
147 void SimplePlatformSharedBufferMapping::Unmap() { | 153 void SimplePlatformSharedBufferMapping::Unmap() { |
148 int result = munmap(real_base_, real_length_); | 154 int result = munmap(real_base_, real_length_); |
149 PLOG_IF(ERROR, result != 0) << "munmap"; | 155 PLOG_IF(ERROR, result != 0) << "munmap"; |
150 } | 156 } |
151 | 157 |
152 } // namespace embedder | 158 } // namespace embedder |
153 } // namespace mojo | 159 } // namespace mojo |
OLD | NEW |