Skip to content

Commit

Permalink
Initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
mcimadamore committed May 30, 2024
1 parent 43a2f17 commit b37f9cc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/java.base/share/classes/jdk/internal/foreign/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.lang.invoke.VarHandle;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import jdk.internal.access.SharedSecrets;
Expand Down Expand Up @@ -88,6 +90,14 @@ public static MemorySegment alignUp(MemorySegment ms, long alignment) {
}

public static VarHandle makeSegmentViewVarHandle(ValueLayout layout) {
final class VarHandleCache {
private static final Map<ValueLayout, VarHandle> HANDLE_MAP = new ConcurrentHashMap<>();
}
return VarHandleCache.HANDLE_MAP
.computeIfAbsent(layout.withoutName(), Utils::makeSegmentViewVarHandleInternal);
}

private static VarHandle makeSegmentViewVarHandleInternal(ValueLayout layout) {
Class<?> baseCarrier = layout.carrier();
if (layout.carrier() == MemorySegment.class) {
baseCarrier = switch ((int) ValueLayout.ADDRESS.byteSize()) {
Expand All @@ -108,7 +118,7 @@ public static VarHandle makeSegmentViewVarHandle(ValueLayout layout) {
handle = MethodHandles.filterValue(handle,
MethodHandles.explicitCastArguments(ADDRESS_TO_LONG, MethodType.methodType(baseCarrier, MemorySegment.class)),
MethodHandles.explicitCastArguments(MethodHandles.insertArguments(LONG_TO_ADDRESS, 1,
pointeeByteSize(addressLayout), pointeeByteAlign(addressLayout)),
pointeeByteSize(addressLayout), pointeeByteAlign(addressLayout)),
MethodType.methodType(MemorySegment.class, baseCarrier)));
}
return handle;
Expand Down

0 comments on commit b37f9cc

Please sign in to comment.