Skip to content

Commit f69b601

Browse files
committed
8343188: Investigate ways to simplify MemorySegment::ofBuffer
Reviewed-by: mcimadamore
1 parent 7f131a9 commit f69b601

File tree

9 files changed

+305
-64
lines changed

9 files changed

+305
-64
lines changed

src/java.base/share/classes/java/nio/Buffer.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,11 +37,9 @@
3737
import jdk.internal.util.Preconditions;
3838
import jdk.internal.vm.annotation.ForceInline;
3939

40-
import java.io.FileDescriptor;
4140
import java.lang.foreign.MemorySegment;
4241
import java.lang.ref.Reference;
4342
import java.util.List;
44-
import java.util.Objects;
4543
import java.util.Spliterator;
4644
import java.util.function.BiFunction;
4745
import java.util.function.Function;
@@ -780,6 +778,23 @@ final int checkIndex(int i, int nb) { // package-private
780778
return Preconditions.checkIndex(i, limit - nb + 1, IOOBE_FORMATTER);
781779
}
782780

781+
/**
782+
* {@return the scale shifts for this Buffer}
783+
* <p>
784+
* The scale shifts are:
785+
* ByteBuffer: 0
786+
* ShortBuffer, CharBuffer: 1
787+
* IntBuffer, FloatBuffer: 2
788+
* LongBuffer, DoubleBuffer: 3
789+
*/
790+
abstract int scaleShifts();
791+
792+
abstract AbstractMemorySegmentImpl heapSegment(Object base,
793+
long offset,
794+
long length,
795+
boolean readOnly,
796+
MemorySessionImpl bufferScope);
797+
783798
final int markValue() { // package-private
784799
return mark;
785800
}
@@ -832,6 +847,7 @@ public ByteBuffer newHeapByteBuffer(byte[] hb, int offset, int capacity, MemoryS
832847
return new HeapByteBuffer(hb, -1, 0, capacity, capacity, offset, segment);
833848
}
834849

850+
@ForceInline
835851
@Override
836852
public Object getBufferBase(Buffer buffer) {
837853
return buffer.base();
@@ -906,6 +922,23 @@ public void unreserveMemory(long size, long cap) {
906922
public int pageSize() {
907923
return Bits.pageSize();
908924
}
925+
926+
@ForceInline
927+
@Override
928+
public int scaleShifts(Buffer buffer) {
929+
return buffer.scaleShifts();
930+
}
931+
932+
@ForceInline
933+
@Override
934+
public AbstractMemorySegmentImpl heapSegment(Buffer buffer,
935+
Object base,
936+
long offset,
937+
long length,
938+
boolean readOnly,
939+
MemorySessionImpl bufferScope) {
940+
return buffer.heapSegment(base, offset, length, readOnly, bufferScope);
941+
}
909942
});
910943
}
911944

src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,10 @@
2828
package java.nio;
2929

3030
import java.lang.foreign.MemorySegment;
31+
import jdk.internal.foreign.AbstractMemorySegmentImpl;
32+
import jdk.internal.foreign.MemorySessionImpl;
33+
import jdk.internal.foreign.SegmentFactories;
34+
import jdk.internal.vm.annotation.ForceInline;
3135
import java.util.Objects;
3236
import jdk.internal.misc.Unsafe;
3337

@@ -246,6 +250,21 @@ class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private
246250

247251
#end[char]
248252

253+
@ForceInline
254+
@Override
255+
int scaleShifts() {
256+
return Integer.numberOfTrailingZeros($Fulltype$.BYTES);
257+
}
258+
259+
@ForceInline
260+
@Override
261+
AbstractMemorySegmentImpl heapSegment(Object base,
262+
long offset,
263+
long length,
264+
boolean readOnly,
265+
MemorySessionImpl bufferScope) {
266+
return SegmentFactories.arrayOfByteSegment(base, offset, length, readOnly, bufferScope);
267+
}
249268

250269
public ByteOrder order() {
251270
#if[boB]

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,10 @@ import java.io.FileDescriptor;
3131
import java.lang.foreign.MemorySegment;
3232
import java.lang.ref.Reference;
3333
import java.util.Objects;
34+
import jdk.internal.foreign.AbstractMemorySegmentImpl;
3435
import jdk.internal.foreign.MemorySessionImpl;
36+
import jdk.internal.foreign.SegmentFactories;
37+
import jdk.internal.vm.annotation.ForceInline;
3538
import jdk.internal.misc.ScopedMemoryAccess.ScopedAccessError;
3639
import jdk.internal.misc.VM;
3740
import jdk.internal.ref.Cleaner;
@@ -528,6 +531,24 @@ class Direct$Type$Buffer$RW$$BO$
528531
}
529532
#end[char]
530533

534+
#if[byte]
535+
@ForceInline
536+
@Override
537+
int scaleShifts() {
538+
return 0;
539+
}
540+
541+
@ForceInline
542+
@Override
543+
AbstractMemorySegmentImpl heapSegment(Object base,
544+
long offset,
545+
long length,
546+
boolean readOnly,
547+
MemorySessionImpl bufferScope) {
548+
// Direct buffers are not backed by an array.
549+
throw new UnsupportedOperationException();
550+
}
551+
#end[byte]
531552

532553
#if[byte]
533554
// #BIN

src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
package java.nio;
2929

3030
import java.lang.foreign.MemorySegment;
31+
import jdk.internal.foreign.AbstractMemorySegmentImpl;
32+
import jdk.internal.foreign.MemorySessionImpl;
33+
import jdk.internal.foreign.SegmentFactories;
34+
import jdk.internal.vm.annotation.ForceInline;
3135
import java.util.Objects;
3236

3337
/**
@@ -735,6 +739,23 @@ class Heap$Type$Buffer$RW$
735739

736740
#end[char]
737741

742+
#if[byte]
743+
@ForceInline
744+
@Override
745+
int scaleShifts() {
746+
return 0;
747+
}
748+
749+
@ForceInline
750+
@Override
751+
AbstractMemorySegmentImpl heapSegment(Object base,
752+
long offset,
753+
long length,
754+
boolean readOnly,
755+
MemorySessionImpl bufferScope) {
756+
return SegmentFactories.arrayOf$Type$Segment(base, offset, length, readOnly, bufferScope);
757+
}
758+
#end[byte]
738759

739760
#if[!byte]
740761

src/java.base/share/classes/java/nio/X-Buffer.java.template

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,10 @@ import java.util.stream.StreamSupport;
3737
import java.util.stream.$Streamtype$Stream;
3838
#end[streamableType]
3939

40+
import jdk.internal.foreign.AbstractMemorySegmentImpl;
41+
import jdk.internal.foreign.MemorySessionImpl;
42+
import jdk.internal.foreign.SegmentFactories;
43+
import jdk.internal.vm.annotation.ForceInline;
4044
import java.lang.foreign.MemorySegment;
4145
import java.util.Objects;
4246
import jdk.internal.util.ArraysSupport;
@@ -2321,6 +2325,22 @@ public abstract sealed class $Type$Buffer
23212325

23222326
#end[byte]
23232327

2328+
@ForceInline
2329+
@Override
2330+
int scaleShifts() {
2331+
return Integer.numberOfTrailingZeros($Fulltype$.BYTES);
2332+
}
2333+
2334+
@ForceInline
2335+
@Override
2336+
AbstractMemorySegmentImpl heapSegment(Object base,
2337+
long offset,
2338+
long length,
2339+
boolean readOnly,
2340+
MemorySessionImpl bufferScope) {
2341+
return SegmentFactories.arrayOf$Type$Segment(base, offset, length, readOnly, bufferScope);
2342+
}
2343+
23242344
#if[streamableType]
23252345

23262346
#if[char]

src/java.base/share/classes/jdk/internal/access/JavaNioAccess.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,10 +27,11 @@
2727

2828
import jdk.internal.access.foreign.MappedMemoryUtilsProxy;
2929
import jdk.internal.access.foreign.UnmapperProxy;
30+
import jdk.internal.foreign.AbstractMemorySegmentImpl;
31+
import jdk.internal.foreign.MemorySessionImpl;
3032
import jdk.internal.misc.VM.BufferPool;
3133

3234
import java.lang.foreign.MemorySegment;
33-
import java.io.FileDescriptor;
3435
import java.nio.Buffer;
3536
import java.nio.ByteBuffer;
3637

@@ -127,4 +128,14 @@ public interface JavaNioAccess {
127128
* Used by {@code jdk.internal.foreign.NativeMemorySegmentImpl}.
128129
*/
129130
int pageSize();
131+
132+
int scaleShifts(Buffer buffer);
133+
134+
AbstractMemorySegmentImpl heapSegment(Buffer buffer,
135+
Object base,
136+
long offset,
137+
long length,
138+
boolean readOnly,
139+
MemorySessionImpl bufferScope);
140+
130141
}

0 commit comments

Comments
 (0)