Bug 1665091: [devtools] Relax timing restrictions to test to prevent intermittency...
[gecko.git] / mozglue / android / SharedMemNatives.cpp
blobf38a2274b301886d08899f4d23865ae592cfe135
1 /* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <errno.h>
7 #include <jni.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/mman.h>
12 #include "mozilla/Sprintf.h"
14 extern "C" {
16 JNIEXPORT
17 void JNICALL
18 Java_org_mozilla_gecko_media_SampleBuffer_nativeReadFromDirectBuffer(
19 JNIEnv* jenv, jclass, jobject src, jlong dest, jint offset, jint size) {
20 uint8_t* from = static_cast<uint8_t*>(jenv->GetDirectBufferAddress(src));
21 if (from == nullptr) {
22 jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"),
23 "Null direct buffer");
24 return;
27 void* to = reinterpret_cast<void*>(dest);
28 if (to == nullptr) {
29 jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"),
30 "Null shared memory buffer");
31 return;
34 memcpy(to, from + offset, size);
37 JNIEXPORT
38 void JNICALL
39 Java_org_mozilla_gecko_media_SampleBuffer_nativeWriteToDirectBuffer(
40 JNIEnv* jenv, jclass, jlong src, jobject dest, jint offset, jint size) {
41 uint8_t* from = reinterpret_cast<uint8_t*>(src);
42 if (from == nullptr) {
43 jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"),
44 "Null shared memory buffer");
45 return;
48 void* to = jenv->GetDirectBufferAddress(dest);
49 if (to == nullptr) {
50 jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"),
51 "Null direct buffer");
52 return;
55 memcpy(to, from + offset, size);
58 JNIEXPORT
59 jlong JNICALL Java_org_mozilla_gecko_mozglue_SharedMemory_map(JNIEnv* env,
60 jobject jobj,
61 jint fd,
62 jint length) {
63 void* address = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
64 if (address == MAP_FAILED) {
65 char msg[128];
66 SprintfLiteral(msg, "mmap failed. errno=%d", errno);
67 env->ThrowNew(env->FindClass("java/lang/NullPointerException"), msg);
68 return 0;
70 return jlong(address);
73 JNIEXPORT
74 void JNICALL Java_org_mozilla_gecko_mozglue_SharedMemory_unmap(JNIEnv* env,
75 jobject jobj,
76 jlong address,
77 jint size) {
78 munmap((void*)address, (size_t)size);