Memory.java revision 7e25eff38a191d9c19e45093f4fde5102fb09d78
1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/* 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership. 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with 7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License. You may obtain a copy of the License at 8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 18f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughespackage libcore.io; 19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesimport java.io.FileDescriptor; 21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.IOException; 228510524dab13e0acc1babf22cbc55002fb122777Elliott Hughesimport java.nio.ByteOrder; 23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/** 25f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes * Unsafe access to memory. 26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 27f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughespublic final class Memory { 28f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes private Memory() { } 29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 318fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes * Used to optimize nio heap buffer bulk get operations. 'dst' must be a primitive array. 326944bea4a129dc2d4be687c72f2a9f228ec532bcElliott Hughes * 'dstOffset' is measured in units of 'sizeofElements' bytes. 336944bea4a129dc2d4be687c72f2a9f228ec532bcElliott Hughes */ 348fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes public static native void unsafeBulkGet(Object dst, int dstOffset, int byteCount, 356944bea4a129dc2d4be687c72f2a9f228ec532bcElliott Hughes byte[] src, int srcOffset, int sizeofElements, boolean swap); 366944bea4a129dc2d4be687c72f2a9f228ec532bcElliott Hughes 376944bea4a129dc2d4be687c72f2a9f228ec532bcElliott Hughes /** 388fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes * Used to optimize nio heap buffer bulk put operations. 'src' must be a primitive array. 398fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes * 'srcOffset' is measured in units of 'sizeofElements' bytes. 408fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes */ 418fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes public static native void unsafeBulkPut(byte[] dst, int dstOffset, int byteCount, 428fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes Object src, int srcOffset, int sizeofElements, boolean swap); 438fbc397fc09158bee0bc0cb231c609c4c6e9fc15Elliott Hughes 44af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes public static int peekInt(byte[] src, int offset, ByteOrder order) { 45af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes if (order == ByteOrder.BIG_ENDIAN) { 46af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes return (((src[offset++] & 0xff) << 24) | 47af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 16) | 48af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 8) | 49af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset ] & 0xff) << 0)); 50af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } else { 51af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes return (((src[offset++] & 0xff) << 0) | 52af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 8) | 53af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 16) | 54af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset ] & 0xff) << 24)); 55af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 56af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 57af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes 58af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes public static long peekLong(byte[] src, int offset, ByteOrder order) { 59af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes if (order == ByteOrder.BIG_ENDIAN) { 60af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes int h = ((src[offset++] & 0xff) << 24) | 61af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 16) | 62af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 8) | 63af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 0); 64af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes int l = ((src[offset++] & 0xff) << 24) | 65af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 16) | 66af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 8) | 67af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset ] & 0xff) << 0); 68af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes return (((long) h) << 32L) | ((long) l) & 0xffffffffL; 69af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } else { 70af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes int l = ((src[offset++] & 0xff) << 0) | 71af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 8) | 72af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 16) | 73af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 24); 74af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes int h = ((src[offset++] & 0xff) << 0) | 75af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 8) | 76af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset++] & 0xff) << 16) | 77af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes ((src[offset ] & 0xff) << 24); 78af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes return (((long) h) << 32L) | ((long) l) & 0xffffffffL; 79af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 80af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 81af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes 82af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes public static short peekShort(byte[] src, int offset, ByteOrder order) { 83af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes if (order == ByteOrder.BIG_ENDIAN) { 84af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes return (short) ((src[offset] << 8) | (src[offset + 1] & 0xff)); 85af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } else { 86af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes return (short) ((src[offset + 1] << 8) | (src[offset] & 0xff)); 87af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 88af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 89af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes 90af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order) { 91af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes if (order == ByteOrder.BIG_ENDIAN) { 92af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 24) & 0xff); 93af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 16) & 0xff); 94af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 8) & 0xff); 95af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset ] = (byte) ((value >> 0) & 0xff); 96af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } else { 97af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 0) & 0xff); 98af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 8) & 0xff); 99af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 16) & 0xff); 100af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset ] = (byte) ((value >> 24) & 0xff); 101af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 102af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 103af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes 104af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes public static void pokeLong(byte[] dst, int offset, long value, ByteOrder order) { 105af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes if (order == ByteOrder.BIG_ENDIAN) { 106af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes int i = (int) (value >> 32); 107af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 24) & 0xff); 108af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 16) & 0xff); 109af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 8) & 0xff); 110af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 0) & 0xff); 111af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes i = (int) value; 112af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 24) & 0xff); 113af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 16) & 0xff); 114af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 8) & 0xff); 115af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset ] = (byte) ((i >> 0) & 0xff); 116af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } else { 117af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes int i = (int) value; 118af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 0) & 0xff); 119af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 8) & 0xff); 120af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 16) & 0xff); 121af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 24) & 0xff); 122af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes i = (int) (value >> 32); 123af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 0) & 0xff); 124af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 8) & 0xff); 125af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((i >> 16) & 0xff); 126af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset ] = (byte) ((i >> 24) & 0xff); 127af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 128af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 129af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes 130af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes public static void pokeShort(byte[] dst, int offset, short value, ByteOrder order) { 131af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes if (order == ByteOrder.BIG_ENDIAN) { 132af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 8) & 0xff); 133af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset ] = (byte) ((value >> 0) & 0xff); 134af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } else { 135af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset++] = (byte) ((value >> 0) & 0xff); 136af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes dst[offset ] = (byte) ((value >> 8) & 0xff); 137af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 138af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes } 139af301fb0e674665dae3e9cb94344558923ec8e0aElliott Hughes 14027c11c7c7b1fc023cb6464bd551aceef300e39b6Elliott Hughes public static native void memmove(int destAddress, int srcAddress, long byteCount); 141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 1421b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native byte peekByte(int address); 1431b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native int peekInt(int address, boolean swap); 1441b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native long peekLong(int address, boolean swap); 1451b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native short peekShort(int address, boolean swap); 146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 147692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void peekByteArray(int address, byte[] dst, int dstOffset, int byteCount); 148961da1e7487bdb8ad8ac226d4f2789d003aa87b9Elliott Hughes public static native void peekCharArray(int address, char[] dst, int dstOffset, int charCount, boolean swap); 149961da1e7487bdb8ad8ac226d4f2789d003aa87b9Elliott Hughes public static native void peekDoubleArray(int address, double[] dst, int dstOffset, int doubleCount, boolean swap); 150961da1e7487bdb8ad8ac226d4f2789d003aa87b9Elliott Hughes public static native void peekFloatArray(int address, float[] dst, int dstOffset, int floatCount, boolean swap); 151692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void peekIntArray(int address, int[] dst, int dstOffset, int intCount, boolean swap); 152961da1e7487bdb8ad8ac226d4f2789d003aa87b9Elliott Hughes public static native void peekLongArray(int address, long[] dst, int dstOffset, int longCount, boolean swap); 153961da1e7487bdb8ad8ac226d4f2789d003aa87b9Elliott Hughes public static native void peekShortArray(int address, short[] dst, int dstOffset, int shortCount, boolean swap); 154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 1551b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native void pokeByte(int address, byte value); 1561b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native void pokeInt(int address, int value, boolean swap); 1571b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native void pokeLong(int address, long value, boolean swap); 1581b9018762e87e3dda69020248817011efd5a40dcElliott Hughes public static native void pokeShort(int address, short value, boolean swap); 159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 160692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeByteArray(int address, byte[] src, int offset, int count); 161692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeCharArray(int address, char[] src, int offset, int count, boolean swap); 162692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeDoubleArray(int address, double[] src, int offset, int count, boolean swap); 163692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeFloatArray(int address, float[] src, int offset, int count, boolean swap); 164692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeIntArray(int address, int[] src, int offset, int count, boolean swap); 165692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeLongArray(int address, long[] src, int offset, int count, boolean swap); 166692222b08ff88eb92b523bf4780d7ea17a23aa80Elliott Hughes public static native void pokeShortArray(int address, short[] src, int offset, int count, boolean swap); 16771961a4957d24f62f7adc5f0549e236018589631Elliott Hughes 1688510524dab13e0acc1babf22cbc55002fb122777Elliott Hughes public static native void load(int addr, long size); 1698510524dab13e0acc1babf22cbc55002fb122777Elliott Hughes public static native boolean isLoaded(int addr, long size); 170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 171