2010-09-11 01:45:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=8 et :
|
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-09-11 01:45:59 +04:00
|
|
|
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "base/process_util.h"
|
|
|
|
|
|
|
|
#include "SharedMemoryBasic.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Temporarily go directly to the kernel interface until we can
|
|
|
|
// interact better with libcutils.
|
|
|
|
//
|
2010-10-29 23:22:28 +04:00
|
|
|
#include <linux/ashmem.h>
|
2010-09-11 01:45:59 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
static void
|
|
|
|
LogError(const char* what)
|
|
|
|
{
|
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "Gecko",
|
|
|
|
"%s: %s (%d)", what, strerror(errno), errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedMemoryBasic::SharedMemoryBasic()
|
|
|
|
: mShmFd(-1)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mMemory(nullptr)
|
2010-09-11 01:45:59 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
SharedMemoryBasic::SharedMemoryBasic(const Handle& aHandle)
|
|
|
|
: mShmFd(aHandle.fd)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mMemory(nullptr)
|
2010-09-11 01:45:59 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
SharedMemoryBasic::~SharedMemoryBasic()
|
|
|
|
{
|
|
|
|
Unmap();
|
2010-11-05 10:17:07 +03:00
|
|
|
Destroy();
|
2010-09-11 01:45:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedMemoryBasic::Create(size_t aNbytes)
|
|
|
|
{
|
2015-02-07 02:04:32 +03:00
|
|
|
NS_ABORT_IF_FALSE(-1 == mShmFd, "Already Create()d");
|
2010-09-11 01:45:59 +04:00
|
|
|
|
|
|
|
// Carve a new instance off of /dev/ashmem
|
2010-10-29 23:22:28 +04:00
|
|
|
int shmfd = open("/" ASHMEM_NAME_DEF, O_RDWR, 0600);
|
2010-09-11 01:45:59 +04:00
|
|
|
if (-1 == shmfd) {
|
|
|
|
LogError("ShmemAndroid::Create():open");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(shmfd, ASHMEM_SET_SIZE, aNbytes)) {
|
|
|
|
LogError("ShmemAndroid::Unmap():ioctl(SET_SIZE)");
|
|
|
|
close(shmfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mShmFd = shmfd;
|
2010-11-05 10:17:07 +03:00
|
|
|
Created(aNbytes);
|
2010-09-11 01:45:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedMemoryBasic::Map(size_t nBytes)
|
|
|
|
{
|
2015-02-07 02:04:32 +03:00
|
|
|
NS_ABORT_IF_FALSE(nullptr == mMemory, "Already Map()d");
|
2010-09-11 01:45:59 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mMemory = mmap(nullptr, nBytes,
|
2010-09-11 01:45:59 +04:00
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
mShmFd,
|
|
|
|
0);
|
|
|
|
if (MAP_FAILED == mMemory) {
|
|
|
|
LogError("ShmemAndroid::Map()");
|
2012-07-30 18:20:58 +04:00
|
|
|
mMemory = nullptr;
|
2010-09-11 01:45:59 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-05 10:17:07 +03:00
|
|
|
Mapped(nBytes);
|
2010-09-11 01:45:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedMemoryBasic::ShareToProcess(base::ProcessHandle/*unused*/,
|
|
|
|
Handle* aNewHandle)
|
|
|
|
{
|
2015-02-07 02:04:32 +03:00
|
|
|
NS_ABORT_IF_FALSE(mShmFd >= 0, "Should have been Create()d by now");
|
2010-09-11 01:45:59 +04:00
|
|
|
|
|
|
|
int shmfdDup = dup(mShmFd);
|
|
|
|
if (-1 == shmfdDup) {
|
|
|
|
LogError("ShmemAndroid::ShareToProcess()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aNewHandle->fd = shmfdDup;
|
|
|
|
aNewHandle->auto_close = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedMemoryBasic::Unmap()
|
|
|
|
{
|
|
|
|
if (!mMemory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-05 10:17:07 +03:00
|
|
|
if (munmap(mMemory, Size())) {
|
2010-09-11 01:45:59 +04:00
|
|
|
LogError("ShmemAndroid::Unmap()");
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mMemory = nullptr;
|
2010-09-11 01:45:59 +04:00
|
|
|
}
|
|
|
|
|
2010-11-05 10:17:07 +03:00
|
|
|
void
|
|
|
|
SharedMemoryBasic::Destroy()
|
|
|
|
{
|
|
|
|
if (mShmFd > 0) {
|
|
|
|
close(mShmFd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 01:45:59 +04:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|