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/. */
12 #include "mozilla/Sprintf.h"
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");
27 void* to
= reinterpret_cast<void*>(dest
);
29 jenv
->ThrowNew(jenv
->FindClass("java/lang/NullPointerException"),
30 "Null shared memory buffer");
34 memcpy(to
, from
+ offset
, size
);
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");
48 void* to
= jenv
->GetDirectBufferAddress(dest
);
50 jenv
->ThrowNew(jenv
->FindClass("java/lang/NullPointerException"),
51 "Null direct buffer");
55 memcpy(to
, from
+ offset
, size
);
59 jlong JNICALL
Java_org_mozilla_gecko_mozglue_SharedMemory_map(JNIEnv
* env
,
63 void* address
= mmap(NULL
, length
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
64 if (address
== MAP_FAILED
) {
66 SprintfLiteral(msg
, "mmap failed. errno=%d", errno
);
67 env
->ThrowNew(env
->FindClass("java/lang/NullPointerException"), msg
);
70 return jlong(address
);
74 void JNICALL
Java_org_mozilla_gecko_mozglue_SharedMemory_unmap(JNIEnv
* env
,
78 munmap((void*)address
, (size_t)size
);