Expose on/off heap agnostic Unsafe accessor methods

Motivation:
These are necessary for creating a buffer implementation that uses Unsafe, and works generically for both on-heap and off-heap memory.

Modification:
PlatformDependent previously forced clients to decide if they are working on on-heap memory, or off-heap memory, by giving accessors distinct APIs for each.
What is added here, are generic accessors that work the same in either case.

Result:
We can now make an Unsafe-based buffer implementation that is agnostic to whether the memory is on- or off-heap.
This commit is contained in:
Chris Vest 2021-03-25 11:38:18 +01:00
parent e3086d50e9
commit 8ae59c1e7b
2 changed files with 101 additions and 1 deletions

View File

@ -416,10 +416,34 @@ public final class PlatformDependent {
return PlatformDependent0.getObject(object, fieldOffset); return PlatformDependent0.getObject(object, fieldOffset);
} }
public static byte getByte(Object object, long fieldOffset) {
return PlatformDependent0.getByte(object, fieldOffset);
}
public static short getShort(Object object, long fieldOffset) {
return PlatformDependent0.getShort(object, fieldOffset);
}
public static char getChar(Object object, long fieldOffset) {
return PlatformDependent0.getChar(object, fieldOffset);
}
public static int getInt(Object object, long fieldOffset) { public static int getInt(Object object, long fieldOffset) {
return PlatformDependent0.getInt(object, fieldOffset); return PlatformDependent0.getInt(object, fieldOffset);
} }
public static float getFloat(Object object, long fieldOffset) {
return PlatformDependent0.getFloat(object, fieldOffset);
}
public static long getLong(Object object, long fieldOffset) {
return PlatformDependent0.getLong(object, fieldOffset);
}
public static double getDouble(Object object, long fieldOffset) {
return PlatformDependent0.getDouble(object, fieldOffset);
}
public static int getIntVolatile(long address) { public static int getIntVolatile(long address) {
return PlatformDependent0.getIntVolatile(address); return PlatformDependent0.getIntVolatile(address);
} }
@ -592,6 +616,30 @@ public final class PlatformDependent {
PlatformDependent0.putByte(data, offset, value); PlatformDependent0.putByte(data, offset, value);
} }
public static void putShort(Object data, int offset, short value) {
PlatformDependent0.putShort(data, offset, value);
}
public static void putChar(Object data, int offset, char value) {
PlatformDependent0.putChar(data, offset, value);
}
public static void putInt(Object data, int offset, int value) {
PlatformDependent0.putInt(data, offset, value);
}
public static void putFloat(Object data, int offset, float value) {
PlatformDependent0.putFloat(data, offset, value);
}
public static void putLong(Object data, int offset, long value) {
PlatformDependent0.putLong(data, offset, value);
}
public static void putDouble(Object data, int offset, double value) {
PlatformDependent0.putDouble(data, offset, value);
}
public static void putShort(byte[] data, int index, short value) { public static void putShort(byte[] data, int index, short value) {
PlatformDependent0.putShort(data, index, value); PlatformDependent0.putShort(data, index, value);
} }
@ -625,6 +673,10 @@ public final class PlatformDependent {
dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, length); dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, length);
} }
public static void copyMemory(Object src, long srcOffset, Object dst, long dstOffset, long length) {
PlatformDependent0.copyMemory(src, srcOffset, dst, dstOffset, length);
}
public static void copyMemory(long srcAddr, byte[] dst, int dstIndex, long length) { public static void copyMemory(long srcAddr, byte[] dst, int dstIndex, long length) {
PlatformDependent0.copyMemory(null, srcAddr, dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, length); PlatformDependent0.copyMemory(null, srcAddr, dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, length);
} }
@ -633,6 +685,10 @@ public final class PlatformDependent {
PlatformDependent0.setMemory(dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, bytes, value); PlatformDependent0.setMemory(dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, bytes, value);
} }
public static void setMemory(Object base, long offset, long length, byte value) {
PlatformDependent0.setMemory(base, offset, length, value);
}
public static void setMemory(long address, long bytes, byte value) { public static void setMemory(long address, long bytes, byte value) {
PlatformDependent0.setMemory(address, bytes, value); PlatformDependent0.setMemory(address, bytes, value);
} }

View File

@ -485,14 +485,34 @@ final class PlatformDependent0 {
return UNSAFE.getObject(object, fieldOffset); return UNSAFE.getObject(object, fieldOffset);
} }
static byte getByte(Object object, long fieldOffset) {
return UNSAFE.getByte(object, fieldOffset);
}
static short getShort(Object object, long fieldOffset) {
return UNSAFE.getShort(object, fieldOffset);
}
static char getChar(Object object, long fieldOffset) {
return UNSAFE.getChar(object, fieldOffset);
}
static int getInt(Object object, long fieldOffset) { static int getInt(Object object, long fieldOffset) {
return UNSAFE.getInt(object, fieldOffset); return UNSAFE.getInt(object, fieldOffset);
} }
private static long getLong(Object object, long fieldOffset) { static float getFloat(Object object, long fieldOffset) {
return UNSAFE.getFloat(object, fieldOffset);
}
static long getLong(Object object, long fieldOffset) {
return UNSAFE.getLong(object, fieldOffset); return UNSAFE.getLong(object, fieldOffset);
} }
static double getDouble(Object object, long fieldOffset) {
return UNSAFE.getDouble(object, fieldOffset);
}
static long objectFieldOffset(Field field) { static long objectFieldOffset(Field field) {
return UNSAFE.objectFieldOffset(field); return UNSAFE.objectFieldOffset(field);
} }
@ -577,14 +597,38 @@ final class PlatformDependent0 {
UNSAFE.putShort(data, BYTE_ARRAY_BASE_OFFSET + index, value); UNSAFE.putShort(data, BYTE_ARRAY_BASE_OFFSET + index, value);
} }
static void putShort(Object data, long offset, short value) {
UNSAFE.putShort(data, offset, value);
}
static void putChar(Object data, long offset, char value) {
UNSAFE.putChar(data, offset, value);
}
static void putInt(byte[] data, int index, int value) { static void putInt(byte[] data, int index, int value) {
UNSAFE.putInt(data, BYTE_ARRAY_BASE_OFFSET + index, value); UNSAFE.putInt(data, BYTE_ARRAY_BASE_OFFSET + index, value);
} }
static void putInt(Object data, long offset, int value) {
UNSAFE.putInt(data, offset, value);
}
static void putFloat(Object data, long offset, float value) {
UNSAFE.putFloat(data, offset, value);
}
static void putLong(byte[] data, int index, long value) { static void putLong(byte[] data, int index, long value) {
UNSAFE.putLong(data, BYTE_ARRAY_BASE_OFFSET + index, value); UNSAFE.putLong(data, BYTE_ARRAY_BASE_OFFSET + index, value);
} }
static void putLong(Object data, long offset, long value) {
UNSAFE.putLong(data, offset, value);
}
static void putDouble(Object data, long offset, double value) {
UNSAFE.putDouble(data, offset, value);
}
static void putObject(Object o, long offset, Object x) { static void putObject(Object o, long offset, Object x) {
UNSAFE.putObject(o, offset, x); UNSAFE.putObject(o, offset, x);
} }