michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=8 et : michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/process_util.h" michael@0: michael@0: #include "SharedMemoryBasic.h" michael@0: michael@0: // michael@0: // Temporarily go directly to the kernel interface until we can michael@0: // interact better with libcutils. michael@0: // michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: static void michael@0: LogError(const char* what) michael@0: { michael@0: __android_log_print(ANDROID_LOG_ERROR, "Gecko", michael@0: "%s: %s (%d)", what, strerror(errno), errno); michael@0: } michael@0: michael@0: SharedMemoryBasic::SharedMemoryBasic() michael@0: : mShmFd(-1) michael@0: , mMemory(nullptr) michael@0: { } michael@0: michael@0: SharedMemoryBasic::SharedMemoryBasic(const Handle& aHandle) michael@0: : mShmFd(aHandle.fd) michael@0: , mMemory(nullptr) michael@0: { } michael@0: michael@0: SharedMemoryBasic::~SharedMemoryBasic() michael@0: { michael@0: Unmap(); michael@0: Destroy(); michael@0: } michael@0: michael@0: bool michael@0: SharedMemoryBasic::Create(size_t aNbytes) michael@0: { michael@0: NS_ABORT_IF_FALSE(-1 == mShmFd, "Already Create()d"); michael@0: michael@0: // Carve a new instance off of /dev/ashmem michael@0: int shmfd = open("/" ASHMEM_NAME_DEF, O_RDWR, 0600); michael@0: if (-1 == shmfd) { michael@0: LogError("ShmemAndroid::Create():open"); michael@0: return false; michael@0: } michael@0: michael@0: if (ioctl(shmfd, ASHMEM_SET_SIZE, aNbytes)) { michael@0: LogError("ShmemAndroid::Unmap():ioctl(SET_SIZE)"); michael@0: close(shmfd); michael@0: return false; michael@0: } michael@0: michael@0: mShmFd = shmfd; michael@0: Created(aNbytes); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SharedMemoryBasic::Map(size_t nBytes) michael@0: { michael@0: NS_ABORT_IF_FALSE(nullptr == mMemory, "Already Map()d"); michael@0: michael@0: mMemory = mmap(nullptr, nBytes, michael@0: PROT_READ | PROT_WRITE, michael@0: MAP_SHARED, michael@0: mShmFd, michael@0: 0); michael@0: if (MAP_FAILED == mMemory) { michael@0: LogError("ShmemAndroid::Map()"); michael@0: mMemory = nullptr; michael@0: return false; michael@0: } michael@0: michael@0: Mapped(nBytes); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SharedMemoryBasic::ShareToProcess(base::ProcessHandle/*unused*/, michael@0: Handle* aNewHandle) michael@0: { michael@0: NS_ABORT_IF_FALSE(mShmFd >= 0, "Should have been Create()d by now"); michael@0: michael@0: int shmfdDup = dup(mShmFd); michael@0: if (-1 == shmfdDup) { michael@0: LogError("ShmemAndroid::ShareToProcess()"); michael@0: return false; michael@0: } michael@0: michael@0: aNewHandle->fd = shmfdDup; michael@0: aNewHandle->auto_close = true; michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: SharedMemoryBasic::Unmap() michael@0: { michael@0: if (!mMemory) { michael@0: return; michael@0: } michael@0: michael@0: if (munmap(mMemory, Size())) { michael@0: LogError("ShmemAndroid::Unmap()"); michael@0: } michael@0: mMemory = nullptr; michael@0: } michael@0: michael@0: void michael@0: SharedMemoryBasic::Destroy() michael@0: { michael@0: if (mShmFd > 0) { michael@0: close(mShmFd); michael@0: } michael@0: } michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla