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
69 * on the same platform.
70 */
71
72 static boolean getBool(long ptr) { return getInt(ptr) != 0; }
73 static boolean getBool(long ptr, int index) { return getInt(ptr, index) != 0; }
74 static void putBool(long ptr, boolean data) { putInt(ptr, (data)?(1):(0)); }
75 static void putBool(long ptr, int index, boolean data) { putInt(ptr, index, (data)?(1):(0)); }
76
77
78 /**
79 * Access to C byte data(one byte)
80 */
81 static int getByteSize() { return 1; }
82 static byte getByte(long ptr) { return unsafe.getByte(ptr); }
83
84 static byte getByte(long ptr, int index) {
85 return getByte(ptr+index);
86 }
87 /**
88 * Stores to C byte data(one byte)
89 */
90 static void putByte(long ptr, byte data) { unsafe.putByte(ptr, data); }
91
92 static void putByte(long ptr, int index, byte data) {
93 putByte(ptr+index, data);
94 }
95 /**
96 * Converts length bytes of data pointed by <code>data</code> into byte array
97 * Returns null if data is zero
98 * @param data native pointer to native memory
99 * @param length size in bytes of native memory
100 */
101 static byte[] toBytes(long data, int length) {
102 if (data == 0) {
103 return null;
104 }
105 byte[] res = new byte[length];
106 for (int i = 0; i < length; i++, data++) {
107 res[i] = getByte(data);
108 }
109 return res;
110 }
111 /**
112 * Stores byte array into native memory and returns pointer to this memory
113 * Returns 0 if bytes is null
114 */
115 static long toData(byte[] bytes) {
116 if (bytes == null) {
126 /**
127 * Access to C unsigned byte data(one byte)
128 */
129 static int getUByteSize() { return 1; }
130 static short getUByte(long ptr) { return (short)(0xFF & unsafe.getByte(ptr)); }
131
132 static short getUByte(long ptr, int index) {
133 return getUByte(ptr+index);
134 }
135
136 /**
137 * Stores to C unsigned byte data(one byte)
138 */
139 static void putUByte(long ptr, short data) { unsafe.putByte(ptr, (byte)data); }
140
141 static void putUByte(long ptr, int index, short data) {
142 putUByte(ptr+index, data);
143 }
144
145 /**
146 * Converts length usnigned bytes of data pointed by <code>data</code> into
147 * short array
148 * Returns null if data is zero
149 * @param data native pointer to native memory
150 * @param length size in bytes of native memory
151 */
152 static short[] toUBytes(long data, int length) {
153 if (data == 0) {
154 return null;
155 }
156 short[] res = new short[length];
157 for (int i = 0; i < length; i++, data++) {
158 res[i] = getUByte(data);
159 }
160 return res;
161 }
162 /**
163 * Stores short array as unsigned bytes into native memory and returns pointer
164 * to this memory
165 * Returns 0 if bytes is null
166 */
280 putUInt(res, i, ints[i]);
281 }
282 return res;
283 }
284
285 /**
286 * Access to C long data(size depends on platform)
287 */
288 static int getLongSize() {
289 return longSize;
290 }
291 static long getLong(long ptr) {
292 if (XlibWrapper.dataModel == 32) {
293 return unsafe.getInt(ptr);
294 } else {
295 return unsafe.getLong(ptr);
296 }
297 }
298 /**
299 * Stores to C long data(four bytes)
300 * Note: <code>data</code> has <code>long</code> type
301 * to be able to keep 64-bit C <code>long</code> data
302 */
303 static void putLong(long ptr, long data) {
304 if (XlibWrapper.dataModel == 32) {
305 unsafe.putInt(ptr, (int)data);
306 } else {
307 unsafe.putLong(ptr, data);
308 }
309 }
310
311 static void putLong(long ptr, int index, long data) {
312 putLong(ptr+index*getLongSize(), data);
313 }
314
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) {
368 return 0;
369 }
370 long res = XlibWrapper.unsafe.allocateMemory(longs.length*getLongSize());
371 for (int i = 0; i < longs.length; i++) {
376
377
378 /**
379 * Access to C "unsigned long" date type, which is XID in X
380 */
381 static long getULong(long ptr) {
382 if (XlibWrapper.dataModel == 32) {
383 // Compensate sign-expansion
384 return ((long)unsafe.getInt(ptr)) & 0xFFFFFFFFL;
385 } else {
386 // Can't do anything!!!
387 return unsafe.getLong(ptr);
388 }
389 }
390
391 static void putULong(long ptr, long value) {
392 putLong(ptr, value);
393 }
394
395 /**
396 * Allocates memory for array of native <code>long</code>s of the size <code>length</code>
397 */
398 static long allocateLongArray(int length) {
399 return unsafe.allocateMemory(getLongSize() * length);
400 }
401
402
403 static long getWindow(long ptr) {
404 return getLong(ptr);
405 }
406 static long getWindow(long ptr, int index) {
407 return getLong(ptr + getWindowSize()*index);
408 }
409
410 static void putWindow(long ptr, long window) {
411 putLong(ptr, window);
412 }
413
414 static void putWindow(long ptr, int index, long window) {
415 putLong(ptr, index, window);
416 }
|
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}
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
69 * on the same platform.
70 */
71
72 static boolean getBool(long ptr) { return getInt(ptr) != 0; }
73 static boolean getBool(long ptr, int index) { return getInt(ptr, index) != 0; }
74 static void putBool(long ptr, boolean data) { putInt(ptr, (data)?(1):(0)); }
75 static void putBool(long ptr, int index, boolean data) { putInt(ptr, index, (data)?(1):(0)); }
76
77
78 /**
79 * Access to C byte data(one byte)
80 */
81 static int getByteSize() { return 1; }
82 static byte getByte(long ptr) { return unsafe.getByte(ptr); }
83
84 static byte getByte(long ptr, int index) {
85 return getByte(ptr+index);
86 }
87 /**
88 * Stores to C byte data(one byte)
89 */
90 static void putByte(long ptr, byte data) { unsafe.putByte(ptr, data); }
91
92 static void putByte(long ptr, int index, byte data) {
93 putByte(ptr+index, data);
94 }
95 /**
96 * Converts length bytes of data pointed by {@code data} into byte array
97 * Returns null if data is zero
98 * @param data native pointer to native memory
99 * @param length size in bytes of native memory
100 */
101 static byte[] toBytes(long data, int length) {
102 if (data == 0) {
103 return null;
104 }
105 byte[] res = new byte[length];
106 for (int i = 0; i < length; i++, data++) {
107 res[i] = getByte(data);
108 }
109 return res;
110 }
111 /**
112 * Stores byte array into native memory and returns pointer to this memory
113 * Returns 0 if bytes is null
114 */
115 static long toData(byte[] bytes) {
116 if (bytes == null) {
126 /**
127 * Access to C unsigned byte data(one byte)
128 */
129 static int getUByteSize() { return 1; }
130 static short getUByte(long ptr) { return (short)(0xFF & unsafe.getByte(ptr)); }
131
132 static short getUByte(long ptr, int index) {
133 return getUByte(ptr+index);
134 }
135
136 /**
137 * Stores to C unsigned byte data(one byte)
138 */
139 static void putUByte(long ptr, short data) { unsafe.putByte(ptr, (byte)data); }
140
141 static void putUByte(long ptr, int index, short data) {
142 putUByte(ptr+index, data);
143 }
144
145 /**
146 * Converts length usnigned bytes of data pointed by {@code data} into
147 * short array
148 * Returns null if data is zero
149 * @param data native pointer to native memory
150 * @param length size in bytes of native memory
151 */
152 static short[] toUBytes(long data, int length) {
153 if (data == 0) {
154 return null;
155 }
156 short[] res = new short[length];
157 for (int i = 0; i < length; i++, data++) {
158 res[i] = getUByte(data);
159 }
160 return res;
161 }
162 /**
163 * Stores short array as unsigned bytes into native memory and returns pointer
164 * to this memory
165 * Returns 0 if bytes is null
166 */
280 putUInt(res, i, ints[i]);
281 }
282 return res;
283 }
284
285 /**
286 * Access to C long data(size depends on platform)
287 */
288 static int getLongSize() {
289 return longSize;
290 }
291 static long getLong(long ptr) {
292 if (XlibWrapper.dataModel == 32) {
293 return unsafe.getInt(ptr);
294 } else {
295 return unsafe.getLong(ptr);
296 }
297 }
298 /**
299 * Stores to C long data(four bytes)
300 * Note: {@code data} has {@code long} type
301 * to be able to keep 64-bit C {@code long} data
302 */
303 static void putLong(long ptr, long data) {
304 if (XlibWrapper.dataModel == 32) {
305 unsafe.putInt(ptr, (int)data);
306 } else {
307 unsafe.putLong(ptr, data);
308 }
309 }
310
311 static void putLong(long ptr, int index, long data) {
312 putLong(ptr+index*getLongSize(), data);
313 }
314
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}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}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}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} 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) {
368 return 0;
369 }
370 long res = XlibWrapper.unsafe.allocateMemory(longs.length*getLongSize());
371 for (int i = 0; i < longs.length; i++) {
376
377
378 /**
379 * Access to C "unsigned long" date type, which is XID in X
380 */
381 static long getULong(long ptr) {
382 if (XlibWrapper.dataModel == 32) {
383 // Compensate sign-expansion
384 return ((long)unsafe.getInt(ptr)) & 0xFFFFFFFFL;
385 } else {
386 // Can't do anything!!!
387 return unsafe.getLong(ptr);
388 }
389 }
390
391 static void putULong(long ptr, long value) {
392 putLong(ptr, value);
393 }
394
395 /**
396 * Allocates memory for array of native {@code long}s of the size {@code length}
397 */
398 static long allocateLongArray(int length) {
399 return unsafe.allocateMemory(getLongSize() * length);
400 }
401
402
403 static long getWindow(long ptr) {
404 return getLong(ptr);
405 }
406 static long getWindow(long ptr, int index) {
407 return getLong(ptr + getWindowSize()*index);
408 }
409
410 static void putWindow(long ptr, long window) {
411 putLong(ptr, window);
412 }
413
414 static void putWindow(long ptr, int index, long window) {
415 putLong(ptr, index, window);
416 }
|