Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(java): use Platform.BYTE_ARRAY_OFFSET replace MemoryBuffer.BYTE_ARRAY_BASE_OFFSET #1414

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@
public final class MemoryBuffer {
// The unsafe handle for transparent memory copied (heap/off-heap).
private static final sun.misc.Unsafe UNSAFE = Platform.UNSAFE;
// The beginning of the byte array contents, relative to the byte array object.
// Note: this offset will change between graalvm build time and runtime.
private static final long BYTE_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
// Constant that flags the byte order. Because this is a boolean constant, the JIT compiler can
// use this well to aggressively eliminate the non-applicable code paths.
private static final boolean LITTLE_ENDIAN = (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN);
Expand Down Expand Up @@ -117,7 +114,7 @@ private void initHeapBuffer(byte[] buffer, int offset, int length) {
}
this.heapMemory = buffer;
this.heapOffset = offset;
final long startPos = BYTE_ARRAY_BASE_OFFSET + offset;
final long startPos = Platform.BYTE_ARRAY_OFFSET + offset;
this.address = startPos;
this.size = length;
this.addressLimit = startPos + length;
Expand Down Expand Up @@ -293,7 +290,7 @@ public void get(int index, byte[] dst, int offset, int length) {
}
final long pos = address + index;
if (index >= 0 && pos <= addressLimit - length) {
final long arrayAddress = BYTE_ARRAY_BASE_OFFSET + offset;
final long arrayAddress = Platform.BYTE_ARRAY_OFFSET + offset;
Platform.copyMemory(heapMemory, pos, dst, arrayAddress, length);
} else {
// index is in fact invalid
Expand Down Expand Up @@ -439,7 +436,7 @@ public void put(int index, byte[] src, int offset, int length) {
}
final long pos = address + index;
if (index >= 0 && pos <= addressLimit - length) {
final long arrayAddress = BYTE_ARRAY_BASE_OFFSET + offset;
final long arrayAddress = Platform.BYTE_ARRAY_OFFSET + offset;
Platform.copyMemory(src, arrayAddress, heapMemory, pos, length);
} else {
// index is in fact invalid
Expand Down Expand Up @@ -1978,7 +1975,7 @@ public void grow(int neededSize) {
public void ensure(int length) {
if (length > size) {
byte[] data = new byte[length * 2];
copyToUnsafe(0, data, BYTE_ARRAY_BASE_OFFSET, size());
copyToUnsafe(0, data, Platform.BYTE_ARRAY_OFFSET, size());
initHeapBuffer(data, 0, data.length);
}
}
Expand Down Expand Up @@ -2139,7 +2136,7 @@ public void readBytes(byte[] dst, int dstIndex, int length) {
if (dstIndex > dst.length - length) {
throw new IndexOutOfBoundsException();
}
copyToUnsafe(readerIdx, dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, length);
copyToUnsafe(readerIdx, dst, Platform.BYTE_ARRAY_OFFSET + dstIndex, length);
readerIndex = readerIdx + length;
}

Expand Down Expand Up @@ -2378,7 +2375,7 @@ public byte[] getBytes(int index, int length) {
throw new IllegalArgumentException();
}
byte[] data = new byte[length];
copyToUnsafe(index, data, BYTE_ARRAY_BASE_OFFSET, length);
copyToUnsafe(index, data, Platform.BYTE_ARRAY_OFFSET, length);
return data;
}

Expand All @@ -2390,7 +2387,7 @@ public void getBytes(int index, byte[] dst, int dstIndex, int length) {
throw new IndexOutOfBoundsException(
String.format("offset(%d) + length(%d) exceeds size(%d): %s", index, length, size, this));
}
copyToUnsafe(index, dst, BYTE_ARRAY_BASE_OFFSET + dstIndex, length);
copyToUnsafe(index, dst, Platform.BYTE_ARRAY_OFFSET + dstIndex, length);
}

public MemoryBuffer slice(int offset) {
Expand Down
Loading