41package com.jogamp.gluegen.runtime;
43import java.lang.reflect.Constructor;
44import java.lang.reflect.Field;
46import sun.misc.Unsafe;
49 private static final long addressFieldOffset;
50 private static final Constructor directByteBufferConstructor;
54 Field f = Buffer.class.getDeclaredField(
"address");
55 addressFieldOffset = UnsafeAccess.getUnsafe().objectFieldOffset(f);
57 Class directByteBufferClass = Class.forName(
"java.nio.DirectByteBuffer");
58 directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(
new Class[] { Long.TYPE, Integer.TYPE });
59 directByteBufferConstructor.setAccessible(
true);
60 }
catch (Exception e) {
61 throw new RuntimeException(e);
66 return ((buf ==
null) ? 0 : UnsafeAccess.getUnsafe().getLong(buf, addressFieldOffset));
74 return (ByteBuffer) directByteBufferConstructor.newInstance(
new Object[] {
new Long(address),
new Integer(capacity) });
75 }
catch (Exception e) {
76 throw new RuntimeException(e);
81 byte[] strBytes = str.getBytes();
82 long strBlock = UnsafeAccess.getUnsafe().allocateMemory(strBytes.length+1);
83 for (
int i = 0; i < strBytes.length; i++) {
84 UnsafeAccess.getUnsafe().putByte(strBlock+i, strBytes[i]);
86 UnsafeAccess.getUnsafe().putByte(strBlock+strBytes.length, (
byte)0);
91 UnsafeAccess.getUnsafe().freeMemory(cStr);
99 while (UnsafeAccess.getUnsafe().getByte(cStr + numChars) != 0) {
102 byte[] bytes =
new byte[numChars];
103 for (
int i = 0; i < numChars; i++) {
104 bytes[i] = UnsafeAccess.getUnsafe().getByte(cStr + i);
106 return new String(bytes);
110 return UnsafeAccess.getUnsafe().arrayBaseOffset(array.getClass());
113 return UnsafeAccess.getUnsafe().arrayIndexScale(array.getClass());
static String newJavaString(long cStr)
static ByteBuffer newDirectByteBuffer(long address, int capacity)
static void freeCString(long cStr)
static long getDirectBufferAddress(Buffer buf)
static long newCString(String str)
static int arrayBaseOffset(Object array)
static int arrayIndexScale(Object array)