GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix memoryAddress() for direct ByteBuffers wrapped by Unpooled withou… · netty/netty@f7b1b7d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • EmptyByteBuf.java
      • UnpooledDirectByteBuf.java
    • test/java/io/netty/buffer
      • EmptyByteBufTest.java
      • UnpooledTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class EmptyByteBuf extends ByteBuf {
4646
static {
4747
long emptyByteBufferAddress = 0;
4848
try {
49-
if (PlatformDependent.hasUnsafe()) {
49+
if (PlatformDependent.hasDirectByteBufferAddress(EMPTY_BYTE_BUFFER)) {
5050
emptyByteBufferAddress = PlatformDependent.directBufferAddress(EMPTY_BYTE_BUFFER);
5151
}
5252
} catch (Throwable t) {
Collapse file

‎buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,28 @@ public int arrayOffset() {
230230
@Override
231231
public boolean hasMemoryAddress() {
232232
CleanableDirectBuffer cleanable = this.cleanable;
233-
return cleanable != null && cleanable.hasMemoryAddress();
233+
if (cleanable != null) {
234+
return cleanable.hasMemoryAddress();
235+
}
236+
ByteBuffer buffer = this.buffer;
237+
return buffer != null && PlatformDependent.hasDirectByteBufferAddress(buffer);
234238
}
235239

236240
@Override
237241
public long memoryAddress() {
238242
ensureAccessible();
239-
if (!hasMemoryAddress()) {
240-
throw new UnsupportedOperationException();
243+
CleanableDirectBuffer cleanable = this.cleanable;
244+
if (cleanable != null) {
245+
if (cleanable.hasMemoryAddress()) {
246+
return cleanable.memoryAddress();
247+
}
248+
} else {
249+
ByteBuffer buffer = this.buffer;
250+
if (PlatformDependent.hasDirectByteBufferAddress(buffer)) {
251+
return PlatformDependent.directBufferAddress(buffer);
252+
}
241253
}
242-
return cleanable.memoryAddress();
254+
throw new UnsupportedOperationException();
243255
}
244256

245257
@Override
Collapse file

‎buffer/src/test/java/io/netty/buffer/EmptyByteBufTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/EmptyByteBufTest.java
+24-11Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package io.netty.buffer;
1717

1818
import io.netty.util.CharsetUtil;
19+
import io.netty.util.internal.PlatformDependent;
1920
import org.junit.jupiter.api.Test;
2021

22+
import java.nio.ByteBuffer;
23+
24+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2125
import static org.junit.jupiter.api.Assertions.assertEquals;
2226
import static org.junit.jupiter.api.Assertions.assertFalse;
23-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2427
import static org.junit.jupiter.api.Assertions.assertSame;
2528
import static org.junit.jupiter.api.Assertions.assertThrows;
2629
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -83,16 +86,26 @@ public void testNioBuffer() {
8386
@Test
8487
public void testMemoryAddress() {
8588
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
86-
if (empty.hasMemoryAddress()) {
87-
assertNotEquals(0L, empty.memoryAddress());
88-
} else {
89-
try {
90-
empty.memoryAddress();
91-
fail();
92-
} catch (UnsupportedOperationException ignored) {
93-
// Ignore.
94-
}
95-
}
89+
assertEmptyBufferMemoryAddress(empty);
90+
}
91+
92+
@Test
93+
public void testAllocatorIoBufferMemoryAddress() {
94+
ByteBuf empty = ByteBufAllocator.DEFAULT.ioBuffer(0, 0);
95+
assertEmptyBufferMemoryAddress(empty);
96+
}
97+
98+
private static void assertEmptyBufferMemoryAddress(ByteBuf empty) {
99+
long memoryAddress = directBufferAddress(empty.nioBuffer());
100+
assertTrue(empty.hasMemoryAddress());
101+
assertEquals(memoryAddress, empty.memoryAddress());
102+
}
103+
104+
private static long directBufferAddress(ByteBuffer buffer) {
105+
assumeTrue(PlatformDependent.hasDirectByteBufferAddress(buffer));
106+
long memoryAddress = PlatformDependent.directBufferAddress(buffer);
107+
assumeTrue(memoryAddress != 0);
108+
return memoryAddress;
96109
}
97110

98111
@Test
Collapse file

‎buffer/src/test/java/io/netty/buffer/UnpooledTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/UnpooledTest.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.netty.buffer;
1717

1818
import io.netty.util.CharsetUtil;
19+
import io.netty.util.internal.PlatformDependent;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.function.Executable;
2122
import org.mockito.Mockito;
@@ -33,6 +34,7 @@
3334

3435
import static io.netty.buffer.Unpooled.*;
3536
import static io.netty.util.internal.EmptyArrays.*;
37+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3638
import static org.junit.jupiter.api.Assertions.assertEquals;
3739
import static org.junit.jupiter.api.Assertions.assertFalse;
3840
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -281,6 +283,22 @@ public void testWrappedBuffer() {
281283
ByteBuffer.wrap(new byte[] { 2 }), ByteBuffer.wrap(new byte[] { 3 })));
282284
}
283285

286+
@Test
287+
public void testWrappedDirectBufferMemoryAddress() {
288+
ByteBuffer nioBuffer = ByteBuffer.allocateDirect(16);
289+
nioBuffer.position(4).limit(12);
290+
ByteBuffer expectedBuffer = nioBuffer.slice();
291+
assumeTrue(PlatformDependent.hasDirectByteBufferAddress(expectedBuffer));
292+
293+
ByteBuf buffer = wrappedBuffer(nioBuffer);
294+
try {
295+
assertTrue(buffer.hasMemoryAddress());
296+
assertEquals(PlatformDependent.directBufferAddress(expectedBuffer), buffer.memoryAddress());
297+
} finally {
298+
buffer.release();
299+
}
300+
}
301+
284302
@Test
285303
public void testSingleWrappedByteBufReleased() {
286304
ByteBuf buf = buffer(12).writeByte(0);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.