Skip to content

Commit

Permalink
8318324: Drop redundant default methods from FFM API
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee
  • Loading branch information
mcimadamore committed Oct 19, 2023
1 parent 1a09835 commit 15acf4b
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 277 deletions.
75 changes: 7 additions & 68 deletions src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@
package java.lang.foreign;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import jdk.internal.foreign.LayoutPath;
Expand All @@ -44,7 +39,6 @@
import jdk.internal.foreign.layout.SequenceLayoutImpl;
import jdk.internal.foreign.layout.StructLayoutImpl;
import jdk.internal.foreign.layout.UnionLayoutImpl;
import jdk.internal.vm.annotation.ForceInline;

/**
* A memory layout describes the contents of a memory segment.
Expand Down Expand Up @@ -404,35 +398,12 @@ public sealed interface MemoryLayout permits SequenceLayout, GroupLayout, Paddin
* @throws IllegalArgumentException if {@code offset} or {@code index} is negative
* @throws ArithmeticException if either the addition or multiplication overflows
*/
@ForceInline
default long scale(long offset, long index) {
if (offset < 0) {
throw new IllegalArgumentException("Negative offset: " + offset);
}
if (index < 0) {
throw new IllegalArgumentException("Negative index: " + index);
}

return Math.addExact(offset, Math.multiplyExact(byteSize(), index));
}
long scale(long offset, long index);

/**
*{@return a method handle that can be used to invoke {@link #scale(long, long)} on this layout}
*/
default MethodHandle scaleHandle() {
class Holder {
static final MethodHandle MH_SCALE;
static {
try {
MH_SCALE = MethodHandles.lookup().findVirtual(MemoryLayout.class, "scale",
MethodType.methodType(long.class, long.class, long.class));
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
}
return Holder.MH_SCALE.bindTo(this);
}
MethodHandle scaleHandle();

/**
* Computes the offset, in bytes, of the layout selected by the given layout path, where the initial layout in the
Expand All @@ -444,10 +415,7 @@ class Holder {
* @throws IllegalArgumentException if the layout path contains one or more <a href=#open-path-elements>open path elements</a>.
* @throws IllegalArgumentException if the layout path contains one or more <a href=#deref-path-elements>dereference path elements</a>.
*/
default long byteOffset(PathElement... elements) {
return computePathOp(LayoutPath.rootPath(this), LayoutPath::offset,
EnumSet.of(PathKind.SEQUENCE_ELEMENT, PathKind.SEQUENCE_RANGE, PathKind.DEREF_ELEMENT), elements);
}
long byteOffset(PathElement... elements);

/**
* Creates a method handle that computes the offset, in bytes, of the layout selected
Expand Down Expand Up @@ -482,10 +450,7 @@ default long byteOffset(PathElement... elements) {
* @throws IllegalArgumentException if the layout path is not <a href="#well-formedness">well-formed</a> for this layout.
* @throws IllegalArgumentException if the layout path contains one or more <a href=#deref-path-elements>dereference path elements</a>.
*/
default MethodHandle byteOffsetHandle(PathElement... elements) {
return computePathOp(LayoutPath.rootPath(this), LayoutPath::offsetHandle,
EnumSet.of(PathKind.DEREF_ELEMENT), elements);
}
MethodHandle byteOffsetHandle(PathElement... elements);

/**
* Creates a var handle that accesses a memory segment at the offset selected by the given layout path,
Expand Down Expand Up @@ -577,14 +542,7 @@ default MethodHandle byteOffsetHandle(PathElement... elements) {
* @throws IllegalArgumentException if the layout path is not <a href="#well-formedness">well-formed</a> for this layout.
* @throws IllegalArgumentException if the layout selected by the provided path is not a {@linkplain ValueLayout value layout}.
*/
default VarHandle varHandle(PathElement... elements) {
Objects.requireNonNull(elements);
if (this instanceof ValueLayout vl && elements.length == 0) {
return vl.varHandle(); // fast path
}
return computePathOp(LayoutPath.rootPath(this), LayoutPath::dereferenceHandle,
Set.of(), elements);
}
VarHandle varHandle(PathElement... elements);

/**
* Creates a method handle which, given a memory segment, returns a {@linkplain MemorySegment#asSlice(long,long) slice}
Expand Down Expand Up @@ -623,10 +581,7 @@ default VarHandle varHandle(PathElement... elements) {
* @throws IllegalArgumentException if the layout path is not <a href="#well-formedness">well-formed</a> for this layout.
* @throws IllegalArgumentException if the layout path contains one or more <a href=#deref-path-elements>dereference path elements</a>.
*/
default MethodHandle sliceHandle(PathElement... elements) {
return computePathOp(LayoutPath.rootPath(this), LayoutPath::sliceHandle,
Set.of(PathKind.DEREF_ELEMENT), elements);
}
MethodHandle sliceHandle(PathElement... elements);

/**
* Returns the layout selected from the provided path, where the initial layout in the path is this layout.
Expand All @@ -638,23 +593,7 @@ default MethodHandle sliceHandle(PathElement... elements) {
* @throws IllegalArgumentException if the layout path contains one or more path elements that select one or more
* sequence element indices, such as {@link PathElement#sequenceElement(long)} and {@link PathElement#sequenceElement(long, long)}).
*/
default MemoryLayout select(PathElement... elements) {
return computePathOp(LayoutPath.rootPath(this), LayoutPath::layout,
EnumSet.of(PathKind.SEQUENCE_ELEMENT_INDEX, PathKind.SEQUENCE_RANGE, PathKind.DEREF_ELEMENT), elements);
}

private static <Z> Z computePathOp(LayoutPath path, Function<LayoutPath, Z> finalizer,
Set<PathKind> badKinds, PathElement... elements) {
Objects.requireNonNull(elements);
for (PathElement e : elements) {
LayoutPath.PathElementImpl pathElem = (LayoutPath.PathElementImpl)Objects.requireNonNull(e);
if (badKinds.contains(pathElem.kind())) {
throw new IllegalArgumentException(String.format("Invalid %s selection in layout path", pathElem.kind().description()));
}
path = pathElem.apply(path);
}
return finalizer.apply(path);
}
MemoryLayout select(PathElement... elements);

/**
* An element in a <a href="MemoryLayout.html#layout-paths"><em>layout path</em></a>. There
Expand Down
Loading

0 comments on commit 15acf4b

Please sign in to comment.