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/java/com/jogamp/common/nio/NativeBuffer.java | 36 +++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'src/java/com/jogamp/common/nio/NativeBuffer.java') diff --git a/src/java/com/jogamp/common/nio/NativeBuffer.java b/src/java/com/jogamp/common/nio/NativeBuffer.java index d81da9d..5ee6af8 100644 --- a/src/java/com/jogamp/common/nio/NativeBuffer.java +++ b/src/java/com/jogamp/common/nio/NativeBuffer.java @@ -1,5 +1,5 @@ /** - * Copyright 2010 JogAmp Community. All rights reserved. + * Copyright 2010-2023 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: @@ -32,6 +32,7 @@ package com.jogamp.common.nio; import java.nio.Buffer; +import java.nio.ByteBuffer; /** * Hardware independent container for various kinds of buffers. @@ -76,19 +77,34 @@ public interface NativeBuffer { */ public Object array() throws UnsupportedOperationException ; + /** Returns the underlying buffer object. */ public Buffer getBuffer(); - + /** Return true if the underlying buffer is NIO direct, otherwise false. */ public boolean isDirect(); + /** Returns the native address of the underlying buffer if {@link #isDirect()}, otherwise {@code 0}. */ + public long getDirectBufferAddress(); + /** + * Store the {@link #getDirectBufferAddress()} into the given {@link ByteBuffer} using relative put. + *

+ * The native pointer value is stored either as a 32bit (int) or 64bit (long) wide value, + * depending of the CPU pointer width. + *

+ */ + public void storeDirectAddress(final ByteBuffer directDest); + /** + * Store the {@link #getDirectBufferAddress()} into the given {@link ByteBuffer} using absolute put. + *

+ * The native pointer value is stored either as a 32bit (int) or 64bit (long) wide value, + * depending of the CPU pointer width. + *

+ **/ + public void storeDirectAddress(final ByteBuffer directDest, final int destOffset); public B rewind(); - public B put(int index, long value); - - public B put(long value); - + /** + * Relative bulk get method. Copy the source values src[position .. capacity] [ + * to this buffer and increment the position by capacity-position. + */ public B put(B src); - - public long get(); - - public long get(int idx); } -- cgit v1.2.3