Skip to content

Commit

Permalink
Compute empty byte address at runtime quarkusio#9750
Browse files Browse the repository at this point in the history
  • Loading branch information
galderz committed Jun 5, 2020
1 parent 4182a75 commit 23e8e8f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@

import java.nio.ByteBuffer;

import io.netty.util.internal.PlatformDependent;

public final class EmptyByteBufStub {
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocateDirect(0);
private static final long EMPTY_BYTE_BUFFER_ADDRESS;

static {
long emptyByteBufferAddress = 0;
try {
if (PlatformDependent.hasUnsafe()) {
emptyByteBufferAddress = PlatformDependent.directBufferAddress(EMPTY_BYTE_BUFFER);
}
} catch (Throwable t) {
// Ignore
}
EMPTY_BYTE_BUFFER_ADDRESS = emptyByteBufferAddress;
}

public static ByteBuffer emptyByteBuffer() {
return EMPTY_BYTE_BUFFER;
}

public static long emptyByteBufferAddress() {
return EMPTY_BYTE_BUFFER_ADDRESS;
}

private EmptyByteBufStub() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ final class Target_io_netty_buffer_EmptyByteBuf {
private static ByteBuffer EMPTY_BYTE_BUFFER;

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.FromAlias)
private static long EMPTY_BYTE_BUFFER_ADDRESS = 0;
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)
private static long EMPTY_BYTE_BUFFER_ADDRESS;

@Substitute
public ByteBuffer nioBuffer() {
Expand All @@ -417,6 +417,20 @@ public ByteBuffer internalNioBuffer(int index, int length) {
return EmptyByteBufStub.emptyByteBuffer();
}

@Substitute
public boolean hasMemoryAddress() {
return EmptyByteBufStub.emptyByteBufferAddress() != 0;
}

@Substitute
public long memoryAddress() {
if (hasMemoryAddress()) {
return EmptyByteBufStub.emptyByteBufferAddress();
} else {
throw new UnsupportedOperationException();
}
}

}

class NettySubstitutions {
Expand Down

0 comments on commit 23e8e8f

Please sign in to comment.