Package sun.misc

Examples of sun.misc.Unsafe


        int c2 = 0x1b873593;

        int h1 = seed;
        int roundedEnd = offset + (len & 0xfffffffc)// round down to 4 byte block

        Unsafe unsafe = UnsafeHelper.UNSAFE;
        for (int i = offset; i < roundedEnd; i += 4) {
            // little endian load order
            int k1 = LITTLE_ENDIAN ? unsafe.getInt(address + i)
                    : (unsafe.getByte(address + i + 3) & 0xff)
                        | ((unsafe.getByte(address + i + 2) & 0xff) << 8)
                        | ((unsafe.getByte(address + i + 1) & 0xff) << 16)
                        | (unsafe.getByte(address + i) << 24);
            k1 *= c1;
            k1 = (k1 << 15) | (k1 >>> 17)// ROTL32(k1,15);
            k1 *= c2;

            h1 ^= k1;
            h1 = (h1 << 13) | (h1 >>> 19)// ROTL32(h1,13);
            h1 = h1 * 5 + 0xe6546b64;
        }

        // tail
        int k1 = 0;

        switch (len & 0x03) {
            case 3:
                k1 = (unsafe.getByte(address + roundedEnd + 2) & 0xff) << 16;
                // fallthrough
            case 2:
                k1 |= (unsafe.getByte(address + roundedEnd + 1) & 0xff) << 8;
                // fallthrough
            case 1:
                k1 |= unsafe.getByte(address + roundedEnd) & 0xff;
                k1 *= c1;
                k1 = (k1 << 15) | (k1 >>> 17)// ROTL32(k1,15);
                k1 *= c2;
                h1 ^= k1;
        }
View Full Code Here


        k1 = 0;
        k2 = 0;

        int tail = ((len >>> 4) << 4) + offset;
        Unsafe unsafe = UnsafeHelper.UNSAFE;
        switch (len & 15) {
            case 15: k2 ^= (long) unsafe.getByte(address + tail + 14) << 48;
            case 14: k2 ^= (long) unsafe.getByte(address + tail + 13) << 40;
            case 13: k2 ^= (long) unsafe.getByte(address + tail + 12) << 32;
            case 12: k2 ^= (long) unsafe.getByte(address + tail + 11) << 24;
            case 11: k2 ^= (long) unsafe.getByte(address + tail + 10) << 16;
            case 10: k2 ^= (long) unsafe.getByte(address + tail + 9) << 8;
            case 9:  k2 ^= unsafe.getByte(address + tail + 8);

            case 8:  k1 ^= (long) unsafe.getByte(address + tail + 7) << 56;
            case 7:  k1 ^= (long) unsafe.getByte(address + tail + 6) << 48;
            case 6:  k1 ^= (long) unsafe.getByte(address + tail + 5) << 40;
            case 5:  k1 ^= (long) unsafe.getByte(address + tail + 4) << 32;
            case 4:  k1 ^= (long) unsafe.getByte(address + tail + 3) << 24;
            case 3:  k1 ^= (long) unsafe.getByte(address + tail + 2) << 16;
            case 2:  k1 ^= (long) unsafe.getByte(address + tail + 1) << 8;
            case 1:  k1 ^= unsafe.getByte(address + tail);

                // bmix();
                k1 *= c1;
                k1 = (k1 << 23) | (k1 >>> 64 - 23);
                k1 *= c2;
View Full Code Here

                | ((key[i + 6] & 0x00000000000000FFL) << 48)
                | ((key[i + 7] & 0x00000000000000FFL) << 56);
    }

    private static long MurmurHash3_getBlock_direct(long address, int i) {
        Unsafe unsafe = UnsafeHelper.UNSAFE;
        if (LITTLE_ENDIAN) {
            return unsafe.getLong(address + i);
        } else {
            return ((unsafe.getByte(address + i) & 0x00000000000000FFL))
                    | ((unsafe.getByte(address + i + 1) & 0x00000000000000FFL) << 8)
                    | ((unsafe.getByte(address + i + 2) & 0x00000000000000FFL) << 16)
                    | ((unsafe.getByte(address + i + 3) & 0x00000000000000FFL) << 24)
                    | ((unsafe.getByte(address + i + 4) & 0x00000000000000FFL) << 32)
                    | ((unsafe.getByte(address + i + 5) & 0x00000000000000FFL) << 40)
                    | ((unsafe.getByte(address + i + 6) & 0x00000000000000FFL) << 48)
                    | ((unsafe.getByte(address + i + 7) & 0x00000000000000FFL) << 56);
        }
    }
View Full Code Here

  public static void main(String[] args)
      throws NoSuchFieldException, IllegalAccessException {
    // work around permissions issues
    Field f = Unsafe.class.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    Unsafe unsafe = (Unsafe)f.get(null);
    long addr = unsafe.allocateMemory(100);

    // test of endianness
    unsafe.putLong(addr, 1);
    System.out.println(unsafe.getByte(addr));

    // ensure we are using signed bytes
    unsafe.setMemory(addr, 10L, (byte)(-1));
    System.out.println(unsafe.getByte(addr + 5));

    unsafe.freeMemory(addr);
  }
View Full Code Here

      return null;
    }
  }

  public static void main(String[] args) {
    Unsafe unsafe = getUnsafe();
    // These will vary from platform to platform,
    // so just make sure they exist
    int addrSize = unsafe.addressSize();
    int abo = unsafe.arrayBaseOffset(int[].class);
    int ais = unsafe.arrayIndexScale(int[].class);

    Object f;
    try {
      f = unsafe.allocateInstance(Foo.class);
      System.out.println(((Foo)f).a);
      System.out.println(((Foo)f).b);
      System.out.println(((Foo)f).c);
    } catch (InstantiationException e) {
      System.out.println(e);
      return;
    }

    Field fc;
    try {
      fc = Foo.class.getDeclaredField("c");
    } catch (NoSuchFieldException e) {
      System.out.println(e);
      return;
    }
    long offset = unsafe.objectFieldOffset(fc);
    System.out.println(unsafe.getObject(f,offset));
    unsafe.putObject(f,offset, "hello Unsafe");
    System.out.println(unsafe.getObject(f,offset));
    System.out.println(((Foo)f).c);
    String newC = "hello again Unsafe";
    unsafe.putOrderedObject(f,offset,newC);
    System.out.println(unsafe.getObject(f,offset));
    System.out.println(((Foo)f).c);

    // test compareAndSwapObject
      boolean updated = unsafe.compareAndSwapObject(f,offset,"not newC","whargl");
      System.out.println(updated);
      System.out.println(((Foo)f).c);
      updated = unsafe.compareAndSwapObject(f,offset,newC,"whargl");
      System.out.println(updated);
      System.out.println(((Foo)f).c);
    }
    // Test throwException
    Exception e = new Exception("I'm exceptional!");
    try {
      unsafe.throwException(e);
    } catch (Exception exception) {
      System.out.println("Caught an exception! Is it the same as the one I threw? "+((e == exception)?"true":"false"));
    }
  }
View Full Code Here

        return (T)b;
    }

    public static void main( String arg[] ) {
        System.setProperty("fst.unsafe","true");
        Unsafe un = FSTUtil.getUnsafe();
        FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
        FSTClazzInfo cl = conf.getClassInfo(Sample.class);
        Sample test = new Sample();
        System.out.println("fioff "+cl.getFieldInfo("fastTime",Date.class).getMemOffset() );
        System.out.println("fioff "+cl.getFieldInfo("x",Sample.class).getMemOffset() );

        test.x = 9999;

        Sample other = new Sample();

        long pos = cl.getFieldInfo("x", Sample.class).getMemOffset();
        long siz = cl.getFieldInfo("s", Sample.class).getMemOffset()+8;
        byte b[] = new byte[(int) siz];
        un.copyMemory(test,0, b, 0, siz);

        other = copyObj(un,test, (int) siz);

    }
View Full Code Here

                    }
                }
            }
        ) ;

        Unsafe unsafe = null;

        try {
            unsafe = (Unsafe)(fld.get( null )) ;
        } catch (Throwable t) {
            Error err = new Error( "Could not access Unsafe" ) ;
View Full Code Here

  public static void main(String[] args) throws Exception {
    Class c = Test7190310_unsafe.class.getClassLoader().loadClass("sun.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    Unsafe unsafe = (Unsafe)f.get(c);

    f = Reference.class.getDeclaredField("referent");
    f.setAccessible(true);
    long referent_offset = unsafe.objectFieldOffset(f);

    Test7190310_unsafe t = new Test7190310_unsafe();
    TestObject o = new TestObject();
    t.obj = o;
View Full Code Here

    private Class<?> performLoadClassChecked(final String className, final boolean exportsOnly, final boolean resolve) throws ClassNotFoundException {
        if (SAFE_JDK) {
            return performLoadClassUnchecked(className, exportsOnly, resolve);
        } else if (Thread.holdsLock(this)) {
            if (LOCKLESS) {
                final Unsafe unsafe = UnsafeHolder.UNSAFE;
                unsafe.monitorExit(this);
                try {
                    return performLoadClassChecked(className, exportsOnly, resolve);
                } finally {
                    unsafe.monitorEnter(this);
                }
            }
            if (Thread.currentThread() != LoaderThreadHolder.LOADER_THREAD) {
                // Only the classloader thread may take this lock; use a condition to relinquish it
                final LoadRequest req = new LoadRequest(className, resolve, exportsOnly, this, AccessController.getContext());
View Full Code Here

    private Class<?> performLoadClassChecked(final String className, final boolean exportsOnly, final boolean resolve) throws ClassNotFoundException {
        if (SAFE_JDK) {
            return performLoadClassUnchecked(className, exportsOnly, resolve);
        } else if (Thread.holdsLock(this)) {
            if (LOCKLESS) {
                final Unsafe unsafe = UnsafeHolder.UNSAFE;
                unsafe.monitorExit(this);
                try {
                    return performLoadClassChecked(className, exportsOnly, resolve);
                } finally {
                    unsafe.monitorEnter(this);
                }
            }
            if (Thread.currentThread() != LoaderThreadHolder.LOADER_THREAD) {
                // Only the classloader thread may take this lock; use a condition to relinquish it
                final LoadRequest req = new LoadRequest(className, resolve, exportsOnly, this, AccessController.getContext());
View Full Code Here

TOP

Related Classes of sun.misc.Unsafe

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.