| | |
| | | * Bits |
| | | * User: Ulysses |
| | | * Date: 2007/6/17 |
| | | * Time: ¤W¤È 01:16:39 |
| | | * Time: 01:16:39 |
| | | */ |
| | | public class Bits |
| | | { |
| | | public class Bits { |
| | | // -- Unsafe access -- |
| | | // private static final Unsafe unsafe = Unsafe.getUnsafe(); |
| | | |
| | |
| | | private static volatile long reservedMemory = 0; |
| | | private static boolean memoryLimitSet = false; |
| | | |
| | | private Bits() |
| | | { |
| | | private Bits() { |
| | | } |
| | | |
| | | // -- Swapping -- |
| | | public static short swap(short x) |
| | | { |
| | | public static short swap(short x) { |
| | | return (short) ((x << 8) | ((x >> 8) & 0xff)); |
| | | } |
| | | |
| | | public static char swap(char x) |
| | | { |
| | | public static char swap(char x) { |
| | | return (char) ((x << 8) | ((x >> 8) & 0xff)); |
| | | } |
| | | |
| | | public static int swap(int x) |
| | | { |
| | | public static int swap(int x) { |
| | | return (int) ((swap((short) x) << 16) | (swap((short) (x >> 16)) & 0xffff)); |
| | | } |
| | | |
| | | public static long swap(long x) |
| | | { |
| | | public static long swap(long x) { |
| | | return (long) (((long) swap((int) (x)) << 32) | ((long) swap((int) (x >> 32)) & 0xffffffffL)); |
| | | } |
| | | |
| | | // -- get/put char -- |
| | | static private char makeChar(byte b1, byte b0) |
| | | { |
| | | static private char makeChar(byte b1, byte b0) { |
| | | return (char) ((b1 << 8) | (b0 & 0xff)); |
| | | } |
| | | |
| | | private static byte char1(char x) |
| | | { |
| | | private static byte char1(char x) { |
| | | return (byte) (x >> 8); |
| | | } |
| | | |
| | | private static byte char0(char x) |
| | | { |
| | | private static byte char0(char x) { |
| | | return (byte) (x >> 0); |
| | | } |
| | | |
| | | // --get/put short-- |
| | | public static short makeShort(byte b1, byte b0) |
| | | { |
| | | public static short makeShort(byte b1, byte b0) { |
| | | return (short) ((b1 << 8) | (b0 & 0xff)); |
| | | } |
| | | |
| | | private static byte short1(short x) |
| | | { |
| | | private static byte short1(short x) { |
| | | return (byte) (x >> 8); |
| | | } |
| | | |
| | | public static byte short0(short x) |
| | | { |
| | | public static byte short0(short x) { |
| | | return (byte) (x >> 0); |
| | | } |
| | | |
| | | // -- get/put int -- |
| | | public static int makeInt(byte b3, byte b2, byte b1, byte b0) |
| | | { |
| | | public static int makeInt(byte b3, byte b2, byte b1, byte b0) { |
| | | return (int) ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0))); |
| | | } |
| | | |
| | | public static int makeInt(short hiword, short loword) |
| | | { |
| | | public static int makeInt(short hiword, short loword) { |
| | | return ((hiword & 0xffff) << 16) + (loword & 0xffff); |
| | | } |
| | | |
| | | public static short getHiShort(int qwValue) |
| | | { |
| | | public static short getHiShort(int qwValue) { |
| | | return ((short) (qwValue >>> 16)); |
| | | } |
| | | |
| | | public static short getLoShort(int qwValue) |
| | | { |
| | | public static short getLoShort(int qwValue) { |
| | | return ((short) (qwValue & 0xFFFF)); |
| | | } |
| | | |
| | | public static byte int3(int x) |
| | | { |
| | | public static byte int3(int x) { |
| | | return (byte) (x >> 24); |
| | | } |
| | | |
| | | public static byte int2(int x) |
| | | { |
| | | public static byte int2(int x) { |
| | | return (byte) (x >> 16); |
| | | } |
| | | |
| | | private static byte int1(int x) |
| | | { |
| | | private static byte int1(int x) { |
| | | return (byte) (x >> 8); |
| | | } |
| | | |
| | | private static byte int0(int x) |
| | | { |
| | | private static byte int0(int x) { |
| | | return (byte) (x >> 0); |
| | | } |
| | | |
| | | // -- get/put long -- |
| | | public static long makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0) |
| | | { |
| | | public static long makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0) { |
| | | return ((((long) b7 & 0xff) << 56) | (((long) b6 & 0xff) << 48) | (((long) b5 & 0xff) << 40) | (((long) b4 & 0xff) << 32) |
| | | | (((long) b3 & 0xff) << 24) | (((long) b2 & 0xff) << 16) | (((long) b1 & 0xff) << 8) |
| | | | (((long) b0 & 0xff) << 0)); |
| | | | (((long) b3 & 0xff) << 24) | (((long) b2 & 0xff) << 16) | (((long) b1 & 0xff) << 8) |
| | | | (((long) b0 & 0xff) << 0)); |
| | | } |
| | | |
| | | public static long makeLong(int LoValue, int HiValue) |
| | | { |
| | | public static long makeLong(int LoValue, int HiValue) { |
| | | return (((long) HiValue & 0xFFFFFFFF) << 32) + (((long) LoValue) & 0xFFFFFFFF); |
| | | } |
| | | |
| | | public static int getHiInt(long qwValue) |
| | | { |
| | | public static int getHiInt(long qwValue) { |
| | | return ((int) (qwValue >>> 32)); |
| | | } |
| | | |
| | | public static int getLoInt(long qwValue) |
| | | { |
| | | public static int getLoInt(long qwValue) { |
| | | return ((int) (qwValue & 0xFFFFFFFF)); |
| | | } |
| | | |
| | | private static byte long7(long x) |
| | | { |
| | | private static byte long7(long x) { |
| | | return (byte) (x >> 56); |
| | | } |
| | | |
| | | private static byte long6(long x) |
| | | { |
| | | private static byte long6(long x) { |
| | | return (byte) (x >> 48); |
| | | } |
| | | |
| | | private static byte long5(long x) |
| | | { |
| | | private static byte long5(long x) { |
| | | return (byte) (x >> 40); |
| | | } |
| | | |
| | | private static byte long4(long x) |
| | | { |
| | | private static byte long4(long x) { |
| | | return (byte) (x >> 32); |
| | | } |
| | | |
| | | private static byte long3(long x) |
| | | { |
| | | private static byte long3(long x) { |
| | | return (byte) (x >> 24); |
| | | } |
| | | |
| | | private static byte long2(long x) |
| | | { |
| | | private static byte long2(long x) { |
| | | return (byte) (x >> 16); |
| | | } |
| | | |
| | | private static byte long1(long x) |
| | | { |
| | | private static byte long1(long x) { |
| | | return (byte) (x >> 8); |
| | | } |
| | | |
| | | private static byte long0(long x) |
| | | { |
| | | private static byte long0(long x) { |
| | | return (byte) (x >> 0); |
| | | } |
| | | |
| | |
| | | } |
| | | */ |
| | | |
| | | static boolean unaligned() |
| | | { |
| | | if (unalignedKnown) |
| | | { |
| | | static boolean unaligned() { |
| | | if (unalignedKnown) { |
| | | return unaligned; |
| | | } |
| | | |
| | |
| | | // These methods should be called whenever direct memory is allocated or |
| | | // freed. They allow the user to control the amount of direct memory |
| | | // which a process may access. All sizes are specified in bytes. |
| | | static void reserveMemory(long size) |
| | | { |
| | | synchronized (Bits.class) |
| | | { |
| | | if (!memoryLimitSet && VM.isBooted()) |
| | | { |
| | | static void reserveMemory(long size) { |
| | | synchronized (Bits.class) { |
| | | if (!memoryLimitSet && VM.isBooted()) { |
| | | maxMemory = VM.maxDirectMemory(); |
| | | memoryLimitSet = true; |
| | | } |
| | | |
| | | if (size <= maxMemory - reservedMemory) |
| | | { |
| | | if (size <= maxMemory - reservedMemory) { |
| | | reservedMemory += size; |
| | | |
| | | return; |
| | |
| | | |
| | | System.gc(); |
| | | |
| | | try |
| | | { |
| | | try { |
| | | Thread.sleep(100); |
| | | } catch (InterruptedException x) |
| | | { |
| | | } catch (InterruptedException x) { |
| | | // Restore interrupt status |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | |
| | | synchronized (Bits.class) |
| | | { |
| | | if (reservedMemory + size > maxMemory) |
| | | { |
| | | synchronized (Bits.class) { |
| | | if (reservedMemory + size > maxMemory) { |
| | | throw new OutOfMemoryError("Direct buffer memory"); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | static synchronized void unreserveMemory(long size) |
| | | { |
| | | if (reservedMemory > 0) |
| | | { |
| | | static synchronized void unreserveMemory(long size) { |
| | | if (reservedMemory > 0) { |
| | | reservedMemory -= size; |
| | | assert (reservedMemory > -1); |
| | | } |