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