-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
1bf44eb
commit a0e10ef
Showing
18 changed files
with
694 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.