Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / SharedMemoryBasic_android.cpp
blobe3a0cb0118dd74225c4f30b44f01eeff0c979771
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include <android/log.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <sys/ioctl.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <unistd.h>
18 #include "base/process_util.h"
20 #include "SharedMemoryBasic.h"
23 // Temporarily go directly to the kernel interface until we can
24 // interact better with libcutils.
26 #include <linux/ashmem.h>
28 namespace mozilla {
29 namespace ipc {
31 static void
32 LogError(const char* what)
34 __android_log_print(ANDROID_LOG_ERROR, "Gecko",
35 "%s: %s (%d)", what, strerror(errno), errno);
38 SharedMemoryBasic::SharedMemoryBasic()
39 : mShmFd(-1)
40 , mMemory(nullptr)
41 { }
43 SharedMemoryBasic::SharedMemoryBasic(const Handle& aHandle)
44 : mShmFd(aHandle.fd)
45 , mMemory(nullptr)
46 { }
48 SharedMemoryBasic::~SharedMemoryBasic()
50 Unmap();
51 Destroy();
54 bool
55 SharedMemoryBasic::Create(size_t aNbytes)
57 NS_ABORT_IF_FALSE(-1 == mShmFd, "Already Create()d");
59 // Carve a new instance off of /dev/ashmem
60 int shmfd = open("/" ASHMEM_NAME_DEF, O_RDWR, 0600);
61 if (-1 == shmfd) {
62 LogError("ShmemAndroid::Create():open");
63 return false;
66 if (ioctl(shmfd, ASHMEM_SET_SIZE, aNbytes)) {
67 LogError("ShmemAndroid::Unmap():ioctl(SET_SIZE)");
68 close(shmfd);
69 return false;
72 mShmFd = shmfd;
73 Created(aNbytes);
74 return true;
77 bool
78 SharedMemoryBasic::Map(size_t nBytes)
80 NS_ABORT_IF_FALSE(nullptr == mMemory, "Already Map()d");
82 mMemory = mmap(nullptr, nBytes,
83 PROT_READ | PROT_WRITE,
84 MAP_SHARED,
85 mShmFd,
86 0);
87 if (MAP_FAILED == mMemory) {
88 LogError("ShmemAndroid::Map()");
89 mMemory = nullptr;
90 return false;
93 Mapped(nBytes);
94 return true;
97 bool
98 SharedMemoryBasic::ShareToProcess(base::ProcessHandle/*unused*/,
99 Handle* aNewHandle)
101 NS_ABORT_IF_FALSE(mShmFd >= 0, "Should have been Create()d by now");
103 int shmfdDup = dup(mShmFd);
104 if (-1 == shmfdDup) {
105 LogError("ShmemAndroid::ShareToProcess()");
106 return false;
109 aNewHandle->fd = shmfdDup;
110 aNewHandle->auto_close = true;
111 return true;
114 void
115 SharedMemoryBasic::Unmap()
117 if (!mMemory) {
118 return;
121 if (munmap(mMemory, Size())) {
122 LogError("ShmemAndroid::Unmap()");
124 mMemory = nullptr;
127 void
128 SharedMemoryBasic::Destroy()
130 if (mShmFd > 0) {
131 close(mShmFd);
135 } // namespace ipc
136 } // namespace mozilla