1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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>
12 #include <sys/ioctl.h>
15 #include <sys/types.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>
32 LogError(const char* what
)
34 __android_log_print(ANDROID_LOG_ERROR
, "Goanna",
35 "%s: %s (%d)", what
, strerror(errno
), errno
);
38 SharedMemoryBasic::SharedMemoryBasic()
43 SharedMemoryBasic::SharedMemoryBasic(const Handle
& aHandle
)
48 SharedMemoryBasic::~SharedMemoryBasic()
55 SharedMemoryBasic::Create(size_t aNbytes
)
57 MOZ_ASSERT(-1 == mShmFd
, "Already Create()d");
59 // Carve a new instance off of /dev/ashmem
60 int shmfd
= open("/" ASHMEM_NAME_DEF
, O_RDWR
, 0600);
62 LogError("ShmemAndroid::Create():open");
66 if (ioctl(shmfd
, ASHMEM_SET_SIZE
, aNbytes
)) {
67 LogError("ShmemAndroid::Unmap():ioctl(SET_SIZE)");
78 SharedMemoryBasic::Map(size_t nBytes
)
80 MOZ_ASSERT(nullptr == mMemory
, "Already Map()d");
82 mMemory
= mmap(nullptr, nBytes
,
83 PROT_READ
| PROT_WRITE
,
87 if (MAP_FAILED
== mMemory
) {
88 LogError("ShmemAndroid::Map()");
98 SharedMemoryBasic::ShareToProcess(base::ProcessHandle
/*unused*/,
101 MOZ_ASSERT(mShmFd
>= 0, "Should have been Create()d by now");
103 int shmfdDup
= dup(mShmFd
);
104 if (-1 == shmfdDup
) {
105 LogError("ShmemAndroid::ShareToProcess()");
109 aNewHandle
->fd
= shmfdDup
;
110 aNewHandle
->auto_close
= true;
115 SharedMemoryBasic::Unmap()
121 if (munmap(mMemory
, Size())) {
122 LogError("ShmemAndroid::Unmap()");
128 SharedMemoryBasic::Destroy()
136 } // namespace mozilla