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/AbstractBuffer.java | 49 ++++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'src/java/com/jogamp/common/nio/AbstractBuffer.java') diff --git a/src/java/com/jogamp/common/nio/AbstractBuffer.java b/src/java/com/jogamp/common/nio/AbstractBuffer.java index a774c39..b31929b 100644 --- a/src/java/com/jogamp/common/nio/AbstractBuffer.java +++ b/src/java/com/jogamp/common/nio/AbstractBuffer.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: @@ -34,6 +34,9 @@ package com.jogamp.common.nio; import com.jogamp.common.os.*; import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; /** * @author Sven Gothel @@ -41,6 +44,8 @@ import java.nio.Buffer; */ @SuppressWarnings("rawtypes") public abstract class AbstractBuffer implements NativeBuffer { + /** Platform dependent pointer size in bytes, i.e. 32bit or 64bit wide, depending of the CPU pointer width. */ + public static final int POINTER_SIZE; protected final Buffer buffer; protected final int elementSize; @@ -49,6 +54,7 @@ public abstract class AbstractBuffer implements Native static { Platform.initSingleton(); // loads native gluegen_rt library + POINTER_SIZE = Platform.is32Bit() ? Buffers.SIZEOF_INT : Buffers.SIZEOF_LONG ; } /** @@ -118,11 +124,44 @@ public abstract class AbstractBuffer implements Native public final Buffer getBuffer() { return buffer; } - @Override public final boolean isDirect() { return buffer.isDirect(); } + @Override + public long getDirectBufferAddress() { + if( isDirect() ) { + return Buffers.getDirectBufferAddressImpl(buffer); + } else { + return 0; + } + } + @Override + public void storeDirectAddress(final ByteBuffer directDest) { + final long addr = getDirectBufferAddress(); + switch(POINTER_SIZE) { + case 4: + directDest.putInt(0, (int) ( addr & 0x00000000FFFFFFFFL ) ); + break; + case 8: + directDest.putLong(0, addr); + break; + } + directDest.position(directDest.position()+POINTER_SIZE); + } + + @Override + public void storeDirectAddress(final ByteBuffer directDest, final int destBytePos) { + final long addr = getDirectBufferAddress(); + switch(POINTER_SIZE) { + case 4: + directDest.putInt(destBytePos, (int) ( addr & 0x00000000FFFFFFFFL ) ); + break; + case 8: + directDest.putLong(destBytePos, addr); + break; + } + } @Override public final boolean hasArray() { @@ -143,9 +182,11 @@ public abstract class AbstractBuffer implements Native return buffer.array(); } + protected String toSubString() { + return "[direct["+isDirect()+", addr 0x"+Long.toHexString(getDirectBufferAddress())+"], hasArray "+hasArray()+", capacity "+capacity+", position "+position+", elementSize "+elementSize+", buffer[capacity "+buffer.capacity()+", lim "+buffer.limit()+", pos "+buffer.position()+"]]"; + } @Override public String toString() { - return "AbstractBuffer[direct "+isDirect()+", hasArray "+hasArray()+", capacity "+capacity+", position "+position+", elementSize "+elementSize+", buffer[capacity "+buffer.capacity()+", lim "+buffer.limit()+", pos "+buffer.position()+"]]"; + return "AbstractBuffer"+toSubString(); } - } -- cgit v1.2.3