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

The following examples show how to use sun.misc.Unsafe#putByte() . 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: TestOffHeapDataStorage.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public void testUnsafeZeroMemory()
{
    FastUnsafeOffHeapMemoryInitialiser initialiser = new FastUnsafeOffHeapMemoryInitialiser();
    Unsafe unsafe = MithraUnsafe.getUnsafe();

    long limit = 1024;
    long ref = unsafe.allocateMemory(limit);

    try
    {
        for (long size = 1; size < limit; size++)
        {
            // Pre-populate the buffer with non-zero data
            for (long i = 0; i < limit; i++)
            {
                unsafe.putByte(ref + i, (byte) 0xff);
            }

            // Execute code under test
            initialiser.unsafeZeroMemory(ref, size, ref, limit);

            for (long i = 0; i < limit; i++)
            {
                long byteRef = ref + i;
                if (i < size)
                {
                    assertEquals("Buffer data content at location " + i + " (buffer size = " + size + ") should have been zeroed but was NOT", (byte) 0x00, unsafe.getByte(byteRef));
                } else
                {
                    assertEquals("Buffer data content at location " + i + " (buffer size = " + size + ") was modified but should NOT have been", (byte) 0xff, unsafe.getByte(byteRef));
                }
            }
        }
    }
    finally
    {
        unsafe.freeMemory(ref);
    }
}
 
Example 2
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 3
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 unsafeDirectMemoryWrite(Unsafe unsafe, long address, long value) {
    // Unsafe.putBoolean(long) and Unsafe.putObject(long) do not exist
    unsafe.putByte(address + 0, (byte) value);
    unsafe.putShort(address + 8, (short) value);
    unsafe.putChar(address + 16, (char) value);
    unsafe.putInt(address + 24, (int) value);
    unsafe.putLong(address + 32, value);
    unsafe.putFloat(address + 40, value);
    unsafe.putDouble(address + 48, value);
    return unsafeDirectMemoryRead(unsafe, address);
}
 
Example 4
Source File: CopyMemory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 5
Source File: CopyMemory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 6
Source File: CopyMemory.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 7
Source File: CopyMemory.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 8
Source File: CopyMemory.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 9
Source File: CopyMemory.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 10
Source File: CopyMemory.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 11
Source File: CopyMemory.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 12
Source File: CopyMemory.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 13
Source File: CopyMemory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 14
Source File: CopyMemory.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 15
Source File: CopyMemory.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}
 
Example 16
Source File: BeanAccessor.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void setBeanValue(Object bean, String fieldName, Object data) {
    if (bean == null || fieldName == null) {
        return;
    }

    Class<?> clazz = bean.getClass();

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

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

    Class<?> type = field.getType();
    if (type == boolean.class) {
        unsafe.putBoolean(bean, offset, ((Boolean) data).booleanValue());
    } else if (type == byte.class) {
        unsafe.putByte(bean, offset, ((Byte) data).byteValue());
    } else if (type == short.class) {
        unsafe.putShort(bean, offset, ((Short) data).shortValue());
    } else if (type == char.class) {
        unsafe.putChar(bean, offset, ((Character) data).charValue());
    } else if (type == int.class) {
        unsafe.putInt(bean, offset, ((Integer) data).intValue());
    } else if (type == long.class) {
        unsafe.putLong(bean, offset, ((Long) data).longValue());
    } else if (type == float.class) {
        unsafe.putFloat(bean, offset, ((Float) data).floatValue());
    } else if (type == double.class) {
        unsafe.putDouble(bean, offset, ((Double) data).doubleValue());
    } else {
        unsafe.putObject(bean, offset, data);
    }
}
 
Example 17
Source File: CopyMemory.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
    for (int i = 0; i < len; i++) {
        unsafe.putByte(null, addr + ofs + i, (byte)value);
    }
}