Java Code Examples for sun.misc.Unsafe#getUnsafe()

The following examples show how to use sun.misc.Unsafe#getUnsafe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridUnsafe.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @return Instance of Unsafe class.
 */
private static Unsafe unsafe() {
    try {
        return Unsafe.getUnsafe();
    }
    catch (SecurityException ignored) {
        try {
            return AccessController.doPrivileged(
                new PrivilegedExceptionAction<Unsafe>() {
                    @Override public Unsafe run() throws Exception {
                        Field f = Unsafe.class.getDeclaredField("theUnsafe");

                        f.setAccessible(true);

                        return (Unsafe)f.get(null);
                    }
                });
        }
        catch (PrivilegedActionException e) {
            throw new RuntimeException("Could not initialize intrinsics.", e.getCause());
        }
    }
}
 
Example 2
Source File: MappedByteBuffer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example 3
Source File: MappedByteBuffer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example 4
Source File: MappedByteBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example 5
Source File: MappedByteBuffer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example 6
Source File: UnsignedBytes.java    From lin-check with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
 * Replace with a simple call to Unsafe.getUnsafe when integrating
 * into a jdk.
 *
 * @return a sun.misc.Unsafe
 */
private static Unsafe getUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {}
    try {
        return java.security.AccessController.doPrivileged
        (new java.security.PrivilegedExceptionAction<Unsafe>() {
            public Unsafe run() throws Exception {
                Class<Unsafe> k = Unsafe.class;
                for (java.lang.reflect.Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x))
                        return k.cast(x);
                }
                throw new NoSuchFieldError("the Unsafe");
            }});
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics",
                                   e.getCause());
    }
}
 
Example 7
Source File: MappedByteBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example 8
Source File: MappedByteBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
Example 9
Source File: UnsafeAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Unsafe initUnsafe() {
    try {
        // Fast path when we are trusted.
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        // Slow path when we are not trusted.
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
Example 10
Source File: UnsafeAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Unsafe initUnsafe() {
    try {
        // Fast path when we are trusted.
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        // Slow path when we are not trusted.
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
Example 11
Source File: UnsafeAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Unsafe initUnsafe() {
    try {
        // Fast path when we are trusted.
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        // Slow path when we are not trusted.
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
Example 12
Source File: AMD64ArrayEqualsOp.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Unsafe initUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
Example 13
Source File: UtilUnsafe.java    From aa with Apache License 2.0 5 votes vote down vote up
/** Fetch the Unsafe.  Use With Caution. */
public static Unsafe getUnsafe() {
  // Not on bootclasspath
  if( UtilUnsafe.class.getClassLoader() == null )
    return Unsafe.getUnsafe();
  try {
    final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
    fld.setAccessible(true);
    return (Unsafe) fld.get(UtilUnsafe.class);
  } catch (Exception e) {
    throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
  }
}
 
Example 14
Source File: RenderBuffer.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected RenderBuffer(int numBytes) {
    unsafe = Unsafe.getUnsafe();
    curAddress = baseAddress = unsafe.allocateMemory(numBytes);
    endAddress = baseAddress + numBytes;
    capacity = numBytes;
}
 
Example 15
Source File: RenderBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
protected RenderBuffer(int numBytes) {
    unsafe = Unsafe.getUnsafe();
    curAddress = baseAddress = unsafe.allocateMemory(numBytes);
    endAddress = baseAddress + numBytes;
    capacity = numBytes;
}
 
Example 16
Source File: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected RenderBuffer(int numBytes) {
    unsafe = Unsafe.getUnsafe();
    curAddress = baseAddress = unsafe.allocateMemory(numBytes);
    endAddress = baseAddress + numBytes;
    capacity = numBytes;
}
 
Example 17
Source File: RenderBuffer.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected RenderBuffer(int numBytes) {
    unsafe = Unsafe.getUnsafe();
    curAddress = baseAddress = unsafe.allocateMemory(numBytes);
    endAddress = baseAddress + numBytes;
    capacity = numBytes;
}
 
Example 18
Source File: RenderBuffer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected RenderBuffer(int numBytes) {
    unsafe = Unsafe.getUnsafe();
    curAddress = baseAddress = unsafe.allocateMemory(numBytes);
    endAddress = baseAddress + numBytes;
    capacity = numBytes;
}
 
Example 19
Source File: RenderBuffer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected RenderBuffer(int numBytes) {
    unsafe = Unsafe.getUnsafe();
    curAddress = baseAddress = unsafe.allocateMemory(numBytes);
    endAddress = baseAddress + numBytes;
    capacity = numBytes;
}
 
Example 20
Source File: Bar.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void run() {
    Unsafe unsafe = Unsafe.getUnsafe();
}