From 8ae59c1e7b1f69272b12f33e6a0936749e9c98c3 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Thu, 25 Mar 2021 11:38:18 +0100 Subject: [PATCH] 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. --- .../util/internal/PlatformDependent.java | 56 +++++++++++++++++++ .../util/internal/PlatformDependent0.java | 46 ++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index 4cc2aac23e..e731023981 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -416,10 +416,34 @@ public final class PlatformDependent { 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) { 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) { return PlatformDependent0.getIntVolatile(address); } @@ -592,6 +616,30 @@ public final class PlatformDependent { 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) { PlatformDependent0.putShort(data, index, value); } @@ -625,6 +673,10 @@ public final class PlatformDependent { 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) { 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); } + 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) { PlatformDependent0.setMemory(address, bytes, value); } diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent0.java b/common/src/main/java/io/netty/util/internal/PlatformDependent0.java index abc55fd795..991b55e9e1 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent0.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent0.java @@ -485,14 +485,34 @@ final class PlatformDependent0 { 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) { 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); } + static double getDouble(Object object, long fieldOffset) { + return UNSAFE.getDouble(object, fieldOffset); + } + static long objectFieldOffset(Field field) { return UNSAFE.objectFieldOffset(field); } @@ -577,14 +597,38 @@ final class PlatformDependent0 { 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) { 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) { 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) { UNSAFE.putObject(o, offset, x); }