diff --git a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/CLinker.java b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/CLinker.java index 745e27691dd..4ba44bd2dc6 100644 --- a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/CLinker.java +++ b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/CLinker.java @@ -55,7 +55,7 @@ * {@linkplain #downcallHandle(FunctionDescriptor) Linking a foreign function} is a process which requires a function descriptor, * a set of memory layouts which, together, specify the signature of the foreign function to be linked, and returns, * when complete, a downcall method handle, that is, a method handle that can be used to invoke the target native function. - * The Java {@link java.lang.invoke.MethodType method type} associated with the returned method handle is + * The Java {@linkplain java.lang.invoke.MethodType method type} associated with the returned method handle is * {@linkplain #downcallType(FunctionDescriptor) derived} from the argument and return layouts in the function descriptor. * More specifically, given each layout {@code L} in the function descriptor, a corresponding carrier {@code C} is inferred, * as described below: @@ -69,7 +69,8 @@ *
- * The downcall method handle type, derived as above, might be decorated by additional leading parameters: + * The downcall method handle type, derived as above, might be decorated by additional leading parameters, + * in the given order if both are present: *
- * The type of the provided method handle has to match the Java {@link java.lang.invoke.MethodType method type} + * The type of the provided method handle has to match the Java {@linkplain java.lang.invoke.MethodType method type} * associated with the upcall stub, which is derived from the argument and return layouts in the function descriptor. * More specifically, given each layout {@code L} in the function descriptor, a corresponding carrier {@code C} is inferred, as described below: *
* All implementations of this interface must be value-based; * programmers should treat instances that are {@linkplain Object#equals(Object) equal} as interchangeable and should not @@ -157,7 +157,7 @@ * scope, it can only be accessed by the thread which owns the scope. *
* Heap and buffer segments are always associated with a global, shared scope. This scope cannot be closed, - * and can be considered as always alive. + * and segments associated with it can be considered as always alive. * *
- * The returned spliterator effectively allows to slice this segment into disjoint sub-segments, which can then - * be processed in parallel by multiple threads. + * The returned spliterator effectively allows to slice this segment into disjoint {@linkplain #asSlice(long, long) slices}, + * which can then be processed in parallel by multiple threads. * * @param elementLayout the layout to be used for splitting. * @return the element spliterator for this segment @@ -679,7 +679,7 @@ static MemorySegment ofByteBuffer(ByteBuffer bb) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated byte array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. @@ -690,7 +690,7 @@ static MemorySegment ofArray(byte[] arr) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated char array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. @@ -701,7 +701,7 @@ static MemorySegment ofArray(char[] arr) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated short array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. @@ -712,7 +712,7 @@ static MemorySegment ofArray(short[] arr) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated int array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. @@ -723,7 +723,7 @@ static MemorySegment ofArray(int[] arr) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated float array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. @@ -734,7 +734,7 @@ static MemorySegment ofArray(float[] arr) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated long array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. @@ -745,7 +745,7 @@ static MemorySegment ofArray(long[] arr) { /** * Creates a new array memory segment that models the memory associated with a given heap-allocated double array. - * The returned segment's resource scope is set to the {@linkplain ResourceScope#globalScope() global} resource scope. + * The returned segment is associated with the {@linkplain ResourceScope#globalScope() global} resource scope. * * @param arr the primitive array backing the array memory segment. * @return a new array memory segment. diff --git a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SegmentAllocator.java b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SegmentAllocator.java index d2f112c5d37..4e2ad325520 100644 --- a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SegmentAllocator.java +++ b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SegmentAllocator.java @@ -456,7 +456,6 @@ static SegmentAllocator nativeAllocator(ResourceScope scope) { * Returns a native allocator which allocates segments in independent {@linkplain ResourceScope#newImplicitScope() implicit scopes}. * Equivalent to (but likely more efficient than) the following code: * {@snippet lang=java : - * ResourceScope scope = ... * SegmentAllocator implicitAllocator = (size, align) -> MemorySegment.allocateNative(size, align, ResourceScope.newImplicitScope()); * } * diff --git a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/VaList.java b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/VaList.java index 7543dac95d0..1bc449fa907 100644 --- a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/VaList.java +++ b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/VaList.java @@ -59,7 +59,8 @@ sealed public interface VaList extends Addressable permits WinVaList, SysVVaList, LinuxAArch64VaList, MacOsAArch64VaList, SharedUtils.EmptyVaList { /** - * Reads the next value as an {@code int} and advances this variable argument list's position. + * Reads the next value as an {@code int} and advances this variable argument list's position. The behavior of this + * method is equivalent to the C {@code va_arg} function. * * @param layout the layout of the value to be read. * @return the {@code int} value read from this variable argument list. @@ -69,7 +70,8 @@ sealed public interface VaList extends Addressable permits WinVaList, SysVVaList int nextVarg(ValueLayout.OfInt layout); /** - * Reads the next value as a {@code long} and advances this variable argument list's position. + * Reads the next value as a {@code long} and advances this variable argument list's position. The behavior of this + * method is equivalent to the C {@code va_arg} function. * * @param layout the layout of the value to be read. * @return the {@code long} value read from this variable argument list. @@ -79,7 +81,8 @@ sealed public interface VaList extends Addressable permits WinVaList, SysVVaList long nextVarg(ValueLayout.OfLong layout); /** - * Reads the next value as a {@code double} and advances this variable argument list's position. + * Reads the next value as a {@code double} and advances this variable argument list's position. The behavior of this + * method is equivalent to the C {@code va_arg} function. * * @param layout the layout of the value * @return the {@code double} value read from this variable argument list. @@ -89,7 +92,8 @@ sealed public interface VaList extends Addressable permits WinVaList, SysVVaList double nextVarg(ValueLayout.OfDouble layout); /** - * Reads the next value as a {@code MemoryAddress} and advances this variable argument list's position. + * Reads the next value as a {@code MemoryAddress} and advances this variable argument list's position. The behavior of this + * method is equivalent to the C {@code va_arg} function. * * @param layout the layout of the value to be read. * @return the {@code MemoryAddress} value read from this variable argument list. @@ -99,7 +103,13 @@ sealed public interface VaList extends Addressable permits WinVaList, SysVVaList MemoryAddress nextVarg(ValueLayout.OfAddress layout); /** - * Reads the next value as a {@code MemorySegment}, and advances this variable argument list's position. + * Reads the next value as a {@code MemorySegment}, and advances this variable argument list's position. The behavior of this + * method is equivalent to the C {@code va_arg} function. The provided group layout must correspond to a C struct or union + * type. + *
+ * How the value is read in the returned segment is ABI-dependent: calling this method on a group layout + * with member layouts {@code L_1, L_2, ... L_n} is not guaranteed to be semantically equivalent to perform distinct + * calls to {@code nextVarg} for each of the layouts in {@code L_1, L_2, ... L_n}. *
* The memory segment returned by this method will be allocated using the given {@link SegmentAllocator}. * @@ -129,9 +139,12 @@ sealed public interface VaList extends Addressable permits WinVaList, SysVVaList /** * Copies this variable argument list at its current position into a new variable argument list associated - * with the same scope as this variable argument list. Copying is useful to - * traverse the variable argument list elements, starting from the current position, without affecting the state - * of the original variable argument list, essentially allowing the elements to be traversed multiple times. + * with the same scope as this variable argument list. The behavior of this method is equivalent to the C + * {@code va_copy} function. + *
+ * Copying is useful to traverse the variable argument list elements, starting from the current position,
+ * without affecting the state of the original variable argument list, essentially allowing the elements to be
+ * traversed multiple times.
*
* @return a copy of this variable argument list.
* @throws IllegalStateException if the scope associated with this variable argument list has been closed, or if access occurs from
@@ -187,8 +200,8 @@ static VaList ofAddress(MemoryAddress address, ResourceScope scope) {
* of the underlying variable argument list.
* @param scope scope the scope to be associated with the new variable arity list.
* @return a new variable argument list.
- * @throws IllegalStateException if the scope associated with {@code allocator} has been already closed,
- * or if access occurs from a thread other than the thread owning that scope.
+ * @throws IllegalStateException if {@code scope} has been already closed, or if access occurs from a thread other
+ * than the thread owning {@code scope}.
*/
static VaList make(Consumer
diff --git a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/package-info.java b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/package-info.java
index a6d140e4266..3621495d064 100644
--- a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/package-info.java
+++ b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/package-info.java
@@ -86,7 +86,7 @@
*
* For example, to compute the length of a string using the C standard library function {@code strlen} on a Linux x64 platform,
* we can use the following code:
@@ -122,7 +122,7 @@
* }
*
* Here, we obtain a {@linkplain jdk.incubator.foreign.CLinker#systemCLinker() linker instance} and we use it
- * to {@linkplain jdk.incubator.foreign.CLinker#lookup(java.lang.String) lookup} the {@code strlen} symbol in the
+ * to {@linkplain jdk.incubator.foreign.CLinker#lookup(java.lang.String) look up} the {@code strlen} symbol in the
* standard C library; a downcall method handle targeting said symbol is subsequently
* {@linkplain jdk.incubator.foreign.CLinker#downcallHandle(jdk.incubator.foreign.FunctionDescriptor) obtained}.
* To complete the linking successfully, we must provide a {@link jdk.incubator.foreign.FunctionDescriptor} instance,
@@ -138,9 +138,9 @@
*
* Raw pointers are modelled using the {@link jdk.incubator.foreign.MemoryAddress} class. When clients receive a
* memory address instance from a foreign function call, they can perform memory dereference on it directly,
@@ -167,7 +167,7 @@
* }
*
*
* Now that we have a method handle instance, we can turn it into a fresh function pointer,
* using the {@link jdk.incubator.foreign.CLinker} interface, as follows:
Safety
*
* This API provides strong safety guarantees when it comes to memory access. First, when dereferencing a memory segment,
- * the access coordinates are validated (upon access), to make sure that access does not occur at an address which resides
+ * the access coordinates are validated (upon access), to make sure that access does not occur at any address which resides
* outside the boundaries of the memory segment used by the dereference operation. We call this guarantee spatial safety;
* in other words, access to memory segments is bounds-checked, in the same way as array access is, as described in
* Section {@jls 15.10.4} of The Java Language Specification.
@@ -99,10 +99,10 @@
* Foreign function access
* The key abstractions introduced to support foreign function access are {@link jdk.incubator.foreign.SymbolLookup},
* {@link jdk.incubator.foreign.MemoryAddress} and {@link jdk.incubator.foreign.CLinker}.
- * The first is used to lookup symbols inside native libraries; the second is used to model native addresses (more on that later),
+ * The first is used to look up symbols inside native libraries; the second is used to model native addresses (more on that later),
* while the third provides linking capabilities which allows modelling foreign functions as {@link java.lang.invoke.MethodHandle} instances,
* so that clients can perform foreign function calls directly in Java, without the need for intermediate layers of native
- * code (as it's the case with the Java Native Interface (JNI)).
+ * code (as is the case with the Java Native Interface (JNI)).
* Foreign addresses
*
* When a memory segment is created from Java code, the segment properties (spatial bounds, temporal bounds and confinement)
- * are fully known at segment creation. But when interacting with native libraries, clients will often receive raw pointers;
- * such pointers have no spatial bounds (example: does the C type {@code char*} refer to a single {@code char} value,
- * or an array of {@code char} values, of given size?), no notion of temporal bounds, nor thread-confinement.
+ * are fully known at segment creation. But when interacting with native libraries, clients will often receive raw pointers.
+ * Such pointers have no spatial bounds. For example, the C type {@code char*} can refer to a single {@code char} value,
+ * or an array of {@code char} values, of given size. Nor do said pointers have any notion of temporal bounds or thread-confinement.
* Upcalls
- * The {@link jdk.incubator.foreign.CLinker} interface also allows to turn an existing method handle (which might point
+ * The {@link jdk.incubator.foreign.CLinker} interface also allows clients to turn an existing method handle (which might point
* to a Java method) into a memory address, so that Java code can effectively be passed to other foreign functions.
* For instance, we can write a method that compares two integer values, as follows:
*
@@ -193,7 +193,7 @@
* As before, we need to create a {@link jdk.incubator.foreign.FunctionDescriptor} instance, this time describing the signature
* of the function pointer we want to create. The descriptor can be used to
* {@linkplain jdk.incubator.foreign.CLinker#upcallType(jdk.incubator.foreign.FunctionDescriptor) derive} a method type
- * that can be used to lookup the method handle for {@code IntComparator.intCompare}.
+ * that can be used to look up the method handle for {@code IntComparator.intCompare}.
*