1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/SharedMemoryBasic_android.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=8 et : 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include <android/log.h> 1.12 +#include <errno.h> 1.13 +#include <fcntl.h> 1.14 +#include <string.h> 1.15 +#include <sys/ioctl.h> 1.16 +#include <sys/mman.h> 1.17 +#include <sys/stat.h> 1.18 +#include <sys/types.h> 1.19 +#include <unistd.h> 1.20 + 1.21 +#include "base/process_util.h" 1.22 + 1.23 +#include "SharedMemoryBasic.h" 1.24 + 1.25 +// 1.26 +// Temporarily go directly to the kernel interface until we can 1.27 +// interact better with libcutils. 1.28 +// 1.29 +#include <linux/ashmem.h> 1.30 + 1.31 +namespace mozilla { 1.32 +namespace ipc { 1.33 + 1.34 +static void 1.35 +LogError(const char* what) 1.36 +{ 1.37 + __android_log_print(ANDROID_LOG_ERROR, "Gecko", 1.38 + "%s: %s (%d)", what, strerror(errno), errno); 1.39 +} 1.40 + 1.41 +SharedMemoryBasic::SharedMemoryBasic() 1.42 + : mShmFd(-1) 1.43 + , mMemory(nullptr) 1.44 +{ } 1.45 + 1.46 +SharedMemoryBasic::SharedMemoryBasic(const Handle& aHandle) 1.47 + : mShmFd(aHandle.fd) 1.48 + , mMemory(nullptr) 1.49 +{ } 1.50 + 1.51 +SharedMemoryBasic::~SharedMemoryBasic() 1.52 +{ 1.53 + Unmap(); 1.54 + Destroy(); 1.55 +} 1.56 + 1.57 +bool 1.58 +SharedMemoryBasic::Create(size_t aNbytes) 1.59 +{ 1.60 + NS_ABORT_IF_FALSE(-1 == mShmFd, "Already Create()d"); 1.61 + 1.62 + // Carve a new instance off of /dev/ashmem 1.63 + int shmfd = open("/" ASHMEM_NAME_DEF, O_RDWR, 0600); 1.64 + if (-1 == shmfd) { 1.65 + LogError("ShmemAndroid::Create():open"); 1.66 + return false; 1.67 + } 1.68 + 1.69 + if (ioctl(shmfd, ASHMEM_SET_SIZE, aNbytes)) { 1.70 + LogError("ShmemAndroid::Unmap():ioctl(SET_SIZE)"); 1.71 + close(shmfd); 1.72 + return false; 1.73 + } 1.74 + 1.75 + mShmFd = shmfd; 1.76 + Created(aNbytes); 1.77 + return true; 1.78 +} 1.79 + 1.80 +bool 1.81 +SharedMemoryBasic::Map(size_t nBytes) 1.82 +{ 1.83 + NS_ABORT_IF_FALSE(nullptr == mMemory, "Already Map()d"); 1.84 + 1.85 + mMemory = mmap(nullptr, nBytes, 1.86 + PROT_READ | PROT_WRITE, 1.87 + MAP_SHARED, 1.88 + mShmFd, 1.89 + 0); 1.90 + if (MAP_FAILED == mMemory) { 1.91 + LogError("ShmemAndroid::Map()"); 1.92 + mMemory = nullptr; 1.93 + return false; 1.94 + } 1.95 + 1.96 + Mapped(nBytes); 1.97 + return true; 1.98 +} 1.99 + 1.100 +bool 1.101 +SharedMemoryBasic::ShareToProcess(base::ProcessHandle/*unused*/, 1.102 + Handle* aNewHandle) 1.103 +{ 1.104 + NS_ABORT_IF_FALSE(mShmFd >= 0, "Should have been Create()d by now"); 1.105 + 1.106 + int shmfdDup = dup(mShmFd); 1.107 + if (-1 == shmfdDup) { 1.108 + LogError("ShmemAndroid::ShareToProcess()"); 1.109 + return false; 1.110 + } 1.111 + 1.112 + aNewHandle->fd = shmfdDup; 1.113 + aNewHandle->auto_close = true; 1.114 + return true; 1.115 +} 1.116 + 1.117 +void 1.118 +SharedMemoryBasic::Unmap() 1.119 +{ 1.120 + if (!mMemory) { 1.121 + return; 1.122 + } 1.123 + 1.124 + if (munmap(mMemory, Size())) { 1.125 + LogError("ShmemAndroid::Unmap()"); 1.126 + } 1.127 + mMemory = nullptr; 1.128 +} 1.129 + 1.130 +void 1.131 +SharedMemoryBasic::Destroy() 1.132 +{ 1.133 + if (mShmFd > 0) { 1.134 + close(mShmFd); 1.135 + } 1.136 +} 1.137 + 1.138 +} // namespace ipc 1.139 +} // namespace mozilla