From 03c548d96e5c81d0fc39503fe3042cf03e0a75e2 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 16 Jun 2023 00:43:11 +0200 Subject: GlueGen Struct [1]: Enhance com.jogamp.common.nio.* to serve a most native-free-code Struct-Code generation Recfactored all NIO buffer utils to Buffers, i.e. buffer <-> address, memcpy, strnlen, etc Buffers: - Added copyNativeToDirectByteBuffer(..), allowing to copy a native memory slice into a direct buffer. - Added typeNameToBufferClass(String) and sizeOfBufferElem(Class) - Completed slize2(..) buffer-mapping methods - Exposure of safe getDirectByteBuffer(..) w/ null-check (package private) Added NativeBuffer.storeDirectAddress(..), allowing to write the array address into a native buffer (struct, etc), allowing to referencing the ElementBuffer (linear array of elements) and PointerBuffer (array of pointer). Hint: This can be read via PointerBuffer.wrap(..).get(0) Added ElementBuffer (a NativeBuffer) mapping an array of elements, completing native abstraction next to PointerBuffer (array of pointer). ElementBuffer can dereference an existing element-array by native address via ElementBuffer.derefPointer(..). Views of its content can be directly accessed via ElementBuffer.slice(..). +++ These utilities and buffer abstractions will allow to reuse code and simplify the GlueGen struct get/set implementations and help to reduce native code injection. --- src/native/common/Buffers.c | 30 ++++++++++++++++++++++++++++++ src/native/common/PointerBuffer.c | 14 -------------- 2 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 src/native/common/Buffers.c delete mode 100644 src/native/common/PointerBuffer.c (limited to 'src/native') diff --git a/src/native/common/Buffers.c b/src/native/common/Buffers.c new file mode 100644 index 0000000..0898e6b --- /dev/null +++ b/src/native/common/Buffers.c @@ -0,0 +1,30 @@ + +#include + +#include +#include + +#include + +#include "com_jogamp_common_nio_Buffers.h" + +JNIEXPORT jlong JNICALL +Java_com_jogamp_common_nio_Buffers_getDirectBufferAddressImpl(JNIEnv *env, jclass _unused, jobject directBuffer) { + return ( NULL != directBuffer ) ? (jlong) (intptr_t) (*env)->GetDirectBufferAddress(env, directBuffer) : 0L ; +} + +JNIEXPORT jobject JNICALL +Java_com_jogamp_common_nio_Buffers_getDirectByteBufferImpl(JNIEnv *env, jclass _unused, jlong japtr, jint jbyteCount) { + return ( 0 != japtr && 0 < jbyteCount ) ? (*env)->NewDirectByteBuffer(env, (void *)(intptr_t)japtr, jbyteCount) : NULL; +} + +JNIEXPORT jint JNICALL +Java_com_jogamp_common_nio_Buffers_strnlenImpl(JNIEnv *env, jclass _unused, jlong jcstrptr, jint jmaxlen) { + return ( 0 != jcstrptr && 0 < jmaxlen ) ? strnlen((const char *)(void *)(intptr_t)jcstrptr, jmaxlen) : 0; +} + +JNIEXPORT jlong JNICALL +Java_com_jogamp_common_nio_Buffers_memcpyImpl(JNIEnv *env, jclass _unused, jlong jdest, jlong jsrc, jlong jlen) { + return ( 0 != jdest && 0 != jsrc && 0 < jlen ) ? memcpy((void *)(intptr_t)jdest, (void *)(intptr_t)jsrc, (size_t)jlen) : jdest; +} + diff --git a/src/native/common/PointerBuffer.c b/src/native/common/PointerBuffer.c deleted file mode 100644 index f3e25d3..0000000 --- a/src/native/common/PointerBuffer.c +++ /dev/null @@ -1,14 +0,0 @@ - -#include - -#include - -#include - -#include "com_jogamp_common_nio_PointerBuffer.h" - -JNIEXPORT jlong JNICALL -Java_com_jogamp_common_nio_PointerBuffer_getDirectBufferAddressImpl(JNIEnv *env, jclass _unused, jobject directBuffer) { - return ( NULL != directBuffer ) ? (jlong) (intptr_t) (*env)->GetDirectBufferAddress(env, directBuffer) : 0L ; -} - -- cgit v1.2.3