OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <sys/mman.h> // For |mmap()|/|munmap()|. |
| 9 #include <sys/types.h> // For |off_t|. |
| 10 #include <unistd.h> |
| 11 |
| 12 #include <limits> |
| 13 |
| 14 #include "base/files/scoped_file.h" |
| 15 #include "base/logging.h" |
| 16 #include "base/macros.h" |
| 17 #include "mojo/edk/embedder/platform_handle.h" |
| 18 #include "third_party/ashmem/ashmem.h" |
| 19 |
| 20 namespace mojo { |
| 21 namespace embedder { |
| 22 |
| 23 // SimplePlatformSharedBuffer -------------------------------------------------- |
| 24 |
| 25 bool SimplePlatformSharedBuffer::Init() { |
| 26 DCHECK(!handle_.is_valid()); |
| 27 |
| 28 if (static_cast<uint64_t>(num_bytes_) > |
| 29 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { |
| 30 return false; |
| 31 } |
| 32 |
| 33 base::ScopedFD fd(ashmem_create_region(nullptr, num_bytes_)); |
| 34 if (!fd.is_valid()) { |
| 35 DPLOG(ERROR) << "ashmem_create_region()"; |
| 36 return false; |
| 37 } |
| 38 |
| 39 if (ashmem_set_prot_region(fd.get(), PROT_READ | PROT_WRITE) < 0) { |
| 40 DPLOG(ERROR) << "ashmem_set_prot_region()"; |
| 41 return false; |
| 42 } |
| 43 |
| 44 handle_.reset(PlatformHandle(fd.release())); |
| 45 return true; |
| 46 } |
| 47 |
| 48 bool SimplePlatformSharedBuffer::InitFromPlatformHandle( |
| 49 ScopedPlatformHandle platform_handle) { |
| 50 DCHECK(!handle_.is_valid()); |
| 51 |
| 52 if (static_cast<uint64_t>(num_bytes_) > |
| 53 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { |
| 54 return false; |
| 55 } |
| 56 |
| 57 int size = ashmem_get_size_region(platform_handle.get().fd); |
| 58 |
| 59 if (size < 0) { |
| 60 DPLOG(ERROR) << "ashmem_get_size_region()"; |
| 61 return false; |
| 62 } |
| 63 |
| 64 if (static_cast<size_t>(size) != num_bytes_) { |
| 65 LOG(ERROR) << "Shared memory region has the wrong size"; |
| 66 return false; |
| 67 } |
| 68 |
| 69 handle_ = platform_handle.Pass(); |
| 70 return true; |
| 71 } |
| 72 |
| 73 } // namespace embedder |
| 74 } // namespace mojo |
OLD | NEW |