Skip to content

Commit

Permalink
musikr: implement raii jni classes
Browse files Browse the repository at this point in the history
This should hopefully mitigate the memory leak problems unless I forget
to transfer over ref ownership to the corresponding class. Analyzed
memory use on load and it looks like the JVM is able to reclaim
everything extracted by the native code, so I should hopefully be fine.
  • Loading branch information
OxygenCobalt committed Jan 19, 2025
1 parent 1bf44eb commit a0e10ef
Show file tree
Hide file tree
Showing 18 changed files with 694 additions and 388 deletions.
10 changes: 7 additions & 3 deletions musikr/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ set_target_properties(
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
taglib_jni.cpp
JVMInputStream.cpp
JVMTagMap.cpp
JVMMetadataBuilder.cpp
JInputStream.cpp
JTagMap.cpp
JMetadataBuilder.cpp
JClassRef.cpp
JObjectRef.cpp
JStringRef.cpp
JByteArrayRef.cpp
)
target_link_options(${CMAKE_PROJECT_NAME}
# @Tolriq found that these flags can reduce the size of the linked
Expand Down
47 changes: 47 additions & 0 deletions musikr/src/main/cpp/JByteArrayRef.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025 Auxio Project
* JByteArrayRef.cpp is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "JByteArrayRef.h"

JByteArrayRef::JByteArrayRef(JNIEnv *env, TagLib::ByteVector &data) : env(env) {
auto size = static_cast<jsize>(data.size());
array = env->NewByteArray(size);
env->SetByteArrayRegion(array, 0, static_cast<jsize>(size),
reinterpret_cast<const jbyte*>(data.data()));
}

JByteArrayRef::JByteArrayRef(JNIEnv *env, jbyteArray array) : env(env), array(
array) {
}

JByteArrayRef::~JByteArrayRef() {
env->DeleteLocalRef(array);
}

TagLib::ByteVector JByteArrayRef::copy() {
jsize length = env->GetArrayLength(array);
auto data = env->GetByteArrayElements(array, nullptr);
TagLib::ByteVector byteVector(reinterpret_cast<const char*>(data), length);
env->ReleaseByteArrayElements(array, data, JNI_ABORT);
return byteVector;
}

jbyteArray& JByteArrayRef::operator*() {
return array;
}

45 changes: 45 additions & 0 deletions musikr/src/main/cpp/JByteArrayRef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 Auxio Project
* JByteArrayRef.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef AUXIO_JBYTEARRAYREF_H
#define AUXIO_JBYTEARRAYREF_H

#include <jni.h>
#include <taglib/tbytevector.h>

class JByteArrayRef {
public:
JByteArrayRef(JNIEnv *env, TagLib::ByteVector &data);
JByteArrayRef(JNIEnv *env, jbyteArray array);

~JByteArrayRef();

JByteArrayRef(const JByteArrayRef&) = delete;

JByteArrayRef& operator=(const JByteArrayRef&) = delete;

TagLib::ByteVector copy();

jbyteArray& operator*();

private:
JNIEnv *env;
jbyteArray array;
};

#endif //AUXIO_JBYTEARRAYREF_H
34 changes: 34 additions & 0 deletions musikr/src/main/cpp/JClassRef.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025 Auxio Project
* JClassRef.cpp is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "JClassRef.h"
JClassRef::JClassRef(JNIEnv *env, const char *classpath) : env(env) {
clazz = env->FindClass(classpath);
}

JClassRef::~JClassRef() {
env->DeleteLocalRef(clazz);
}

jmethodID JClassRef::method(const char *name, const char *signature) {
return env->GetMethodID(clazz, name, signature);
}

jclass& JClassRef::operator*() {
return clazz;
}
45 changes: 45 additions & 0 deletions musikr/src/main/cpp/JClassRef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 Auxio Project
* JClassRef.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef AUXIO_JCLASSREF_H
#define AUXIO_JCLASSREF_H

#include <jni.h>

class JClassRef {
public:
JClassRef(JNIEnv *env, const char *classpath);

~JClassRef();

JClassRef(const JClassRef&) = delete;

JClassRef& operator=(const JClassRef&) = delete;

// Only exists to work around a broken lint that doesn't
// realize that this class is a smart pointer to jclass.
jmethodID method(const char *name, const char *signature);

jclass& operator*();

private:
JNIEnv *env;
jclass clazz;
};

#endif //AUXIO_JCLASSREF_H
134 changes: 134 additions & 0 deletions musikr/src/main/cpp/JInputStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2024 Auxio Project
* JInputStream.cpp is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "JInputStream.h"

#include <cmath>

#include "JClassRef.h"
#include "JByteArrayRef.h"

JInputStream::JInputStream(JNIEnv *env, jobject jInputStream) : env(env), jInputStream(
jInputStream) {
JClassRef jInputStreamClass = { env,
"org/oxycblt/musikr/metadata/NativeInputStream" };
if (!env->IsInstanceOf(jInputStream, *jInputStreamClass)) {
throw std::runtime_error("oStream is not an instance of TagLibOStream");
}
jInputStreamReadBlockMethod = jInputStreamClass.method("readBlock",
"(J)[B");
jInputStreamIsOpenMethod = jInputStreamClass.method("isOpen", "()Z");
jInputStreamSeekFromBeginningMethod = jInputStreamClass.method(
"seekFromBeginning", "(J)Z");
jInputStreamSeekFromCurrentMethod = jInputStreamClass.method(
"seekFromCurrent", "(J)Z");
jInputStreamSeekFromEndMethod = jInputStreamClass.method("seekFromEnd",
"(J)Z");
jInputStreamTellMethod = jInputStreamClass.method("tell", "()J");
jInputStreamLengthMethod = jInputStreamClass.method("length", "()J");
}

JInputStream::~JInputStream() {
// The implicit assumption is that inputStream is managed by the owner,
// so we don't need to delete any references here
}

TagLib::FileName JInputStream::name() const {
// Not actually used except in FileRef, can safely ignore.
return "";
}

TagLib::ByteVector JInputStream::readBlock(size_t length) {
// Do manual memory management here since we don't to avoid the added abstraction
// overhead of a smart JByteArrayRef.
auto data = env->CallObjectMethod(jInputStream, jInputStreamReadBlockMethod,
static_cast<jlong>(length));
if (data == nullptr) {
throw std::runtime_error("Failed to read block, see logs");
}
JByteArrayRef jByteArray = { env, reinterpret_cast<jbyteArray>(data) };
return jByteArray.copy();
}

void JInputStream::writeBlock(const TagLib::ByteVector &data) {
throw std::runtime_error("Not implemented");
}

void JInputStream::insert(const TagLib::ByteVector &data,
TagLib::offset_t start, size_t replace) {
throw std::runtime_error("Not implemented");
}

void JInputStream::removeBlock(TagLib::offset_t start, size_t length) {
throw std::runtime_error("Not implemented");
}

bool JInputStream::readOnly() const {
return true;
}

bool JInputStream::isOpen() const {
return env->CallBooleanMethod(jInputStream, jInputStreamIsOpenMethod);
}

void JInputStream::seek(TagLib::offset_t offset, Position p) {
auto joffset = static_cast<jlong>(std::llround(offset));
jboolean result;
switch (p) {
case Beginning:
result = env->CallBooleanMethod(jInputStream,
jInputStreamSeekFromBeginningMethod, joffset);
break;
case Current:
result = env->CallBooleanMethod(jInputStream,
jInputStreamSeekFromCurrentMethod, joffset);
break;
case End:
result = env->CallBooleanMethod(jInputStream,
jInputStreamSeekFromEndMethod, joffset);
break;
}
if (!result) {
throw std::runtime_error("Failed to seek, see logs");
}
}

void JInputStream::clear() {
// Nothing to do
}

TagLib::offset_t JInputStream::tell() const {
jlong jposition = env->CallLongMethod(jInputStream, jInputStreamTellMethod);
if (jposition == INT64_MIN) {
throw std::runtime_error("Failed to get position, see logs");
}
return static_cast<TagLib::offset_t>(jposition);
}

TagLib::offset_t JInputStream::length() {
jlong jlength = env->CallLongMethod(jInputStream, jInputStreamLengthMethod);
if (jlength == INT64_MIN) {
throw std::runtime_error("Failed to get length, see logs");
}
return static_cast<TagLib::offset_t>(jlength);
}

void JInputStream::truncate(TagLib::offset_t length) {
throw std::runtime_error("Not implemented");
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2024 Auxio Project
* JVMInputStream.h is part of Auxio.
* JInputStream.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -16,21 +16,22 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef AUXIO_JVMINPUTSTREAM_H
#define AUXIO_JVMINPUTSTREAM_H
#ifndef AUXIO_JINPUTSTREAM_H
#define AUXIO_JINPUTSTREAM_H

#include <jni.h>
#include "JObjectRef.h"

#include "taglib/tiostream.h"

class JVMInputStream: public TagLib::IOStream {
class JInputStream: public TagLib::IOStream {
public:
JVMInputStream(JNIEnv *env, jobject inputStream);
JInputStream(JNIEnv *env, jobject jInputStream);

~JVMInputStream();
~JInputStream();

JVMInputStream(const JVMInputStream&) = delete;
JVMInputStream& operator=(const JVMInputStream&) = delete;
JInputStream(const JInputStream&) = delete;
JInputStream& operator=(const JInputStream&) = delete;

/*!
* Returns the stream name in the local file system encoding.
Expand Down Expand Up @@ -113,15 +114,14 @@ class JVMInputStream: public TagLib::IOStream {

private:
JNIEnv *env;
jobject inputStream;
jmethodID inputStreamReadBlockMethod;
jmethodID inputStreamIsOpenMethod;
jmethodID inputStreamSeekFromBeginningMethod;
jmethodID inputStreamSeekFromCurrentMethod;
jmethodID inputStreamSeekFromEndMethod;
jmethodID inputStreamTellMethod;
jmethodID inputStreamLengthMethod;

jobject jInputStream;
jmethodID jInputStreamReadBlockMethod;
jmethodID jInputStreamIsOpenMethod;
jmethodID jInputStreamSeekFromBeginningMethod;
jmethodID jInputStreamSeekFromCurrentMethod;
jmethodID jInputStreamSeekFromEndMethod;
jmethodID jInputStreamTellMethod;
jmethodID jInputStreamLengthMethod;
};

#endif //AUXIO_JVMINPUTSTREAM_H
#endif //AUXIO_JINPUTSTREAM_H
Loading

0 comments on commit a0e10ef

Please sign in to comment.