Skip to content

Commit

Permalink
Refactor UnsafeUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Jan 8, 2024
1 parent bb3b01b commit afa4365
Showing 1 changed file with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,45 @@ private UnsafeUtils() {
}

private static boolean checkLongsOffset(final long[] array, final long offset) {
final long index = fromLongsOffset(offset);
return 0 <= index && index < array.length;
return inBounds(fromLongsOffset(offset), array.length);
}

private static boolean checkObjectsOffset(final Object[] array, final long offset) {
final long index = fromObjectsOffset(offset);
return 0 <= index && index < array.length;
return inBounds(fromObjectsOffset(offset), array.length);
}

public static void copyBytes(final byte[] src, final long srcPos, final byte[] dest, final long destPos, final long length) {
assert 0 <= srcPos && srcPos + length <= src.length && 0 <= destPos && destPos + length <= dest.length;
assert inBounds(srcPos, length, src.length) && inBounds(destPos, length, dest.length);
UNSAFE.copyMemory(src, Unsafe.ARRAY_BYTE_BASE_OFFSET + srcPos * Unsafe.ARRAY_BYTE_INDEX_SCALE,
dest, Unsafe.ARRAY_BYTE_BASE_OFFSET + destPos * Unsafe.ARRAY_BYTE_INDEX_SCALE, Byte.BYTES * length);
}

public static void copyChars(final char[] src, final long srcPos, final char[] dest, final long destPos, final long length) {
assert 0 <= srcPos && srcPos + length <= src.length && 0 <= destPos && destPos + length <= dest.length;
assert inBounds(srcPos, length, src.length) && inBounds(destPos, length, dest.length);
UNSAFE.copyMemory(src, Unsafe.ARRAY_CHAR_BASE_OFFSET + srcPos * Unsafe.ARRAY_CHAR_INDEX_SCALE,
dest, Unsafe.ARRAY_CHAR_BASE_OFFSET + destPos * Unsafe.ARRAY_CHAR_INDEX_SCALE, Character.BYTES * length);
}

public static void copyDoubles(final double[] src, final long srcPos, final double[] dest, final long destPos, final long length) {
assert 0 <= srcPos && srcPos + length <= src.length && 0 <= destPos && destPos + length <= dest.length;
assert inBounds(srcPos, length, src.length) && inBounds(destPos, length, dest.length);
UNSAFE.copyMemory(src, Unsafe.ARRAY_DOUBLE_BASE_OFFSET + srcPos * Unsafe.ARRAY_DOUBLE_INDEX_SCALE,
dest, Unsafe.ARRAY_DOUBLE_BASE_OFFSET + destPos * Unsafe.ARRAY_DOUBLE_INDEX_SCALE, Double.BYTES * length);
}

public static void copyInts(final int[] src, final long srcPos, final int[] dest, final long destPos, final long length) {
assert 0 <= srcPos && srcPos + length <= src.length && 0 <= destPos && destPos + length <= dest.length;
assert inBounds(srcPos, length, src.length) && inBounds(destPos, length, dest.length);
UNSAFE.copyMemory(src, Unsafe.ARRAY_INT_BASE_OFFSET + srcPos * Unsafe.ARRAY_INT_INDEX_SCALE,
dest, Unsafe.ARRAY_INT_BASE_OFFSET + destPos * Unsafe.ARRAY_INT_INDEX_SCALE, Integer.BYTES * length);
}

public static void copyLongs(final long[] src, final long srcPos, final long[] dest, final long destPos, final long length) {
assert 0 <= srcPos && srcPos + length <= src.length && 0 <= destPos && destPos + length <= dest.length;
assert inBounds(srcPos, length, src.length) && inBounds(destPos, length, dest.length);
UNSAFE.copyMemory(src, Unsafe.ARRAY_LONG_BASE_OFFSET + srcPos * Unsafe.ARRAY_LONG_INDEX_SCALE,
dest, Unsafe.ARRAY_LONG_BASE_OFFSET + destPos * Unsafe.ARRAY_LONG_INDEX_SCALE, Long.BYTES * length);
}

public static void copyShorts(final short[] src, final long srcPos, final short[] dest, final long destPos, final long length) {
assert 0 <= srcPos && srcPos + length <= src.length && 0 <= destPos && destPos + length <= dest.length;
assert inBounds(srcPos, length, src.length) && inBounds(destPos, length, dest.length);
UNSAFE.copyMemory(src, Unsafe.ARRAY_SHORT_BASE_OFFSET + srcPos * Unsafe.ARRAY_SHORT_INDEX_SCALE,
dest, Unsafe.ARRAY_SHORT_BASE_OFFSET + destPos * Unsafe.ARRAY_SHORT_INDEX_SCALE, Short.BYTES * length);
}
Expand Down Expand Up @@ -89,7 +87,7 @@ public static boolean getBoolAt(final AbstractPointersObject object, final long
}

public static boolean getBoolFromLongs(final long[] array, final long index) {
assert 0 <= index && index < array.length;
assert inBounds(index, array.length);
return UNSAFE.getBoolean(array, Unsafe.ARRAY_LONG_BASE_OFFSET + index * Unsafe.ARRAY_LONG_INDEX_SCALE);
}

Expand All @@ -99,12 +97,12 @@ public static boolean getBoolFromLongsOffset(final long[] array, final long offs
}

public static byte getByte(final byte[] storage, final long index) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
return UNSAFE.getByte(storage, Unsafe.ARRAY_BYTE_BASE_OFFSET + index * Unsafe.ARRAY_BYTE_INDEX_SCALE);
}

public static char getChar(final char[] storage, final long index) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
return UNSAFE.getChar(storage, Unsafe.ARRAY_CHAR_BASE_OFFSET + index * Unsafe.ARRAY_CHAR_INDEX_SCALE);
}

Expand All @@ -118,7 +116,7 @@ public static char getCharFromLongsOffset(final long[] array, final long offset)
}

public static double getDouble(final double[] storage, final long index) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
return UNSAFE.getDouble(storage, Unsafe.ARRAY_DOUBLE_BASE_OFFSET + index * Unsafe.ARRAY_DOUBLE_INDEX_SCALE);
}

Expand All @@ -132,7 +130,7 @@ public static double getDoubleFromLongsOffset(final long[] array, final long off
}

public static int getInt(final int[] storage, final long index) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
return UNSAFE.getInt(storage, Unsafe.ARRAY_INT_BASE_OFFSET + index * Unsafe.ARRAY_INT_INDEX_SCALE);
}

Expand All @@ -141,7 +139,7 @@ public static int getIntAt(final Object object, final long address) {
}

public static long getLong(final long[] storage, final long index) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
return UNSAFE.getLong(storage, Unsafe.ARRAY_LONG_BASE_OFFSET + index * Unsafe.ARRAY_LONG_INDEX_SCALE);
}

Expand All @@ -155,7 +153,7 @@ public static long getLongOffset(final long[] storage, final long offset) {
}

public static Object getObject(final Object[] storage, final long index) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
return UNSAFE.getObject(storage, Unsafe.ARRAY_OBJECT_BASE_OFFSET + index * Unsafe.ARRAY_OBJECT_INDEX_SCALE);
}

Expand All @@ -178,11 +176,18 @@ public static short getShort(final int[] ints, final long index) {
}

public static short getShort(final short[] storage, final long index) {
assert 0 <= index && index < storage.length;

assert inBounds(index, storage.length);
return UNSAFE.getShort(storage, Unsafe.ARRAY_SHORT_BASE_OFFSET + index * Unsafe.ARRAY_SHORT_INDEX_SCALE);
}

private static boolean inBounds(final long index, final long totalLength) {
return 0 <= index && index < totalLength;
}

private static boolean inBounds(final long start, final long length, final long totalLength) {
return 0 <= start && start + length <= totalLength;
}

private static Unsafe initUnsafe() {
try {
// Fast path when we are trusted.
Expand All @@ -209,12 +214,12 @@ public static void putBoolIntoLongsOffset(final long[] array, final long offset,
}

public static void putByte(final byte[] storage, final long index, final byte value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putByte(storage, Unsafe.ARRAY_BYTE_BASE_OFFSET + index * Unsafe.ARRAY_BYTE_INDEX_SCALE, value);
}

public static void putChar(final char[] storage, final long index, final char value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putChar(storage, Unsafe.ARRAY_CHAR_BASE_OFFSET + index * Unsafe.ARRAY_CHAR_INDEX_SCALE, value);
}

Expand All @@ -228,7 +233,7 @@ public static void putCharIntoLongsOffset(final long[] array, final long offset,
}

public static void putDouble(final double[] storage, final long index, final double value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putDouble(storage, Unsafe.ARRAY_DOUBLE_BASE_OFFSET + index * Unsafe.ARRAY_DOUBLE_INDEX_SCALE, value);
}

Expand All @@ -242,7 +247,7 @@ public static void putDoubleIntoLongsOffset(final long[] array, final long offse
}

public static void putInt(final int[] storage, final long index, final int value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putInt(storage, Unsafe.ARRAY_INT_BASE_OFFSET + index * Unsafe.ARRAY_INT_INDEX_SCALE, value);
}

Expand All @@ -251,7 +256,7 @@ public static void putIntAt(final Object object, final long offset, final int va
}

public static void putLong(final long[] storage, final long index, final long value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putLong(storage, Unsafe.ARRAY_LONG_BASE_OFFSET + index * Unsafe.ARRAY_LONG_INDEX_SCALE, value);
}

Expand All @@ -265,7 +270,7 @@ public static void putLongOffset(final long[] storage, final long offset, final
}

public static void putObject(final Object[] storage, final long index, final Object value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putObject(storage, Unsafe.ARRAY_OBJECT_BASE_OFFSET + index * Unsafe.ARRAY_OBJECT_INDEX_SCALE, value);
}

Expand All @@ -284,7 +289,7 @@ public static void putShort(final int[] ints, final long index, final short valu
}

public static void putShort(final short[] storage, final long index, final short value) {
assert 0 <= index && index < storage.length;
assert inBounds(index, storage.length);
UNSAFE.putShort(storage, Unsafe.ARRAY_SHORT_BASE_OFFSET + index * Unsafe.ARRAY_SHORT_INDEX_SCALE, value);
}

Expand Down

0 comments on commit afa4365

Please sign in to comment.