Skip to content
Closed
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ private NativeMemorySegmentImpl reinterpretInternal(Class<?> callerClass, long n
Reflection.ensureNativeAccess(callerClass, MemorySegment.class, "reinterpret", false);
Utils.checkNonNegativeArgument(newSize, "newSize");
if (!isNative()) throw new UnsupportedOperationException("Not a native segment");
Runnable action = cleanup != null ?
() -> cleanup.accept(SegmentFactories.makeNativeSegmentUnchecked(address(), newSize)) :
null;
Runnable action = cleanupAction(address(), newSize, cleanup);
return SegmentFactories.makeNativeSegmentUnchecked(address(), newSize, scope, readOnly, action);
}

// Using a static helper method ensures there is no unintended lambda capturing of `this`
private static Runnable cleanupAction(long address, long newSize, Consumer<MemorySegment> cleanup) {
return cleanup != null ?
() -> cleanup.accept(SegmentFactories.makeNativeSegmentUnchecked(address, newSize)) :
null;
}

private AbstractMemorySegmentImpl asSliceNoCheck(long offset, long newSize) {
return dup(offset, newSize, readOnly, scope);
}
Expand Down