Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
85b1246
Initial (hacky) pass.
mcimadamore Jan 14, 2021
0d52da8
Cleanup VaList implementation
mcimadamore Jan 14, 2021
911d739
Fix benchmarks
mcimadamore Jan 14, 2021
9c8799a
All tests pass
mcimadamore Jan 15, 2021
b0790bd
Cleanup scope logic - pull resource list into its own abstraction
mcimadamore Jan 15, 2021
5628993
Add NativeAllocator API.
mcimadamore Jan 18, 2021
f2f6946
Add initial implementation of ResourceScope::fork
mcimadamore Jan 18, 2021
87cfd70
Improve implementation of NativeScope
mcimadamore Jan 18, 2021
e71a193
Remove access modes
mcimadamore Jan 18, 2021
a46a480
Add biggie test for ResourceScope::fork
mcimadamore Jan 18, 2021
7757c10
Switch default for MemorySegment::allocateNative to be cleaner-enabled.
mcimadamore Jan 18, 2021
5dba87d
Simplify implementation of LibrariesHelper to use ResourceScope
mcimadamore Jan 19, 2021
1a4a8ef
Add support for non-closeable ResourceScopes
mcimadamore Jan 20, 2021
7ba07cc
First stab at javadoc
mcimadamore Jan 20, 2021
77de618
Flip defaults for short native/mapped segment factories.
mcimadamore Jan 21, 2021
63fef62
Fix NPE assertions on ResourceScope
mcimadamore Jan 21, 2021
2773f15
Fix MemoryScope javadoc
mcimadamore Jan 21, 2021
9a2e417
Tweak more defaults - now short segment factories return shared, non-…
mcimadamore Jan 21, 2021
443e166
Simplify implementation for VaList::read
mcimadamore Jan 22, 2021
16b1032
Clarify implementation of valists, by accepting a single scope.
mcimadamore Jan 22, 2021
7dc2e93
Remove ad-hoc liveness check segment from Windows VaList impl
mcimadamore Jan 22, 2021
cd89bc3
Simplify VaList API.
mcimadamore Jan 22, 2021
8b512df
Tweak benchmark after VaList changes
mcimadamore Jan 22, 2021
980544d
Add implementations for recycling/malloc/arena allocators.
mcimadamore Jan 22, 2021
1a033e8
Split ResourceScope.fork() into lock() and addOnClose().
mcimadamore Jan 22, 2021
97a60c9
Do not call cleanup action if addOnClose fails because of closed scope
mcimadamore Jan 22, 2021
475eb13
Simplify MemoryScope implementation
mcimadamore Jan 25, 2021
4e9a06f
Add extra documentation on MemoryScope
mcimadamore Jan 25, 2021
525ebbc
Fix javadoc
mcimadamore Jan 25, 2021
145b122
Remove spurious debug print statement
mcimadamore Jan 25, 2021
ae9a8e4
Minor javadoc tweaks
mcimadamore Jan 25, 2021
a0e9151
Fix javadoc of ResourceScope::close
mcimadamore Jan 25, 2021
689c94b
Another javadoc fix
mcimadamore Jan 25, 2021
fb226da
Tweak API for recycling allocator.
mcimadamore Jan 25, 2021
2847f8b
Centralize testing for all scoped entities
mcimadamore Jan 26, 2021
87d12dc
Generalize NativeAllocator to cover heap segments too (->SegmentAlloc…
mcimadamore Jan 28, 2021
2aaf78b
* Remove SharedUtils.Allocator
mcimadamore Jan 29, 2021
da936ac
Add SegmentAllocator support to CLinker
mcimadamore Jan 29, 2021
cb762d7
Enhance and fix the CallOverhead benchmark:
mcimadamore Feb 4, 2021
c96fd3d
Merge branch 'foreign-memaccess+abi' into resourceScope
mcimadamore Feb 19, 2021
bbccc3a
Fix most test failures
mcimadamore Feb 19, 2021
2fb8a8f
ALl tests pass
mcimadamore Feb 19, 2021
db0220f
Fix adaptation type comments
mcimadamore Feb 19, 2021
8e564e5
Add benchmark for strlen with recyclig scope
mcimadamore Feb 19, 2021
16cec3f
Tweak logic by which CLinker adds a SegmentAllocator argument
mcimadamore Feb 19, 2021
14d4956
Updated CLinker javadoc
mcimadamore Feb 19, 2021
2b584e4
Rename allocator factories, and add a factory which takes a scope.
mcimadamore Feb 19, 2021
9d60bd9
Fix javadoc in SegmentAllocator
mcimadamore Feb 19, 2021
35968a7
Remove linker implementation dependencies on NativeScope
mcimadamore Feb 19, 2021
0c37bf3
Fix javadoc issue in CLinker
mcimadamore Feb 19, 2021
3af1f54
Remove NativeScope
mcimadamore Feb 24, 2021
8b2894b
Clarify javadoc of ResourceScope::Lock
mcimadamore Feb 24, 2021
941edbc
Simplify arena allocators
mcimadamore Feb 25, 2021
04de173
Add useful overloads in CLinker:
mcimadamore Feb 26, 2021
a0a9dc7
Fix benchmark for implicit deallocation
mcimadamore Feb 26, 2021
9fa07a7
Simplify ResourceScope API by removing closeable bit.
mcimadamore Feb 26, 2021
67e1831
Merge branch 'foreign-memaccess+abi' into resourceScope
mcimadamore Mar 1, 2021
3f7b263
Fix some issues in upcall handler (Jorn)
mcimadamore Mar 1, 2021
ceb0774
Fix Windows VaList to add eager scope check on creation/copy
mcimadamore Mar 1, 2021
91e2794
Fix issues with GC cleaned upcall
mcimadamore Mar 1, 2021
167e25f
Fix TestResourceScope
mcimadamore Mar 1, 2021
fec4b65
Add overload for restricted VaList factory which takes a scope
mcimadamore Mar 1, 2021
321b389
Remove comments from TestDowncall
mcimadamore Mar 2, 2021
91ed099
Add assertion when functions are void
mcimadamore Mar 2, 2021
16f983a
Slightly clarify ProgrammableInvoker::specialize
mcimadamore Mar 2, 2021
092eca2
Fix javadoc
mcimadamore Mar 3, 2021
3e1399b
Rename ResourceScope::Lock
mcimadamore Mar 4, 2021
bf0ee80
Fix whitespaces
mcimadamore Mar 4, 2021
d04ee60
Fix issue in ResourceScope::ofShared() javadoc
mcimadamore Mar 5, 2021
5db5e3b
Address javadoc issues in review comments
mcimadamore Mar 10, 2021
df7eb2b
Address more review comments on MemoryScope/ResourceList
mcimadamore Mar 11, 2021
472b214
Address more review comments (VaList/SharedUtils/tests)
mcimadamore Mar 11, 2021
9d1ba85
Revert changes to SharedUtils::adaptDowncall
mcimadamore Mar 11, 2021
6328866
Cleanup SharedUtils::wrapWithAllocator and Binding.Context
mcimadamore Mar 11, 2021
69c084f
Improve arena allocator implementation:
mcimadamore Mar 11, 2021
924f8e5
Refactor MemoryScope and ResourceList hierarchies
mcimadamore Mar 11, 2021
465df11
Clarify javadoc for SegmentAllocator::arenaUnbounded
mcimadamore Mar 11, 2021
5710a35
Remove spurious println
mcimadamore Mar 11, 2021
d2c6b68
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 12, 2021
65e5d63
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 12, 2021
8335568
Update src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/A…
mcimadamore Mar 12, 2021
f3741fc
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 12, 2021
81c4d9b
Update src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/C…
mcimadamore Mar 12, 2021
b5bdc61
Update src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/C…
mcimadamore Mar 12, 2021
285644a
Update src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/C…
mcimadamore Mar 12, 2021
9c5af18
Address review comments
mcimadamore Mar 12, 2021
c19b04a
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 12, 2021
5592426
Merge branch 'resourceScope' of https://github.com/mcimadamore/panama…
mcimadamore Mar 12, 2021
8545160
Address rest of review comments from Paul
mcimadamore Mar 12, 2021
195ba0f
Fix javadoc of SegmentAllocator
mcimadamore Mar 12, 2021
d02e86f
Add ResourceScope overloads in CLinker
mcimadamore Mar 18, 2021
0ea68e1
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 19, 2021
697d179
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 19, 2021
15431e7
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 19, 2021
79dde96
Update src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/…
mcimadamore Mar 19, 2021
5de033e
* More javadoc fixes
mcimadamore Mar 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
* <p><a id = "var-symbols"></a></p>
* In cases where a client wants to create a memory segment out of a lookup symbol, the client might want to attach the
* lookup symbol to the newly created segment, so that the symbol will be kept reachable as long as the memory segment
* is reachable; this can be achieved by creating the segment using the {@link MemoryAddress#asSegmentRestricted(long, Runnable, Object)}
* is reachable; this can be achieved by creating the segment using the {@link MemoryAddress#asSegmentRestricted(long, ResourceScope)}.
* restricted segment factory, as follows:
* <pre>{@code
LibraryLookup defaultLookup = LibraryLookup.defaultLookup();
LibraryLookup.Symbol errno = defaultLookup.lookup("errno");
MemorySegment errnoSegment = errno.address().asRestrictedSegment(4, errno);
MemorySegment errnoSegment = errno.address().asSegmentRestricted(4, ResourceScope.ofShared(errno, Cleaner.create()));
* }</pre>
* <p>
* To allow for a library to be unloaded, a client will have to discard any strong references it
Expand Down Expand Up @@ -82,7 +82,7 @@ interface Symbol extends Addressable {

/**
* The memory address of this lookup symbol. If the memory associated with this symbol needs to be dereferenced,
* clients can obtain a segment from this symbol's address using the {@link MemoryAddress#asSegmentRestricted(long, Runnable, Object)},
* clients can obtain a segment from this symbol's address using the {@link MemoryAddress#asSegmentRestricted(long, Runnable, ResourceScope)},
* and making sure that the created segment maintains a <a href="LibraryLookup.html#var-symbols">strong reference</a> to this symbol, to prevent library unloading.
* @return the memory address of this lookup symbol.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* Clients requiring sophisticated, low-level control over mapped memory segments, should consider writing
* custom mapped memory segment factories; using JNI, e.g. on Linux, it is possible to call {@code mmap}
* with the desired parameters; the returned address can be easily wrapped into a memory segment, using
* {@link MemoryAddress#ofLong(long)} and {@link MemoryAddress#asSegmentRestricted(long, Runnable, Object)}.
* {@link MemoryAddress#ofLong(long)} and {@link MemoryAddress#asSegmentRestricted(long, Runnable, ResourceScope)}.
*
* <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null}
* elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@

package jdk.incubator.foreign;

import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemoryAddressImpl;
import jdk.internal.foreign.NativeMemorySegmentImpl;
import jdk.internal.foreign.Utils;
import jdk.internal.ref.CleanerFactory;

import java.lang.ref.Cleaner;

Expand Down Expand Up @@ -91,72 +89,92 @@ default MemoryAddress address() {
long segmentOffset(MemorySegment segment);

/**
* Returns a new confined native memory segment with given size, and whose base address is this address; the returned segment has its own temporal
* bounds, and can therefore be closed. This method can be useful when interacting with custom native memory sources (e.g. custom allocators),
* where an address to some underlying memory region is typically obtained from native code (often as a plain {@code long} value).
* <p>
* The returned segment will feature all <a href="#access-modes">access modes</a>
* (see {@link MemorySegment#ALL_ACCESS}), and its confinement thread is the current thread (see {@link Thread#currentThread()}).
* Returns a shared native memory segment with given size, and whose base address is this address. This method
* can be useful when interacting with custom native memory sources (e.g. custom allocators), where an address to some
* underlying memory region is typically obtained from native code (often as a plain {@code long} value).
* The returned segment is associated with the {@link ResourceScope#globalScope() global} resource scope.
* <p>
* Clients should ensure that the address and bounds refers to a valid region of memory that is accessible for reading and,
* if appropriate, writing; an attempt to access an invalid memory location from Java code will either return an arbitrary value,
* have no visible effect, or cause an unspecified exception to be thrown.
* <p>
* Calling {@link MemorySegment#close()} on the returned segment will <em>not</em> result in releasing any
* memory resources which might implicitly be associated with the segment. This method is equivalent to the following code:
* This method is equivalent to the following code:
* <pre>{@code
asSegmentRestricted(byteSize, null, null);
asSegmentRestricted(byteSize, null, ResourceScope.globalScope());
* }</pre>
* This method is <em>restricted</em>. Restricted methods are unsafe, and, if used incorrectly, their use might crash
* the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on
* restricted methods, and use safe and supported functionalities, where possible.
*
* @param bytesSize the desired size.
* @return a new confined native memory segment with given base address and size.
* @return a new native memory segment with given base address and size.
* @throws IllegalArgumentException if {@code bytesSize <= 0}.
* @throws UnsupportedOperationException if this address is an heap address.
* @throws IllegalAccessError if the runtime property {@code foreign.restricted} is not set to either
* {@code permit}, {@code warn} or {@code debug} (the default value is set to {@code deny}).
*/
default MemorySegment asSegmentRestricted(long bytesSize) {
return asSegmentRestricted(bytesSize, null, null);
return asSegmentRestricted(bytesSize, null, ResourceScope.globalScope());
}

/**
* Returns a new confined native memory segment with given size, and whose base address is this address; the returned segment has its own temporal
* bounds, and can therefore be closed. This method can be useful when interacting with custom native memory sources (e.g. custom allocators),
* where an address to some underlying memory region is typically obtained from native code (often as a plain {@code long} value).
* <p>
* The returned segment will feature all <a href="#access-modes">access modes</a>
* (see {@link MemorySegment#ALL_ACCESS}), and its confinement thread is the current thread (see {@link Thread#currentThread()}).
* Moreover, the returned segment will keep a strong reference to the supplied attachment object (if any), which can
* be useful in cases where the lifecycle of the segment is dependent on that of some other external resource.
* Returns a native memory segment with given size and resource scope, and whose base address is this address. This method
* can be useful when interacting with custom native memory sources (e.g. custom allocators), where an address to some
* underlying memory region is typically obtained from native code (often as a plain {@code long} value).
* The returned segment is not read-only (see {@link MemorySegment#isReadOnly()}), and is associated with the
* provided resource scope.
* <p>
* Clients should ensure that the address and bounds refers to a valid region of memory that is accessible for reading and,
* if appropriate, writing; an attempt to access an invalid memory location from Java code will either return an arbitrary value,
* have no visible effect, or cause an unspecified exception to be thrown.
* <p>
* Calling {@link MemorySegment#close()} on the returned segment will <em>not</em> result in releasing any
* memory resources which might implicitly be associated with the segment, but will result in calling the
* provided cleanup action (if any).
* This method is equivalent to the following code:
* <pre>{@code
asSegmentRestricted(byteSize, null, scope);
* }</pre>
* This method is <em>restricted</em>. Restricted methods are unsafe, and, if used incorrectly, their use might crash
* the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on
* restricted methods, and use safe and supported functionalities, where possible.
*
* @param bytesSize the desired size.
* @param scope the native segment scope.
* @return a new native memory segment with given base address, size and scope.
* @throws IllegalArgumentException if {@code bytesSize <= 0}.
* @throws UnsupportedOperationException if this address is an heap address.
* @throws IllegalAccessError if the runtime property {@code foreign.restricted} is not set to either
* {@code permit}, {@code warn} or {@code debug} (the default value is set to {@code deny}).
*/
default MemorySegment asSegmentRestricted(long bytesSize, ResourceScope scope) {
return asSegmentRestricted(bytesSize, null, scope);
}

/**
* Returns a new native memory segment with given size and resource scope, and whose base address is this address. This method
* can be useful when interacting with custom native memory sources (e.g. custom allocators), where an address to some
* underlying memory region is typically obtained from native code (often as a plain {@code long} value).
* The returned segment is associated with the provided resource scope.
* <p>
* Clients should ensure that the address and bounds refers to a valid region of memory that is accessible for reading and,
* if appropriate, writing; an attempt to access an invalid memory location from Java code will either return an arbitrary value,
* have no visible effect, or cause an unspecified exception to be thrown.
* <p>
* Both the cleanup action and the attachment object (if any) will be preserved under terminal operations such as
* {@link MemorySegment#handoff(Thread)}, {@link MemorySegment#share()} and {@link MemorySegment#registerCleaner(Cleaner)}.
* Calling {@link ResourceScope#close()} on the scope associated with the returned segment will result in calling
* the provided cleanup action (if any).
* <p>
* This method is <em>restricted</em>. Restricted methods are unsafe, and, if used incorrectly, their use might crash
* the JVM or, worse, silently result in memory corruption. Thus, clients should refrain from depending on
* restricted methods, and use safe and supported functionalities, where possible.
*
* @param bytesSize the desired size.
* @param cleanupAction the cleanup action; can be {@code null}.
* @param attachment an attachment object that will be kept strongly reachable by the returned segment; can be {@code null}.
* @return a new confined native memory segment with given base address and size.
* @param scope the native segment scope.
* @return a new native memory segment with given base address, size and scope.
* @throws IllegalArgumentException if {@code bytesSize <= 0}.
* @throws UnsupportedOperationException if this address is an heap address.
* @throws IllegalAccessError if the runtime property {@code foreign.restricted} is not set to either
* {@code permit}, {@code warn} or {@code debug} (the default value is set to {@code deny}).
*/
MemorySegment asSegmentRestricted(long bytesSize, Runnable cleanupAction, Object attachment);
MemorySegment asSegmentRestricted(long bytesSize, Runnable cleanupAction, ResourceScope scope);

/**
* Returns the raw long value associated with this memory address.
Expand Down
Loading