Skip to content

Commit b7263bb

Browse files
fb64jbonofre
authored andcommitted
fixup! [feat] add arrow-memory-ffm module
Get the ByteBuffer address without duplicating the buffer Use MemorySegment.ofBuffer(buf).address() - buf.position(), so no duplicate ByteBuffer is created (review comment r4207177122).
1 parent 82338d7 commit b7263bb

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,15 @@ public long getLong(byte[] bytes, int index) {
151151
* Returns the address of byte 0 of {@code buf}'s backing memory, independent of {@code buf}'s
152152
* current position.
153153
*
154-
* @implNote {@code MemorySegment.ofBuffer(buf)} only spans {@code [position, limit)}, so its
155-
* address shifts with the position. Callers (notably {@link
154+
* @implNote {@code MemorySegment.ofBuffer(buf)} starts at the buffer's position, so subtracting
155+
* the position gives byte 0 without changing {@code buf}. Callers (notably {@link
156156
* org.apache.arrow.memory.ArrowBuf}) add {@code position()} or an explicit index on top of
157157
* the returned address themselves, matching the {@code sun.misc.Unsafe}-backed accessor,
158-
* which reads the raw {@code java.nio.Buffer.address} field. Clearing a duplicate (rather
159-
* than {@code buf} itself, whose state must not change) resets position to 0 and limit to
160-
* capacity so the segment spans the whole backing buffer from byte 0.
158+
* which reads the raw {@code java.nio.Buffer.address} field.
161159
*/
162160
@Override
163161
public long getByteBufferAddress(ByteBuffer buf) {
164-
return MemorySegment.ofBuffer(buf.duplicate().clear()).address();
162+
return MemorySegment.ofBuffer(buf).address() - buf.position();
165163
}
166164

167165
@Override

‎memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public void getByteBufferAddressIsPositionIndependent() {
126126
ByteBuffer readOnly = buf.asReadOnlyBuffer();
127127
readOnly.position(7);
128128
assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(readOnly));
129+
130+
buf.position(12);
131+
assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
132+
buf.limit(16).position(16);
133+
assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
129134
}
130135

131136
@Test

0 commit comments

Comments
 (0)