From 9ba98f0de0dd40389c737e18c2fabbd29de62ac6 Mon Sep 17 00:00:00 2001 From: Scrappers Date: Fri, 20 May 2022 19:50:43 +0200 Subject: [PATCH 1/5] Android: Implemented AndroidNativeBufferAllocator - Deprecated AndroidBufferAllocator --- jme3-android-native/bufferallocator.gradle | 59 +++++++++++++ jme3-android-native/build.gradle | 1 + .../src/native/jme_bufferallocator/Android.mk | 50 +++++++++++ .../native/jme_bufferallocator/Application.mk | 39 +++++++++ ...m_jme3_util_AndroidNativeBufferAllocator.c | 85 +++++++++++++++++++ .../com/jme3/system/android/OGLESContext.java | 4 +- .../com/jme3/util/AndroidBufferAllocator.java | 3 +- .../util/AndroidNativeBufferAllocator.java | 74 ++++++++++++++++ 8 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 jme3-android-native/bufferallocator.gradle create mode 100644 jme3-android-native/src/native/jme_bufferallocator/Android.mk create mode 100644 jme3-android-native/src/native/jme_bufferallocator/Application.mk create mode 100644 jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c create mode 100644 jme3-android/src/main/java/com/jme3/util/AndroidNativeBufferAllocator.java diff --git a/jme3-android-native/bufferallocator.gradle b/jme3-android-native/bufferallocator.gradle new file mode 100644 index 0000000000..f03027e275 --- /dev/null +++ b/jme3-android-native/bufferallocator.gradle @@ -0,0 +1,59 @@ +// build file for native buffer allocator, created by pavl_g on 5/17/22. + +// directories for native source +String bufferAllocatorAndroidPath = 'src/native/jme_bufferallocator' +String bufferAllocatorHeaders = 'src/native/headers' + +//Pre-compiled libs directory +def rootPath = rootProject.projectDir.absolutePath +String bufferAllocatorPreCompiledLibsDir = + rootPath + File.separator + "build" + File.separator + 'native' + File.separator + 'android' + File.separator + 'allocator' + +// directories for build +String bufferAllocatorBuildDir = "$buildDir" + File.separator + "bufferallocator" +String bufferAllocatorJniDir = bufferAllocatorBuildDir + File.separator + "jni" +String bufferAllocatorHeadersBuildDir = bufferAllocatorJniDir + File.separator + "headers" +String bufferAllocatorBuildLibsDir = bufferAllocatorBuildDir + File.separator + "libs" + +// copy native src to build dir +task copyJmeBufferAllocator(type: Copy) { + from file(bufferAllocatorAndroidPath) + into file(bufferAllocatorJniDir) +} + +// copy native headers to build dir +task copyJmeHeadersBufferAllocator(type: Copy, dependsOn: copyJmeBufferAllocator) { + from file(bufferAllocatorHeaders) + into file(bufferAllocatorHeadersBuildDir) +} + +// compile and build copied natives in build dir +task buildBufferAllocatorNativeLib(type: Exec, dependsOn: [copyJmeBufferAllocator, copyJmeHeadersBufferAllocator]) { + workingDir bufferAllocatorBuildDir + executable rootProject.ndkCommandPath + args "-j" + Runtime.runtime.availableProcessors() +} + +task updatePreCompiledLibsBufferAllocator(type: Copy, dependsOn: buildBufferAllocatorNativeLib) { + from file(bufferAllocatorBuildLibsDir) + into file(bufferAllocatorPreCompiledLibsDir) +} + +// Copy pre-compiled libs to build directory (when not building new libs) +task copyPreCompiledLibsBufferAllocator(type: Copy) { + from file(bufferAllocatorPreCompiledLibsDir) + into file(bufferAllocatorBuildLibsDir) +} + +// ndkExists is a boolean from the build.gradle in the root project +// buildNativeProjects is a string set to "true" +if (ndkExists && buildNativeProjects == "true") { + // build native libs and update stored pre-compiled libs to commit + compileJava.dependsOn { updatePreCompiledLibsBufferAllocator } +} else { + // use pre-compiled native libs (not building new ones) + compileJava.dependsOn { copyPreCompiledLibsBufferAllocator } +} + +// package the native object files inside the lib folder in a production jar +jar.into("lib") { from bufferAllocatorBuildLibsDir } diff --git a/jme3-android-native/build.gradle b/jme3-android-native/build.gradle index d0618bb1ea..cc78ab4722 100644 --- a/jme3-android-native/build.gradle +++ b/jme3-android-native/build.gradle @@ -35,3 +35,4 @@ apply from: file('openalsoft.gradle') // apply from: file('stb_image.gradle') // apply from: file('tremor.gradle') apply from: file('decode.gradle') +apply from: file('bufferallocator.gradle') diff --git a/jme3-android-native/src/native/jme_bufferallocator/Android.mk b/jme3-android-native/src/native/jme_bufferallocator/Android.mk new file mode 100644 index 0000000000..d735478fb6 --- /dev/null +++ b/jme3-android-native/src/native/jme_bufferallocator/Android.mk @@ -0,0 +1,50 @@ +# +# Copyright (c) 2009-2022 jMonkeyEngine +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of 'jMonkeyEngine' nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +## +# Created by pavl_g on 5/17/22. +# For more : https://developer.android.com/ndk/guides/android_mk. +## +TARGET_PLATFORM := android-19 + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_LDLIBS := -llog -Wl,-s + +LOCAL_MODULE := bufferallocatorjme + +LOCAL_C_INCLUDES := $(LOCAL_PATH) + +LOCAL_SRC_FILES := com_jme3_util_AndroidNativeBufferAllocator.c + +include $(BUILD_SHARED_LIBRARY) diff --git a/jme3-android-native/src/native/jme_bufferallocator/Application.mk b/jme3-android-native/src/native/jme_bufferallocator/Application.mk new file mode 100644 index 0000000000..4bcc2ef85e --- /dev/null +++ b/jme3-android-native/src/native/jme_bufferallocator/Application.mk @@ -0,0 +1,39 @@ +# +# Copyright (c) 2009-2022 jMonkeyEngine +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of 'jMonkeyEngine' nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +## +# Created by pavl_g on 5/17/22. +# For more : https://developer.android.com/ndk/guides/application_mk. +## +APP_PLATFORM := android-19 +# change this to 'debug' to see android logs +APP_OPTIM := release +APP_ABI := all \ No newline at end of file diff --git a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c new file mode 100644 index 0000000000..c685a80914 --- /dev/null +++ b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2009-2022 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file com_jme3_util_AndroidNativeBufferAllocator.c + * @author pavl_g. + * @brief Creates and releases direct byte buffers for {com.jme3.util.AndroidNativeBufferAllocator}. + * @date 2022-05-17. + * @note + * Find more at : + * - JNI Direct byte buffers : https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#NewDirectByteBuffer. + * - JNI Get Direct byte buffer : https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#GetDirectBufferAddress. + * - GNU Allocating memory : https://www.gnu.org/software/libc/manual/html_node/Basic-Allocation.html. + * - GNU Freeing memory : https://www.gnu.org/software/libc/manual/html_node/Freeing-after-Malloc.html. + * - Android logging : https://developer.android.com/ndk/reference/group/logging. + * - Android logging example : https://github.com/android/ndk-samples/blob/7a8ff4c5529fce6ec4c5796efbe773f5d0e569cc/hello-libs/app/src/main/cpp/hello-libs.cpp#L25-L26. + */ + +#include "headers/com_jme3_util_AndroidNativeBufferAllocator.h" +#include + +#ifndef NDEBUG +#include +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, \ + "AndroidNativeBufferAllocator", ##__VA_ARGS__); +#else +#define LOGI(...) +#endif + +JNIEXPORT void JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_releaseDirectByteBuffer +(JNIEnv * env, jobject object, jobject bufferObject) +{ + void* buffer = (*env)->GetDirectBufferAddress(env, bufferObject); + // deallocates the buffer pointer + free(buffer); + // log the destruction by mem address + LOGI("Buffer released (mem_address, size) -> (%p, %lu)", buffer, sizeof(buffer)); + // avoid accessing this memory space by resetting the memory address + buffer = NULL; + LOGI("Buffer mem_address formatted (mem_address, size) -> (%p, %lu)", buffer, sizeof(buffer)); +} + +JNIEXPORT jobject JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_createDirectByteBuffer +(JNIEnv * env, jobject object, jlong size) +{ + void* buffer = malloc(size); + // create a new buffer object starting from memory address of buffer ptr + // and with a size + jobject bufferObject = (*env)->NewDirectByteBuffer(env, buffer, size); + if (bufferObject != NULL) { + LOGI("Buffer created (mem_address, size) -> (%p, %lli)", buffer, size); + } else { + LOGI("Buffer cannot be created (mem_address, size) -> (%p, %lli)", buffer, size); + } + return bufferObject; +} \ No newline at end of file diff --git a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java index 6d34238100..a80eb7cccd 100644 --- a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java +++ b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java @@ -54,8 +54,8 @@ import com.jme3.renderer.android.AndroidGL; import com.jme3.renderer.opengl.*; import com.jme3.system.*; -import com.jme3.util.AndroidBufferAllocator; import com.jme3.util.BufferAllocatorFactory; +import com.jme3.util.AndroidNativeBufferAllocator; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -82,7 +82,7 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex final String implementation = BufferAllocatorFactory.PROPERTY_BUFFER_ALLOCATOR_IMPLEMENTATION; if (System.getProperty(implementation) == null) { - System.setProperty(implementation, AndroidBufferAllocator.class.getName()); + System.setProperty(implementation, AndroidNativeBufferAllocator.class.getName()); } } diff --git a/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java b/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java index 596bafc138..d86017c5aa 100644 --- a/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java +++ b/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2019 jMonkeyEngine + * Copyright (c) 2009-2022 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -40,6 +40,7 @@ /** * @author Jesus Oliver + * @deprecated implemented {@link AndroidNativeBufferAllocator} instead. */ public class AndroidBufferAllocator implements BufferAllocator { diff --git a/jme3-android/src/main/java/com/jme3/util/AndroidNativeBufferAllocator.java b/jme3-android/src/main/java/com/jme3/util/AndroidNativeBufferAllocator.java new file mode 100644 index 0000000000..f91334db7e --- /dev/null +++ b/jme3-android/src/main/java/com/jme3/util/AndroidNativeBufferAllocator.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2009-2022 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.util; + +import java.nio.Buffer; +import java.nio.ByteBuffer; + +/** + * Allocates and destroys direct byte buffers using native code. + * + * @author pavl_g. + */ +public final class AndroidNativeBufferAllocator implements BufferAllocator { + + static { + System.loadLibrary("bufferallocatorjme"); + } + + @Override + public void destroyDirectBuffer(Buffer toBeDestroyed) { + releaseDirectByteBuffer(toBeDestroyed); + } + + @Override + public ByteBuffer allocate(int size) { + return createDirectByteBuffer(size); + } + + /** + * Releases the memory of a direct buffer using a buffer object reference. + * + * @param buffer the buffer reference to release its memory. + * @see AndroidNativeBufferAllocator#destroyDirectBuffer(Buffer) + */ + private native void releaseDirectByteBuffer(Buffer buffer); + + /** + * Creates a new direct byte buffer explicitly with a specific size. + * + * @param size the byte buffer size used for allocating the buffer. + * @return a new direct byte buffer object. + * @see AndroidNativeBufferAllocator#allocate(int) + */ + private native ByteBuffer createDirectByteBuffer(long size); +} \ No newline at end of file From 2b9317b97a448b74cd584a095eca8b6c225f8b69 Mon Sep 17 00:00:00 2001 From: Scrappers Date: Sat, 21 May 2022 13:49:31 +0200 Subject: [PATCH 2/5] AndroidBufferAllocator: Added @Deprecated annotation --- .../src/main/java/com/jme3/util/AndroidBufferAllocator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java b/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java index d86017c5aa..6ddc00f17e 100644 --- a/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java +++ b/jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java @@ -42,6 +42,7 @@ * @author Jesus Oliver * @deprecated implemented {@link AndroidNativeBufferAllocator} instead. */ +@Deprecated public class AndroidBufferAllocator implements BufferAllocator { // We make use of the ReflectionAllocator to remove the inner buffer From 7d89ff99a9b068bc3c28563ba9df22dff32b380e Mon Sep 17 00:00:00 2001 From: Scrappers Date: Sat, 21 May 2022 15:19:04 +0200 Subject: [PATCH 3/5] com_jme3_util_AndroidNativeBufferAllocator: migrated to `calloc()` and better error logging --- ...m_jme3_util_AndroidNativeBufferAllocator.c | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c index c685a80914..b83c00c326 100644 --- a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c +++ b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c @@ -39,7 +39,9 @@ * Find more at : * - JNI Direct byte buffers : https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#NewDirectByteBuffer. * - JNI Get Direct byte buffer : https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#GetDirectBufferAddress. - * - GNU Allocating memory : https://www.gnu.org/software/libc/manual/html_node/Basic-Allocation.html. + * - GNU Basic allocation : https://www.gnu.org/software/libc/manual/html_node/Basic-Allocation.html. + * - GNU Allocating Cleared Space : https://www.gnu.org/software/libc/manual/html_node/Allocating-Cleared-Space.html. + * - GNU No Memory error : https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html#index-ENOMEM. * - GNU Freeing memory : https://www.gnu.org/software/libc/manual/html_node/Freeing-after-Malloc.html. * - Android logging : https://developer.android.com/ndk/reference/group/logging. * - Android logging example : https://github.com/android/ndk-samples/blob/7a8ff4c5529fce6ec4c5796efbe773f5d0e569cc/hello-libs/app/src/main/cpp/hello-libs.cpp#L25-L26. @@ -47,15 +49,29 @@ #include "headers/com_jme3_util_AndroidNativeBufferAllocator.h" #include +#include +#include #ifndef NDEBUG #include -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, \ - "AndroidNativeBufferAllocator", ##__VA_ARGS__); +#define LOG(LOG_ID, ...) __android_log_print(LOG_ID, \ + "AndroidNativeBufferAllocator", ##__VA_ARGS__); #else -#define LOGI(...) +#define LOG(...) #endif +bool isDeviceOutOfMemory(void*); + +/** + * @brief Tests if the device is out of memory. + * + * @return true if the buffer to allocate is a NULL pointer and the errno is ENOMEM (Error-no-memory). + * @return false otherwise. + */ +bool isDeviceOutOfMemory(void* buffer) { + return buffer == NULL && errno == ENOMEM; +} + JNIEXPORT void JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_releaseDirectByteBuffer (JNIEnv * env, jobject object, jobject bufferObject) { @@ -63,23 +79,21 @@ JNIEXPORT void JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_releaseDi // deallocates the buffer pointer free(buffer); // log the destruction by mem address - LOGI("Buffer released (mem_address, size) -> (%p, %lu)", buffer, sizeof(buffer)); + LOG(ANDROID_LOG_INFO, "Buffer released (mem_address, size) -> (%p, %lu)", buffer, sizeof(buffer)); // avoid accessing this memory space by resetting the memory address buffer = NULL; - LOGI("Buffer mem_address formatted (mem_address, size) -> (%p, %lu)", buffer, sizeof(buffer)); + LOG(ANDROID_LOG_INFO, "Buffer mem_address formatted (mem_address, size) -> (%p, %u)", buffer, sizeof(buffer)); } JNIEXPORT jobject JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_createDirectByteBuffer (JNIEnv * env, jobject object, jlong size) { - void* buffer = malloc(size); - // create a new buffer object starting from memory address of buffer ptr - // and with a size - jobject bufferObject = (*env)->NewDirectByteBuffer(env, buffer, size); - if (bufferObject != NULL) { - LOGI("Buffer created (mem_address, size) -> (%p, %lli)", buffer, size); + void* buffer = calloc(1, size); + if (isDeviceOutOfMemory(buffer)) { + LOG(ANDROID_LOG_FATAL, "Device is out of memory"); + exit(errno); } else { - LOGI("Buffer cannot be created (mem_address, size) -> (%p, %lli)", buffer, size); + LOG(ANDROID_LOG_INFO, "Buffer created successfully (mem_address, size) -> (%p %lli)", buffer, size); } - return bufferObject; + return (*env)->NewDirectByteBuffer(env, buffer, size); } \ No newline at end of file From dede03139492fd96c18e01dae9b79014dbdbe620 Mon Sep 17 00:00:00 2001 From: Scrappers Date: Sat, 21 May 2022 15:36:15 +0200 Subject: [PATCH 4/5] com_jme3_util_AndroidNativeBufferAllocator: addressing the errno with the fatal log --- .../com_jme3_util_AndroidNativeBufferAllocator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c index b83c00c326..e02c0694ad 100644 --- a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c +++ b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c @@ -90,7 +90,7 @@ JNIEXPORT jobject JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_create { void* buffer = calloc(1, size); if (isDeviceOutOfMemory(buffer)) { - LOG(ANDROID_LOG_FATAL, "Device is out of memory"); + LOG(ANDROID_LOG_FATAL, "Device is out of memory exiting with %u ", errno); exit(errno); } else { LOG(ANDROID_LOG_INFO, "Buffer created successfully (mem_address, size) -> (%p %lli)", buffer, size); From 9b82cbfc8a69e2a1f755dce7c7ba4c005abc438d Mon Sep 17 00:00:00 2001 From: Scrappers Date: Sat, 21 May 2022 15:38:15 +0200 Subject: [PATCH 5/5] com_jme3_util_AndroidNativeBufferAllocator: removing an extra empty space --- .../com_jme3_util_AndroidNativeBufferAllocator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c index e02c0694ad..4f5cd66d09 100644 --- a/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c +++ b/jme3-android-native/src/native/jme_bufferallocator/com_jme3_util_AndroidNativeBufferAllocator.c @@ -90,7 +90,7 @@ JNIEXPORT jobject JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_create { void* buffer = calloc(1, size); if (isDeviceOutOfMemory(buffer)) { - LOG(ANDROID_LOG_FATAL, "Device is out of memory exiting with %u ", errno); + LOG(ANDROID_LOG_FATAL, "Device is out of memory exiting with %u", errno); exit(errno); } else { LOG(ANDROID_LOG_INFO, "Buffer created successfully (mem_address, size) -> (%p %lli)", buffer, size);