-
CSR
-
Resolution: Approved
-
P2
-
None
-
behavioral
-
minimal
-
Change to an API that was finalized in this release. Changes are not in a central part of the API. Comparing scopes of different segments is very much a corner use case.
-
Java API
Summary
The scope instances associated with segments created from Java array or Buffer
s should be equal if the segments refer to the same underlying object. Moreover, all zero-length memory segments should be associated with the same scope (the global scope).
Problem
When two memory segments wrap the same Java array (using the MemorySegment::ofArray
factory), the segment scopes are not equal, as each segment is associated with a fresh scope instance. This can lead to confusion, as both segment share the same lifetime; as such, it would be desirable for the two scope instances to be equal. The same issue exists for memory segments created from a Buffer
using the MemorySegment::ofBuffer
factory, and also for memory segments returned by the loader lookup (SymbolLookup::loaderLookup
).
Moreover, a fresh scope is also created for zero-length memory segments. That is, every time a native addressed is wrapped into a segment by the Java runtime, a new scope is created. This ensures that the scopes of two segments obtained unsafely from native code are never considered equal. While this is pedantically correct (after all, the underlying lifetime of the externally allocated region of memory is unknown), creating a fresh scope seems overkill: after all, this fresh scope is just a transient scope given to the segment by the API, and clients are unlikely to use this scope directly. To achieve extra safety, zero-length memory segments will be given a new scope using MemorySegment::reinterpret
.
Solution
First we propose to change the implementation of MemorySegment.Scope::equals to return true when both scopes refer to the same underlying Java array or Buffer
. That is, if a client compares the scope of two buffer/array segments that are created from the same buffer/array, the two segment scopes will be considered equal.
Secondly, we propose to change the scope of zero-length memory segments to be a singleton, namely the global scope.
(Note: for zero-length memory segment scopes, we also considered using a singleton whose equals to always return false, but this was ruled out as it violates the general contract of Object::equals
, which says that x == x
implies x.equals(x)
)
Finally, the documentation of both MemorySegment.Scope
and Arena
was improved. Now MemorySegment.Scope
introduces the concept of global and automatic scope. These concepts are helpful as we can then say e.g. that the scope of a zero-length memory segment is the global scope. Or that the scope of an array segment is an automatic scope. Also, the specification of Arena
has been refined, to say that e.g. the scope of an automatic arena is an automatic scope.
Specification
Javadoc (v1): https://cr.openjdk.org/~mcimadamore/jdk/8317050/v1/javadoc/java.base/module-summary.html
Specdiff (v1): https://cr.openjdk.org/~mcimadamore/jdk/8317050/v1/specdiff_out/overview-summary.html
I've also attached a zipped version of the specdiff (v1).
- csr of
-
JDK-8317819 Scope should reflect lifetime of underying resource (mainline)
-
- Resolved
-