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

The following examples show how to use sun.misc.Unsafe#getByte() . 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: MappedByteBuffer.java    From dragonwell8_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 2
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 3
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 4
Source File: MappedByteBuffer.java    From jdk8u-dev-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 5
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 6
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 7
Source File: MappedByteBuffer.java    From jdk1.8-source-analysis 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 8
Source File: CopyMemory.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
        if (r != value) {
            throw new RuntimeException("mismatch");
        }
    }
}
 
Example 9
Source File: CopyMemory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
        if (r != value) {
            throw new RuntimeException("mismatch");
        }
    }
}
 
Example 10
Source File: UnsafeUtils.java    From message-queue-java with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    Unsafe unsafe = getUnsafe();
    long address = unsafe.allocateMemory(2);
    short number = 1;
    unsafe.putShort(address, number);
    if (unsafe.getByte(address) == 0)
        System.out.println("Big Endian");
    else
        System.out.println("Little Endian");
    unsafe.freeMemory(address);
}
 
Example 11
Source File: CopyMemory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
        if (r != value) {
            throw new RuntimeException("mismatch");
        }
    }
}
 
Example 12
Source File: CopyMemory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
        if (r != value) {
            throw new RuntimeException("mismatch");
        }
    }
}
 
Example 13
Source File: CopyMemory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
        if (r != value) {
            throw new RuntimeException("mismatch");
        }
    }
}
 
Example 14
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("all")
public static int unsafePutByte(Unsafe unsafe, Object obj, long offset, byte value) {
    int res = 1;
    unsafe.putByte(obj, offset, (byte) (value + 1));
    res += unsafe.getByte(obj, offset);
    unsafe.putByteVolatile(obj, offset, (byte) (value + 2));
    res += unsafe.getByte(obj, offset);
    return res;
}
 
Example 15
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("all")
public static double unsafeDirectMemoryRead(Unsafe unsafe, long address) {
    // Unsafe.getBoolean(long) and Unsafe.getObject(long) do not exist
    // @formatter:off
    return unsafe.getByte(address) +
           unsafe.getShort(address + 8) +
           unsafe.getChar(address + 16) +
           unsafe.getInt(address + 24) +
           unsafe.getLong(address + 32) +
           unsafe.getFloat(address + 40) +
           unsafe.getDouble(address + 48);
    // @formatter:on
}
 
Example 16
Source File: CopyMemory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
        if (r != value) {
            throw new RuntimeException("mismatch");
        }
    }
}
 
Example 17
Source File: BeanAccessor.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Object getBeanValue(Object bean, String fieldName) {
    if (bean == null || fieldName == null) {
        return null;
    }
    Class<?> clazz = bean.getClass();

    Map<String, Field> fieldMap = getBeanFieldMap(clazz);
    Field field = fieldMap.get(fieldName);
    Unsafe unsafe = getUnsafeInstance();
    if (unsafe == null) {
        try {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            return field.get(bean);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    long offset = getBeanFieldOffset(clazz, fieldName);
    if (offset == -1) {
        return null;
    }

    Class<?> type = field.getType();
    if (type == boolean.class) {
        return unsafe.getBoolean(bean, offset);
    } else if (type == byte.class) {
        return unsafe.getByte(bean, offset);
    } else if (type == short.class) {
        return unsafe.getShort(bean, offset);
    } else if (type == char.class) {
        return unsafe.getChar(bean, offset);
    } else if (type == int.class) {
        return unsafe.getInt(bean, offset);
    } else if (type == long.class) {
        return unsafe.getLong(bean, offset);
    } else if (type == float.class) {
        return unsafe.getFloat(bean, offset);
    } else if (type == double.class) {
        return unsafe.getDouble(bean, offset);
    }
    return unsafe.getObject(bean, offset);

}
 
Example 18
Source File: MarlinTileGenerator.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the alpha coverage values for the current tile.
 * Either this method, or the nextTile() method should be called
 * once per tile, but not both.
 */
private void getAlphaNoRLE(final byte[] tile, final int offset,
                           final int rowstride)
{
    if (DO_MONITORS) {
        rdrStats.mon_ptg_getAlpha.start();
    }

    // local vars for performance:
    final MarlinCache _cache = this.cache;
    final long[] rowAAChunkIndex = _cache.rowAAChunkIndex;
    final int[] rowAAx0 = _cache.rowAAx0;
    final int[] rowAAx1 = _cache.rowAAx1;

    final int x0 = this.x;
    final int x1 = FloatMath.min(x0 + TILE_W, _cache.bboxX1);

    // note: process tile line [0 - 32[
    final int y0 = 0;
    final int y1 = FloatMath.min(this.y + TILE_H, _cache.bboxY1) - this.y;

    if (DO_LOG_BOUNDS) {
        MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
                            + "[ [" + y0 + " ... " + y1 + "[");
    }

    final Unsafe _unsafe = OffHeapArray.UNSAFE;
    final long SIZE = 1L;
    final long addr_rowAA = _cache.rowAAChunk.address;
    long addr;

    final int skipRowPixels = (rowstride - (x1 - x0));

    int aax0, aax1, end;
    int idx = offset;

    for (int cy = y0, cx; cy < y1; cy++) {
        // empty line (default)
        cx = x0;

        aax1 = rowAAx1[cy]; // exclusive

        // quick check if there is AA data
        // corresponding to this tile [x0; x1[
        if (aax1 > x0) {
            aax0 = rowAAx0[cy]; // inclusive

            if (aax0 < x1) {
                // note: cx is the cursor pointer in the tile array
                // (left to right)
                cx = aax0;

                // ensure cx >= x0
                if (cx <= x0) {
                    cx = x0;
                } else {
                    // fill line start until first AA pixel rowAA exclusive:
                    for (end = x0; end < cx; end++) {
                        tile[idx++] = 0;
                    }
                }

                // now: cx >= x0 and cx >= aax0

                // Copy AA data (sum alpha data):
                addr = addr_rowAA + rowAAChunkIndex[cy] + (cx - aax0);

                for (end = (aax1 <= x1) ? aax1 : x1; cx < end; cx++) {
                    // cx inside tile[x0; x1[ :
                    tile[idx++] = _unsafe.getByte(addr); // [0-255]
                    addr += SIZE;
                }
            }
        }

        // fill line end
        while (cx < x1) {
            tile[idx++] = 0;
            cx++;
        }

        if (DO_TRACE) {
            for (int i = idx - (x1 - x0); i < idx; i++) {
                System.out.print(hex(tile[i], 2));
            }
            System.out.println();
        }

        idx += skipRowPixels;
    }

    nextTile();

    if (DO_MONITORS) {
        rdrStats.mon_ptg_getAlpha.stop();
    }
}
 
Example 19
Source File: MarlinTileGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the alpha coverage values for the current tile.
 * Either this method, or the nextTile() method should be called
 * once per tile, but not both.
 */
private void getAlphaNoRLE(final byte tile[], final int offset,
                           final int rowstride)
{
    if (doMonitors) {
        RendererContext.stats.mon_ptg_getAlpha.start();
    }

    // local vars for performance:
    final MarlinCache _cache = this.cache;
    final long[] rowAAChunkIndex = _cache.rowAAChunkIndex;
    final int[] rowAAx0 = _cache.rowAAx0;
    final int[] rowAAx1 = _cache.rowAAx1;

    final int x0 = this.x;
    final int x1 = FloatMath.min(x0 + TILE_SIZE, _cache.bboxX1);

    // note: process tile line [0 - 32[
    final int y0 = 0;
    final int y1 = FloatMath.min(this.y + TILE_SIZE, _cache.bboxY1) - this.y;

    if (doLogBounds) {
        MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
                            + "[ [" + y0 + " ... " + y1 + "[");
    }

    final Unsafe _unsafe = OffHeapArray.unsafe;
    final long SIZE = 1L;
    final long addr_rowAA = _cache.rowAAChunk.address;
    long addr;

    final int skipRowPixels = (rowstride - (x1 - x0));

    int aax0, aax1, end;
    int idx = offset;

    for (int cy = y0, cx; cy < y1; cy++) {
        // empty line (default)
        cx = x0;

        aax1 = rowAAx1[cy]; // exclusive

        // quick check if there is AA data
        // corresponding to this tile [x0; x1[
        if (aax1 > x0) {
            aax0 = rowAAx0[cy]; // inclusive

            if (aax0 < x1) {
                // note: cx is the cursor pointer in the tile array
                // (left to right)
                cx = aax0;

                // ensure cx >= x0
                if (cx <= x0) {
                    cx = x0;
                } else {
                    // fill line start until first AA pixel rowAA exclusive:
                    for (end = x0; end < cx; end++) {
                        tile[idx++] = 0;
                    }
                }

                // now: cx >= x0 but cx < aax0 (x1 < aax0)

                // Copy AA data (sum alpha data):
                addr = addr_rowAA + rowAAChunkIndex[cy] + (cx - aax0);

                for (end = (aax1 <= x1) ? aax1 : x1; cx < end; cx++) {
                    // cx inside tile[x0; x1[ :
                    tile[idx++] = _unsafe.getByte(addr); // [0..255]
                    addr += SIZE;
                }
            }
        }

        // fill line end
        while (cx < x1) {
            tile[idx++] = 0;
            cx++;
        }

        if (doTrace) {
            for (int i = idx - (x1 - x0); i < idx; i++) {
                System.out.print(hex(tile[i], 2));
            }
            System.out.println();
        }

        idx += skipRowPixels;
    }

    nextTile();

    if (doMonitors) {
        RendererContext.stats.mon_ptg_getAlpha.stop();
    }
}
 
Example 20
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("all")
public static int unsafeGetByte(Unsafe unsafe, Object obj, long offset) {
    return unsafe.getByte(obj, offset) + unsafe.getByteVolatile(obj, offset);
}