GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
BufferFactoryInternal.java
Go to the documentation of this file.
1/*
2
3 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40
41package com.jogamp.gluegen.runtime;
42
43import java.lang.reflect.Constructor;
44import java.lang.reflect.Field;
45import java.nio.*;
46import sun.misc.Unsafe;
47
49 private static final long addressFieldOffset;
50 private static final Constructor directByteBufferConstructor;
51
52 static {
53 try {
54 Field f = Buffer.class.getDeclaredField("address");
55 addressFieldOffset = UnsafeAccess.getUnsafe().objectFieldOffset(f);
56
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);
62 }
63 }
64
65 public static long getDirectBufferAddress(Buffer buf) {
66 return ((buf == null) ? 0 : UnsafeAccess.getUnsafe().getLong(buf, addressFieldOffset));
67 }
68
69 public static ByteBuffer newDirectByteBuffer(long address, int capacity) {
70 try {
71 if (address == 0) {
72 return null;
73 }
74 return (ByteBuffer) directByteBufferConstructor.newInstance(new Object[] { new Long(address), new Integer(capacity) });
75 } catch (Exception e) {
76 throw new RuntimeException(e);
77 }
78 }
79
80 public static long newCString(String str) {
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]);
85 }
86 UnsafeAccess.getUnsafe().putByte(strBlock+strBytes.length, (byte)0); // null termination
87 return strBlock;
88 }
89
90 public static void freeCString(long cStr) {
91 UnsafeAccess.getUnsafe().freeMemory(cStr);
92 }
93
94 public static String newJavaString(long cStr) {
95 if (cStr == 0) {
96 return null;
97 }
98 int numChars = 0;
99 while (UnsafeAccess.getUnsafe().getByte(cStr + numChars) != 0) {
100 ++numChars;
101 }
102 byte[] bytes = new byte[numChars];
103 for (int i = 0; i < numChars; i++) {
104 bytes[i] = UnsafeAccess.getUnsafe().getByte(cStr + i);
105 }
106 return new String(bytes);
107 }
108
109 public static int arrayBaseOffset(Object array) {
110 return UnsafeAccess.getUnsafe().arrayBaseOffset(array.getClass());
111 }
112 public static int arrayIndexScale(Object array) {
113 return UnsafeAccess.getUnsafe().arrayIndexScale(array.getClass());
114 }
115}
static ByteBuffer newDirectByteBuffer(long address, int capacity)