001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.misc.accessors;
019:
020: import org.apache.harmony.misc.internal.nls.Messages;
021:
022: /**
023: * The class describes low level memory operation for memory allocation/deallocation
024: * and access.
025: * <p>Type <code>long</code> is used for memory address representation on all platforms.
026: * On 32 bit platforms only low 32 address bits are used,
027: * high bits can have arbitrary value.
028: */
029: public class MemoryAccessor {
030:
031: private static MemoryAccessor instance;
032:
033: static MemoryAccessor getInstance() {
034: if (instance == null) {
035: System.loadLibrary("accessors"); //$NON-NLS-1$
036: instance = new MemoryAccessor();
037: }
038: return instance;
039: }
040:
041: private MemoryAccessor() {
042: }
043:
044: /**
045: * Means high order bytes in larger than byte type memory
046: * representation will have higher addresses in memory than low order bytes.
047: */
048: public static final int BIG_ENDIAN = 0;
049:
050: /**
051: * Means high order bytes in larger than byte type memory
052: * representation will have lower addresses in memory than low order bytes.
053: */
054: public static final int LITTLE_ENDIAN = 1;
055:
056: /**
057: * The cached value of the native byte order used by the current platform.
058: */
059: private final int nativeByteOrder = getNativeByteOrder0();
060:
061: /**
062: * The cached value of the native pointer size of the current platform.
063: */
064: private final int pointerSize = Malloc.getPointerSize();
065:
066: /**
067: * The cached value of the C long size of the current platform.
068: */
069: private final int cLongSize = Malloc.getCLongSize();
070:
071: /**
072: * Finds the offset of the first different element in two
073: * memory blocks. If no difference the size is returned. If reorder is set
074: * changes the byte order of first block elements before comparison.
075: * @param addr1 start address of the first block
076: * @param addr2 start address of the second block
077: * @param elemSize size of the element can be 1, 2, 4, 8
078: * @param size number of elements to compare
079: * @param reorder blocks are in different byte order
080: * @return index of the first different element
081: */
082: public final long findFirstDiff(long addr1, long addr2,
083: int elemSize, long size, boolean reorder) {
084: if (elemSize == 1 || !reorder) {
085: return findFirstDiff(addr1, addr2, elemSize * size)
086: / elemSize;
087: }
088: switch (elemSize) {
089: case 2:
090: return findFirstDiffReorder16(addr1, addr2, size);
091: case 4:
092: return findFirstDiffReorder32(addr1, addr2, size);
093: case 8:
094: return findFirstDiffReorder64(addr1, addr2, size);
095: }
096: // misc.0=bad elemSize
097: throw new IllegalArgumentException(Messages.getString("misc.0")); //$NON-NLS-1$
098: }
099:
100: private native long findFirstDiff(long addr1, long addr2, long size);
101:
102: private native long findFirstDiffReorder16(long addr1, long addr2,
103: long size);
104:
105: private native long findFirstDiffReorder32(long addr1, long addr2,
106: long size);
107:
108: private native long findFirstDiffReorder64(long addr1, long addr2,
109: long size);
110:
111: /**
112: * Releases memory block previously allocated with malloc or realloc.
113: * @param addr start address of block
114: */
115:
116: public final void free(long addr) {
117: Malloc.free(addr);
118: }
119:
120: private native void getArray(long addr, Object array, long offset,
121: long length);
122:
123: private native void getArrayReorder16(long addr, Object array,
124: long offset, long length);
125:
126: private native void getArrayReorder32(long addr, Object array,
127: long offset, long length);
128:
129: private native void getArrayReorder64(long addr, Object array,
130: long offset, long length);
131:
132: /**
133: * Reads a boolean value from memory address.
134: * @param addr memory address
135: * @return boolean value
136: */
137: public final native boolean getBoolean(long addr);
138:
139: /**
140: * Reads data from memory address in platform byte order into boolean array.
141: * @param addr data start address in memory
142: * @param value array to save data
143: * @param offset initial offset in java array
144: * @param length number of array elements to read
145: */
146: public final void getBoolean(long addr, boolean[] value,
147: int offset, int length) {
148: getArray(addr, value, offset, length);
149: }
150:
151: /**
152: * Reads a byte value from memory address.
153: * @param addr memory address
154: * @return byte value
155: */
156: public final native byte getByte(long addr);
157:
158: /**
159: * Reads data from memory address in platform byte order into byte array.
160: * @param addr data start address in memory
161: * @param value array to save data
162: * @param offset initial offset in java array
163: * @param length number of array elements to read
164: */
165: public final void getByte(long addr, byte[] value, int offset,
166: int length) {
167: getArray(addr, value, offset, length);
168: }
169:
170: /**
171: * Reads a char value from memory address in a platform byte order.
172: * @param addr memory address
173: * @return memory value
174: */
175: public final char getChar(long addr) {
176: return (char) getShort(addr);
177: }
178:
179: /**
180: * Reads data from memory address in a platform byte order into char array.
181: * @param addr data start address in memory
182: * @param value array to save data
183: * @param offset initial offset in java array
184: * @param length number of array elements to read
185: */
186: public final void getChar(long addr, char[] value, int offset,
187: int length) {
188: getArray(addr, value, offset << 1, length << 1);
189: }
190:
191: /**
192: * Reads data from memory address in selected order into char array.
193: * @param addr data start address in memory
194: * @param value array to save data
195: * @param offset initial offset in java array
196: * @param length number of array elements to read
197: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
198: */
199: public final void getChar(long addr, char[] value, int offset,
200: int length, int byteOrder) {
201: if (byteOrder == nativeByteOrder) {
202: getArray(addr, value, offset << 1, length << 1);
203: return;
204: }
205: getArrayReorder16(addr, value, offset, length);
206: }
207:
208: /**
209: * Reads a char value from memory address in selected order. *
210: * @param addr memory address
211: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
212: * @return memory value
213: */
214: public final char getChar(long addr, int byteOrder) {
215: if (byteOrder == nativeByteOrder) {
216: return getChar(addr);
217: }
218: return getCharReorder(addr);
219: }
220:
221: private char getCharReorder(long addr) {
222: return (char) getShortReorder(addr);
223: }
224:
225: /**
226: * Reads a double value from memory address in a platform byte order.
227: * @param addr memory address
228: * @return memory value
229: */
230: public final native double getDouble(long addr);
231:
232: /**
233: * Reads data from memory address in platform byte order into array of doubles.
234: * @param addr data start address in memory
235: * @param value array to save data
236: * @param offset initial offset in java array
237: * @param length number of array elements to read
238: */
239: public final void getDouble(long addr, double[] value, int offset,
240: int length) {
241: getArray(addr, value, offset << 3, length << 3);
242: }
243:
244: /**
245: * Reads data from memory address in selected byte order into array of doubles.
246: * @param addr data start address in memory
247: * @param value array to save data
248: * @param offset initial offset in java array
249: * @param length number of array elements to read
250: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
251: */
252: public final void getDouble(long addr, double[] value, int offset,
253: int length, int byteOrder) {
254: if (byteOrder == nativeByteOrder) {
255: getArray(addr, value, offset << 3, length << 3);
256: return;
257: }
258: getArrayReorder64(addr, value, offset, length);
259: }
260:
261: /**
262: * Reads a double value from memory address in selected byte order.
263: * @param addr memory address
264: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
265: * @return memory value
266: */
267: public final double getDouble(long addr, int byteOrder) {
268: if (byteOrder == nativeByteOrder) {
269: return getDouble(addr);
270: }
271: return getDoubleReorder(addr);
272: }
273:
274: private final native double getDoubleReorder(long addr);
275:
276: /**
277: * Reads a float value from memory address in platform byte order.
278: * @param addr memory address
279: * @return memory value
280: */
281: public final native float getFloat(long addr);
282:
283: /**
284: * Reads data from memory address in platform byte order into float array.
285: * @param addr data start address in memory
286: * @param value array to save data
287: * @param offset initial offset in java array
288: * @param length number of array elements to read
289: */
290:
291: public final void getFloat(long addr, float[] value, int offset,
292: int length) {
293: getArray(addr, value, offset << 2, length << 2);
294: }
295:
296: /**
297: * Reads data from memory address in selected byte order into float array.
298: * @param addr data start address in memory
299: * @param value array to save data
300: * @param offset initial offset in java array
301: * @param length number of array elements to read
302: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
303: */
304: public final void getFloat(long addr, float[] value, int offset,
305: int length, int byteOrder) {
306: if (byteOrder == nativeByteOrder) {
307: getArray(addr, value, offset << 2, length << 2);
308: return;
309: }
310: getArrayReorder32(addr, value, offset, length);
311: }
312:
313: /**
314: * Reads a float value from memory address in selected byte order.
315: * @param addr memory address
316: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
317: * @return memory value
318: */
319: public final float getFloat(long addr, int byteOrder) {
320: if (byteOrder == nativeByteOrder) {
321: return getFloat(addr);
322: }
323: return getFloatReorder(addr);
324: }
325:
326: private final native float getFloatReorder(long addr);
327:
328: /**
329: * Calculates the hash function of the block of bytes
330: * @param addr memory address
331: * @param size number of bytes to process
332: * @return hash function value
333: */
334: public final native int getHashCode(long addr, long size);
335:
336: /**
337: * Reads an integer value from memory address in platform byte order.
338: * @param addr memory address
339: * @return memory value
340: */
341: public final native int getInt(long addr);
342:
343: /**
344: * Reads an integer value from memory address in selected byte order.
345: * @param addr memory address
346: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
347: * @return memory value
348: */
349: public final int getInt(long addr, int byteOrder) {
350: if (byteOrder == nativeByteOrder) {
351: return getInt(addr);
352: }
353: return getIntReorder(addr);
354: }
355:
356: /**
357: * Reads data from memory address in platform byte order into array of integers.
358: * @param addr data start address in memory
359: * @param value array to save data
360: * @param offset initial offset in java array
361: * @param length number of array elements to read
362: */
363: public final void getInt(long addr, int[] value, int offset,
364: int length) {
365: getArray(addr, value, offset << 2, length << 2);
366: }
367:
368: /**
369: * Reads data from memory address in selected byte order into array of integers.
370: * @param addr data start address in memory
371: * @param value array to save data
372: * @param offset initial offset in java array
373: * @param length number of array elements to read
374: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
375: */
376: public final void getInt(long addr, int[] value, int offset,
377: int length, int byteOrder) {
378: if (byteOrder == nativeByteOrder) {
379: getArray(addr, value, offset << 2, length << 2);
380: return;
381: }
382: getArrayReorder32(addr, value, offset, length);
383: }
384:
385: private final native int getIntReorder(long addr);
386:
387: /**
388: * Reads a long value from memory address in platform byte order.
389: * @param addr memory address
390: * @return memory value
391: */
392: public final native long getLong(long addr);
393:
394: /**
395: * Reads a long value from memory address in selected byte order.
396: * @param addr memory address
397: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
398: * @return memory value
399: */
400: public final long getLong(long addr, int byteOrder) {
401: if (byteOrder == nativeByteOrder) {
402: return getLong(addr);
403: }
404: return getLongReorder(addr);
405: }
406:
407: /**
408: * Reads data from memory address in platform byte order into array of longs.
409: * @param addr data start address in memory
410: * @param value array to save data
411: * @param offset initial offset in java array
412: * @param length number of array elements to read
413: */
414: public final void getLong(long addr, long[] value, int offset,
415: int length) {
416: getArray(addr, value, offset << 3, length << 3);
417: }
418:
419: /**
420: * Reads data from memory address in selected byte order into array of longs.
421: * @param addr data start address in memory
422: * @param value array to save data
423: * @param offset initial offset in java array
424: * @param length number of array elements to read
425: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
426: */
427: public final void getLong(long addr, long[] value, int offset,
428: int length, int byteOrder) {
429: if (byteOrder == nativeByteOrder) {
430: getArray(addr, value, offset << 3, length << 3);
431: return;
432: }
433: getArrayReorder64(addr, value, offset, length);
434: }
435:
436: private final native long getLongReorder(long addr);
437:
438: private native final int getNativeByteOrder0();
439:
440: /**
441: * Reads a short value from memory address in platform byte order.
442: * @param addr memory address
443: * @return memory value
444: */
445: public final native short getShort(long addr);
446:
447: /**
448: * Reads a short value from memory address in selected byte order.
449: * @param addr memory address
450: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
451: * @return memory value
452: */
453: public final short getShort(long addr, int byteOrder) {
454: if (byteOrder == nativeByteOrder) {
455: return getShort(addr);
456: }
457: return getShortReorder(addr);
458: }
459:
460: /**
461: * Reads data from memory address in platform byte order into short array.
462: * @param addr data start address in memory
463: * @param value array to save data
464: * @param offset initial offset in java array
465: * @param length number of array elements to read
466: */
467: public final void getShort(long addr, short[] value, int offset,
468: int length) {
469: getArray(addr, value, offset << 1, length << 1);
470: }
471:
472: /**
473: * Reads data from memory address in selected byte order into short array.
474: * @param addr data start address in memory
475: * @param value array to save data
476: * @param offset initial offset in java array
477: * @param length number of array elements to read
478: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
479: */
480: public final void getShort(long addr, short[] value, int offset,
481: int length, int byteOrder) {
482: if (byteOrder == nativeByteOrder) {
483: getArray(addr, value, offset << 1, length << 1);
484: return;
485: }
486: getArrayReorder16(addr, value, offset, length);
487: }
488:
489: private native short getShortReorder(long addr);
490:
491: /**
492: * Allocates memory block in the native heap.
493: * @param size size of block in bytes
494: * @return start address of allocated block
495: */
496: public final long malloc(long size) {
497: return Malloc.malloc(size);
498: }
499:
500: /**
501: * Compares two memory blocks.
502: * @param addr0 start address of block 0
503: * @param addr1 start address of block 1
504: * @param size size in bytes
505: * @return 0 if equal byte by byte, < 0 if block 0 less than block 1,
506: * > 0 if block 0 greater than block 1.
507: */
508: public final int memcmp(long addr0, long addr1, long size) {
509: return Malloc.memcmp(addr0, addr1, size);
510: }
511:
512: /**
513: * Copies size bytes from src to dst, scr and dst can't
514: * overlap.
515: * @param dst start address of destination block
516: * @param src start address of source block
517: * @param size number of bytes to copy
518: * @return dst
519: */
520: public final long memcpy(long dst, long src, long size) {
521: return Malloc.memcpy(dst, src, size);
522: }
523:
524: /**
525: * Copies size bytes from src to dst, scr and dst can
526: * overlap.
527: * @param dst start address of destination block
528: * @param src start address of source block
529: * @param size number of bytes to copy
530: * @return dst
531: */
532: public final long memmove(long dst, long src, long size) {
533: return Malloc.memmove(dst, src, size);
534: }
535:
536: /**
537: * Fills the memory block with fill value.
538: * @param addr start address of block
539: * @param fill fill value
540: * @param size number of bytes to fill
541: * @return addr
542: */
543: public final long memset(long addr, byte fill, long size) {
544: return Malloc.memset(addr, fill, size);
545: }
546:
547: /**
548: * Resizes memory block previously allocated with malloc or realloc.
549: * @param addr start address of block
550: * @param size new size in bytes
551: * @return new start address of block
552: */
553: public final long realloc(long addr, long size) {
554: return Malloc.realloc(addr, size);
555: }
556:
557: private native void setArray(long addr, Object array, long offset,
558: long length);
559:
560: private native void setArrayReorder16(long addr, Object array,
561: long offset, long length);
562:
563: private native void setArrayReorder32(long addr, Object array,
564: long offset, long length);
565:
566: private native void setArrayReorder64(long addr, Object array,
567: long offset, long length);
568:
569: /**
570: * Writes boolean value to memory address.
571: * @param addr memory address
572: * @param value new memory value
573: */
574: public final native void setBoolean(long addr, boolean value);
575:
576: /**
577: * Writes data from boolean array to memory address in platform byte order.
578: * @param addr data start address in memory
579: * @param value data array
580: * @param offset initial offset in java array
581: * @param length number of array elements to write
582: */
583: public final void setBoolean(long addr, boolean[] value,
584: int offset, int length) {
585: setArray(addr, value, offset, length);
586: }
587:
588: /**
589: * Writes byte value to memory address.
590: * @param addr memory address
591: * @param value new memory value
592: */
593: public final native void setByte(long addr, byte value);
594:
595: /**
596: * Writes data from byte array to memory address in platform byte order.
597: * @param addr data start address in memory
598: * @param value data array
599: * @param offset initial offset in java array
600: * @param length number of array elements to write
601: */
602: public final void setByte(long addr, byte[] value, int offset,
603: int length) {
604: setArray(addr, value, offset, length);
605: }
606:
607: /**
608: * Writes char value to memory address in platform byte order.
609: * @param addr memory address
610: * @param value new memory value
611: */
612: public final void setChar(long addr, char value) {
613: setShort(addr, (short) value);
614: }
615:
616: /**
617: * Writes char value to memory address in selected order.
618: * @param addr memory address
619: * @param value new memory value
620: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
621: */
622: public final void setChar(long addr, char value, int byteOrder) {
623: if (byteOrder == nativeByteOrder) {
624: setChar(addr, value);
625: return;
626: }
627: setCharReorder(addr, value);
628: }
629:
630: /**
631: * Writes data from char array to memory address in platform byte order.
632: * @param addr data start address in memory
633: * @param value data array
634: * @param offset initial offset in java array
635: * @param length number of array elements to write
636: */
637: public final void setChar(long addr, char[] value, int offset,
638: int length) {
639: setArray(addr, value, offset << 1, length << 1);
640: }
641:
642: /**
643: * Writes data from char array to memory address in selected byte order.
644: * @param addr data start address in memory
645: * @param value data array
646: * @param offset initial offset in java array
647: * @param length number of array elements to write
648: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
649: */
650: public final void setChar(long addr, char[] value, int offset,
651: int length, int byteOrder) {
652: if (byteOrder == nativeByteOrder) {
653: setArray(addr, value, offset << 1, length << 1);
654: return;
655: }
656: setArrayReorder16(addr, value, offset, length);
657: }
658:
659: private void setCharReorder(long addr, char value) {
660: setShortReorder(addr, (short) value);
661: }
662:
663: /**
664: * Writes a double value to memory address in platform byte order,
665: * @param addr memory address
666: * @param value new memory value
667: */
668: public final native void setDouble(long addr, double value);
669:
670: /**
671: * Writes a double value to memory address in selected byte order.
672: * @param addr memory address
673: * @param value new memory value
674: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
675: */
676: public final void setDouble(long addr, double value, int byteOrder) {
677: if (byteOrder == nativeByteOrder) {
678: setDouble(addr, value);
679: return;
680: }
681: setDoubleReorder(addr, value);
682: }
683:
684: /**
685: * Writes data from double array to memory address in platform byte order.
686: * @param addr data start address in memory
687: * @param value data array
688: * @param offset initial offset in java array
689: * @param length number of array elements to write
690: */
691: public final void setDouble(long addr, double[] value, int offset,
692: int length) {
693: setArray(addr, value, offset << 3, length << 3);
694: }
695:
696: /**
697: * Writes data from a double array to memory address in selected byte order.
698: * @param addr data start address in memory
699: * @param value data array
700: * @param offset initial offset in java array
701: * @param length number of array elements to write
702: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
703: */
704: public final void setDouble(long addr, double[] value, int offset,
705: int length, int byteOrder) {
706: if (byteOrder == nativeByteOrder) {
707: setArray(addr, value, offset << 3, length << 3);
708: return;
709: }
710: setArrayReorder64(addr, value, offset, length);
711: }
712:
713: private final native void setDoubleReorder(long addr, double value);
714:
715: /**
716: * Writes a float value to memory address in platform byte order.
717: * @param addr memory address
718: * @param value new memory value
719: */
720: public final native void setFloat(long addr, float value);
721:
722: /**
723: * Writes data to memory address in selected byte order.
724: * @param addr memory address
725: * @param value new memory value
726: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
727: */
728: public final void setFloat(long addr, float value, int byteOrder) {
729: if (byteOrder == nativeByteOrder) {
730: setFloat(addr, value);
731: return;
732: }
733: setFloatReorder(addr, value);
734: }
735:
736: /**
737: * Writes data from float array to memory address in platform byte order.
738: * @param addr data start address in memory
739: * @param value data array
740: * @param offset initial offset in java array
741: * @param length number of array elements to write
742: */
743: public final void setFloat(long addr, float[] value, int offset,
744: int length) {
745: setArray(addr, value, offset << 2, length << 2);
746: }
747:
748: /**
749: * Writes data from float array to memory address in selected byte order.
750: * @param addr data start address in memory
751: * @param value data array
752: * @param offset initial offset in java array
753: * @param length number of array elements to write
754: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
755: */
756: public final void setFloat(long addr, float[] value, int offset,
757: int length, int byteOrder) {
758: if (byteOrder == nativeByteOrder) {
759: setArray(addr, value, offset << 2, length << 2);
760: return;
761: }
762: setArrayReorder32(addr, value, offset, length);
763: }
764:
765: private final native void setFloatReorder(long addr, float value);
766:
767: /**
768: * Writes an integer value to memory address in platform byte order.
769: * @param addr memory address
770: * @param value new memory value
771: */
772: public final native void setInt(long addr, int value);
773:
774: /**
775: * Writes an integer value to memory address in selected byte order.
776: * @param addr memory address
777: * @param value new memory value
778: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
779: */
780: public final void setInt(long addr, int value, int byteOrder) {
781: if (byteOrder == nativeByteOrder) {
782: setInt(addr, value);
783: return;
784: }
785: setIntReorder(addr, value);
786: }
787:
788: /**
789: * Writes data from integer array to memory address in platform byte order.
790: * @param addr data start address in memory
791: * @param value data array
792: * @param offset initial offset in java array
793: * @param length number of array elements to write
794: */
795: public final void setInt(long addr, int[] value, int offset,
796: int length) {
797: setArray(addr, value, offset << 2, length << 2);
798: }
799:
800: /**
801: * Writes data from integer array to memory address in selected byte order.
802: * @param addr data start address in memory
803: * @param value data array
804: * @param offset initial offset in java array
805: * @param length number of array elements to write
806: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
807: */
808: public final void setInt(long addr, int[] value, int offset,
809: int length, int byteOrder) {
810: if (byteOrder == nativeByteOrder) {
811: setArray(addr, value, offset << 2, length << 2);
812: return;
813: }
814: setArrayReorder32(addr, value, offset, length);
815: }
816:
817: private final native void setIntReorder(long addr, int value);
818:
819: /**
820: * Writes a long value to memory address in platform byte order.
821: * @param addr memory address
822: * @param value new memory value
823: */
824: public final native void setLong(long addr, long value);
825:
826: /**
827: * Writes a long value to memory address in selected byte order.
828: * @param addr memory address
829: * @param value new memory value
830: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
831: */
832: public final void setLong(long addr, long value, int byteOrder) {
833: if (byteOrder == nativeByteOrder) {
834: setLong(addr, value);
835: return;
836: }
837: setLongReorder(addr, value);
838: }
839:
840: /**
841: * Writes data from long array to memory address in platform byte order.
842: * @param addr data start address in memory
843: * @param value data array
844: * @param offset initial offset in java array
845: * @param length number of array elements to write
846: */
847: public final void setLong(long addr, long[] value, int offset,
848: int length) {
849: setArray(addr, value, offset << 3, length << 3);
850: }
851:
852: /**
853: * Writes data from long array to memory address in selected byte order.
854: * @param addr data start address in memory
855: * @param value data array
856: * @param offset initial offset in java array
857: * @param length number of array elements to write
858: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
859: */
860: public void setLong(long addr, long[] value, int offset,
861: int length, int byteOrder) {
862: if (byteOrder == nativeByteOrder) {
863: setArray(addr, value, offset << 3, length << 3);
864: return;
865: }
866: setArrayReorder64(addr, value, offset, length);
867: }
868:
869: private final native void setLongReorder(long addr, long value);
870:
871: /**
872: * Writes a short value to memory address in platform byte order.
873: * @param addr memory address
874: * @param value new memory value
875: */
876: public final native void setShort(long addr, short value);
877:
878: /**
879: * Writes a short value to memory address in selected byte order.
880: * @param addr memory address
881: * @param value new memory value
882: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
883: */
884: public final void setShort(long addr, short value, int byteOrder) {
885: if (byteOrder == nativeByteOrder) {
886: setShort(addr, value);
887: return;
888: }
889: setShortReorder(addr, value);
890: }
891:
892: /**
893: * Writes data from short array to memory address in platform byte order.
894: * @param addr data start address in memory
895: * @param value data array
896: * @param offset initial offset in java array
897: * @param length number of array elements to write
898: */
899: public final void setShort(long addr, short[] value, int offset,
900: int length) {
901: setArray(addr, value, offset << 1, length << 1);
902: }
903:
904: /**
905: * Writes data from short array to memory address in selected byte order.
906: * @param addr data start address in memory
907: * @param value data array
908: * @param offset initial offset in java array
909: * @param length number of array elements to write
910: * @param byteOrder LITTLE_ENDIAN or BIG_ENDIAN
911: */
912: public final void setShort(long addr, short[] value, int offset,
913: int length, int byteOrder) {
914: if (byteOrder == nativeByteOrder) {
915: setArray(addr, value, offset << 1, length << 1);
916: return;
917: }
918: setArrayReorder16(addr, value, offset, length);
919: }
920:
921: private final native void setShortReorder(long addr, short value);
922:
923: /**
924: * Returns native pointer value from addr.
925: * @param addr address in memory
926: * @return memory value
927: */
928: public final native long getPointer(long addr);
929:
930: /**
931: * Writes a native pointer value to addr.
932: * @param addr address in memory
933: * @param value new memory value
934: */
935: public final native void setPointer(long addr, long value);
936:
937: /**
938: * Returns the native pointer size of the current platform in bytes,
939: * may be 4 or 8.
940: */
941: public final int getPointerSize() {
942: return pointerSize;
943: }
944:
945: /**
946: * Returns the C long size of the current platform in bytes,
947: * may be 4 or 8.
948: */
949: public final int getCLongSize() {
950: return cLongSize;
951: }
952:
953: /**
954: * Returns byte order used by platform
955: * @return LITTLE_ENDIAN or BIG_ENDIAN
956: */
957: public final int getNativeByteOrder() {
958: return nativeByteOrder;
959: }
960:
961: }
|