|
40 | 40 | * A native library lookup. Exposes a lookup operation for searching symbols, see {@link LibraryLookup#lookup(String)}.
|
41 | 41 | * A given native library remains loaded as long as there is at least one <em>live</em> library lookup instance referring
|
42 | 42 | * to it.
|
43 |
| - * All symbol instances (see {@link LibraryLookup.Symbol}) generated by a given library lookup object contain a strong reference |
44 |
| - * to said lookup object, therefore preventing library unloading; in turn method handle instances obtained from |
45 |
| - * {@link CLinker#downcallHandle(Addressable, MethodType, FunctionDescriptor)}) also maintain a strong reference |
46 |
| - * to the addressable parameter used for their construction. This means that there is always a strong reachability chain |
47 |
| - * from a native method handle to a lookup object (the one that was used to lookup the native library symbol the method handle |
48 |
| - * refers to); this is useful to prevent situations where a native library is unloaded in the middle of a native call. |
49 |
| - * <p><a id = "var-symbols"></a></p> |
50 |
| - * In cases where a client wants to create a memory segment out of a lookup symbol, the client might want to attach the |
51 |
| - * lookup symbol to the newly created segment, so that the symbol will be kept reachable as long as the memory segment |
52 |
| - * is reachable; this can be achieved by creating the segment using the {@link MemoryAddress#asSegmentRestricted(long, ResourceScope)}. |
53 |
| - * restricted segment factory, as follows: |
| 43 | + * All instances generated by a given library lookup object contain a strong reference to said lookup object, |
| 44 | + * therefore preventing library unloading. For {@link #lookup(String, MemoryLayout) memory segments} obtained from a library lookup object, |
| 45 | + * this means that clients can safely dereference memory associated with lookup symbols, as follows: |
54 | 46 | * <pre>{@code
|
55 |
| -LibraryLookup defaultLookup = LibraryLookup.defaultLookup(); |
56 |
| -LibraryLookup.Symbol errno = defaultLookup.lookup("errno"); |
57 |
| -MemorySegment errnoSegment = errno.address().asSegmentRestricted(4, ResourceScope.ofShared(errno, Cleaner.create())); |
| 47 | + * LibraryLookup defaultLookup = LibraryLookup.defaultLookup(); |
| 48 | + * MemorySegment errnoSegment = defaultLookup.lookup("errno", MemoryLayouts.JAVA_INT).get(); |
| 49 | + * int errno = MemoryAccess.getInt(errnoSegment); |
58 | 50 | * }</pre>
|
59 | 51 | * <p>
|
| 52 | + * For {@link #lookup(String) memory addresses} obtained from a library lookup object, |
| 53 | + * since {@link CLinker#downcallHandle(Addressable, MethodType, FunctionDescriptor) native method handles} |
| 54 | + * also maintain a strong reference to the addressable parameter used for their construction, there is |
| 55 | + * always a strong reachability chain from a native method handle to a lookup object (the one that was used to lookup |
| 56 | + * the native library symbol the method handle refers to). This is useful to prevent situations where a native library |
| 57 | + * is unloaded in the middle of a native call. |
| 58 | + * <p> |
60 | 59 | * To allow for a library to be unloaded, a client will have to discard any strong references it
|
61 | 60 | * maintains, directly, or indirectly to a lookup object associated with given library.
|
62 | 61 | *
|
|
66 | 65 | public interface LibraryLookup {
|
67 | 66 |
|
68 | 67 | /**
|
69 |
| - * A symbol retrieved during a library lookup. A lookup symbol has a <em>name</em> and can be projected |
70 |
| - * into a memory address (see {@link #name()} and {@link #address()}, respectively). |
71 |
| - * |
72 |
| - * @apiNote In the future, if the Java language permits, {@link Symbol} |
73 |
| - * may become a {@code sealed} interface, which would prohibit subclassing except by |
74 |
| - * explicitly permitted types. |
75 |
| - * |
76 |
| - * @implSpec |
77 |
| - * Implementations of this interface are immutable, thread-safe and <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>. |
| 68 | + * Looks up a symbol with given name in this library. The returned memory address maintains a strong reference to this lookup object. |
| 69 | + * @param name the symbol name. |
| 70 | + * @return the memory address associated with the library symbol (if any). |
78 | 71 | */
|
79 |
| - interface Symbol extends Addressable { |
80 |
| - /** |
81 |
| - * The name of this lookup symbol. |
82 |
| - * @return the name of this lookup symbol. |
83 |
| - */ |
84 |
| - String name(); |
85 |
| - |
86 |
| - /** |
87 |
| - * The memory address of this lookup symbol. If the memory associated with this symbol needs to be dereferenced, |
88 |
| - * clients can obtain a segment from this symbol's address using the {@link MemoryAddress#asSegmentRestricted(long, Runnable, ResourceScope)}, |
89 |
| - * 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. |
90 |
| - * @return the memory address of this lookup symbol. |
91 |
| - */ |
92 |
| - @Override |
93 |
| - MemoryAddress address(); |
94 |
| - } |
| 72 | + Optional<MemoryAddress> lookup(String name); |
95 | 73 |
|
96 | 74 | /**
|
97 |
| - * Looks up a symbol with given name in this library. The returned symbol maintains a strong reference to this lookup object. |
| 75 | + * Looks up a symbol with given name in this library. The returned memory segment has a size that matches that of |
| 76 | + * the specified layout, and maintains a strong reference to this lookup object. This method can be useful |
| 77 | + * to lookup global variable symbols in a foreign library. |
98 | 78 | * @param name the symbol name.
|
99 |
| - * @return the library symbol (if any). |
| 79 | + * @param layout the layout to be associated with the library symbol. |
| 80 | + * @return the memory segment associated with the library symbol (if any). |
| 81 | + * @throws IllegalArgumentException if the address associated with the lookup symbol do not match the |
| 82 | + * {@link MemoryLayout#byteAlignment() alignment constraints} in {@code layout}. |
100 | 83 | */
|
101 |
| - Optional<Symbol> lookup(String name); |
| 84 | + @NativeAccess |
| 85 | + Optional<MemorySegment> lookup(String name, MemoryLayout layout); |
102 | 86 |
|
103 | 87 | /**
|
104 | 88 | * Obtain a default library lookup object.
|
|
0 commit comments