26 package sun.awt.X11;
27
28 import sun.misc.Unsafe;
29 import java.util.Vector;
30 import java.security.AccessController;
31 import java.security.PrivilegedAction;
32
33 /**
34 * This class contains the collection of utility functions to help work with
35 * native data types on different platforms similarly.
36 */
37
38 class Native {
39
40 private static Unsafe unsafe = XlibWrapper.unsafe;
41
42 static int longSize;
43
44 static int dataModel;
45 static {
46 String dataModelProp = (String)AccessController.
47 doPrivileged(
48 new PrivilegedAction() {
49 public Object run() {
50 return System.getProperty("sun.arch.data.model");
51 }
52 });
53 try {
54 dataModel = Integer.parseInt(dataModelProp);
55 } catch (Exception e) {
56 dataModel = 32;
57 }
58 if (dataModel == 32) {
59 longSize = 4;
60 } else {
61 longSize = 8;
62 }
63 }
64
65 /**
66 * Set of helper function to read data of different PLATFORM types
67 * from memory pointer by <code>ptr</code>
68 * Note, names of types in function are NATIVE PLATFORM types
69 * and they have the same size as they would have in C compiler
316 /**
317 * Returns index's element of the array of native long pointed by ptr
318 */
319 static long getLong(long ptr, int index) {
320 return getLong(ptr + index*getLongSize());
321 }
322 /**
323 * Stores Java long[] array into memory. Memory location is treated as array
324 * of native <code>long</code>s
325 */
326 static void put(long ptr, long[] arr) {
327 for (int i = 0; i < arr.length; i ++, ptr += getLongSize()) {
328 putLong(ptr, arr[i]);
329 }
330 }
331
332 /**
333 * Stores Java Vector of Longs into memory. Memory location is treated as array
334 * of native <code>long</code>s
335 */
336 static void putLong(long ptr, Vector arr) {
337 for (int i = 0; i < arr.size(); i ++, ptr += getLongSize()) {
338 putLong(ptr, ((Long)arr.elementAt(i)).longValue());
339 }
340 }
341
342 /**
343 * Stores Java Vector of Longs into memory. Memory location is treated as array
344 * of native <code>long</code>s. Array is stored in reverse order
345 */
346 static void putLongReverse(long ptr, Vector arr) {
347 for (int i = arr.size()-1; i >= 0; i--, ptr += getLongSize()) {
348 putLong(ptr, ((Long)arr.elementAt(i)).longValue());
349 }
350 }
351 /**
352 * Converts length bytes of data pointed by <code>data</code> into byte array
353 * Returns null if data is zero
354 * @param data native pointer to native memory
355 * @param length size in longs(platform dependent) of native memory
356 */
357 static long[] toLongs(long data, int length) {
358 if (data == 0) {
359 return null;
360 }
361 long[] res = new long[length];
362 for (int i = 0; i < length; i++, data += getLongSize()) {
363 res[i] = getLong(data);
364 }
365 return res;
366 }
367 static long toData(long[] longs) {
368 if (longs == null) {
|
26 package sun.awt.X11;
27
28 import sun.misc.Unsafe;
29 import java.util.Vector;
30 import java.security.AccessController;
31 import java.security.PrivilegedAction;
32
33 /**
34 * This class contains the collection of utility functions to help work with
35 * native data types on different platforms similarly.
36 */
37
38 class Native {
39
40 private static Unsafe unsafe = XlibWrapper.unsafe;
41
42 static int longSize;
43
44 static int dataModel;
45 static {
46 String dataModelProp = AccessController.doPrivileged(
47 new PrivilegedAction<String>() {
48 public String run() {
49 return System.getProperty("sun.arch.data.model");
50 }
51 });
52 try {
53 dataModel = Integer.parseInt(dataModelProp);
54 } catch (Exception e) {
55 dataModel = 32;
56 }
57 if (dataModel == 32) {
58 longSize = 4;
59 } else {
60 longSize = 8;
61 }
62 }
63
64 /**
65 * Set of helper function to read data of different PLATFORM types
66 * from memory pointer by <code>ptr</code>
67 * Note, names of types in function are NATIVE PLATFORM types
68 * and they have the same size as they would have in C compiler
315 /**
316 * Returns index's element of the array of native long pointed by ptr
317 */
318 static long getLong(long ptr, int index) {
319 return getLong(ptr + index*getLongSize());
320 }
321 /**
322 * Stores Java long[] array into memory. Memory location is treated as array
323 * of native <code>long</code>s
324 */
325 static void put(long ptr, long[] arr) {
326 for (int i = 0; i < arr.length; i ++, ptr += getLongSize()) {
327 putLong(ptr, arr[i]);
328 }
329 }
330
331 /**
332 * Stores Java Vector of Longs into memory. Memory location is treated as array
333 * of native <code>long</code>s
334 */
335 static void putLong(long ptr, Vector<Long> arr) {
336 for (int i = 0; i < arr.size(); i ++, ptr += getLongSize()) {
337 putLong(ptr, arr.elementAt(i).longValue());
338 }
339 }
340
341 /**
342 * Stores Java Vector of Longs into memory. Memory location is treated as array
343 * of native <code>long</code>s. Array is stored in reverse order
344 */
345 static void putLongReverse(long ptr, Vector<Long> arr) {
346 for (int i = arr.size()-1; i >= 0; i--, ptr += getLongSize()) {
347 putLong(ptr, arr.elementAt(i).longValue());
348 }
349 }
350 /**
351 * Converts length bytes of data pointed by <code>data</code> into byte array
352 * Returns null if data is zero
353 * @param data native pointer to native memory
354 * @param length size in longs(platform dependent) of native memory
355 */
356 static long[] toLongs(long data, int length) {
357 if (data == 0) {
358 return null;
359 }
360 long[] res = new long[length];
361 for (int i = 0; i < length; i++, data += getLongSize()) {
362 res[i] = getLong(data);
363 }
364 return res;
365 }
366 static long toData(long[] longs) {
367 if (longs == null) {
|