From 492f34867778e3c2016fe669dd0c0a0ea9c8f3c0 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Tue, 2 Jun 2026 09:06:28 +0200 Subject: [PATCH 1/8] Make finalizable reference release idempotent --- .../builtins/modules/io/FileIOBuiltins.java | 3 --- .../python/builtins/modules/io/PFileIO.java | 18 +++++++++++------- .../objects/memoryview/PMemoryView.java | 3 ++- .../python/builtins/objects/mmap/PMMap.java | 3 +-- .../objects/posix/PScandirIterator.java | 5 ++--- .../graal/python/runtime/AsyncHandler.java | 13 ++++++++----- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java index 14813470e3..786be247d1 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java @@ -202,9 +202,6 @@ public FDReleaseCallback(OwnFD fd) { @Override public void execute(PythonContext context, Access access) { - if (fd.isReleased()) { - return; - } try { fd.doRelease(); } catch (Exception e) { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/PFileIO.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/PFileIO.java index 89fdb4eac1..f2ff239b55 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/PFileIO.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/PFileIO.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -91,7 +91,9 @@ public void setFD(int fd, PythonContext context) { public void setClosed() { if (fd != null) { if (fd.getOwnFD() != null) { - fd.getOwnFD().markReleased(); + boolean markedReleased = fd.getOwnFD().markReleased(); + assert markedReleased || fd.getOwnFD().isReleased(); + } fd = null; } @@ -208,11 +210,13 @@ public OwnFD(Object referent, int fd, PythonContext context) { @SuppressWarnings("try") void doRelease() { - markReleased(); - try (GilNode.UncachedRelease gil = GilNode.uncachedRelease()) { - PosixSupportLibrary.getUncached().close(context.getPosixSupport(), (int) getReference()); - } catch (PosixException e) { - // ignore + if (markReleased()) { + assert isReleased(); + try (GilNode.UncachedRelease gil = GilNode.uncachedRelease()) { + PosixSupportLibrary.getUncached().close(context.getPosixSupport(), (int) getReference()); + } catch (PosixException e) { + // ignore + } } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/PMemoryView.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/PMemoryView.java index 326cec3553..6fb2524ae9 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/PMemoryView.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/PMemoryView.java @@ -238,7 +238,8 @@ public void setCachedHash(int cachedHash) { public void setReleased() { flags |= FLAG_RELEASED; if (reference != null) { - reference.markReleased(); + boolean markedReleased = reference.markReleased(); + assert markedReleased || reference.isReleased(); reference = null; } buffer = null; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/PMMap.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/PMMap.java index 836b012b3d..cec536ae08 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/PMMap.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/PMMap.java @@ -187,10 +187,9 @@ public AsyncHandler.AsyncAction release() { } void close(PosixSupportLibrary posixLib, Object posixSupport) { - if (isReleased()) { + if (!markReleased()) { return; } - markReleased(); Object handle = getReference(); if (fd != -1) { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/PScandirIterator.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/PScandirIterator.java index 62b6450a5c..fbe75c77dc 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/PScandirIterator.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/PScandirIterator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -76,10 +76,9 @@ public AsyncAction release() { } void rewindAndClose(PosixSupportLibrary posixLib, Object posixSupport) { - if (isReleased()) { + if (!markReleased()) { return; } - markReleased(); Object dirStream = getReference(); if (needsRewind) { posixLib.rewinddir(posixSupport, dirStream); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java index 82f84611ea..c185d60248 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java @@ -54,6 +54,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import org.graalvm.polyglot.SandboxPolicy; @@ -510,7 +511,7 @@ public SharedFinalizer(PythonContext context) { */ public abstract static class FinalizableReference extends PhantomReference { private final Object reference; - private boolean released; + private final AtomicBoolean released = new AtomicBoolean(false); @SuppressWarnings("this-escape") public FinalizableReference(Object referent, Object reference, SharedFinalizer sharedFinalizer) { @@ -538,15 +539,17 @@ public final Object getReference() { } public final boolean isReleased() { - return released; + return released.get(); } /** - * Mark the FinalizableReference as freed in case it has been freed elsewhare. This will + * Mark the FinalizableReference as freed in case it has been freed elsewhere. This will * avoid double-freeing the reference. */ - public final void markReleased() { - this.released = true; + public final boolean markReleased() { + boolean markedReleased = released.compareAndSet(false, true); + assert markedReleased || released.get(); + return markedReleased; } /** From 6386d8bc37f3ab87b50acda97a05069beeaf4461 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Fri, 29 May 2026 14:52:48 +0200 Subject: [PATCH 2/8] Migrate zlib native support to NativeAccess --- .../modules/zlib/NativeZlibCompObject.java | 36 +- .../builtins/modules/zlib/ZLibCompObject.java | 4 +- .../modules/zlib/ZLibModuleBuiltins.java | 91 +- .../modules/zlib/ZlibCompressBuiltins.java | 22 +- .../modules/zlib/ZlibDecompressBuiltins.java | 29 +- .../zlib/ZlibDecompressorBuiltins.java | 22 +- .../modules/zlib/ZlibDecompressorObject.java | 8 +- .../builtins/modules/zlib/ZlibNodes.java | 170 ++-- .../graal/python/runtime/AsyncHandler.java | 7 + .../graal/python/runtime/NFIZlibSupport.java | 890 +++++++----------- .../runtime/NativeCompressionSupport.java | 107 +++ .../graal/python/runtime/object/PFactory.java | 8 +- graalpython/python-libzsupport/src/zlib.c | 109 +-- 13 files changed, 617 insertions(+), 886 deletions(-) create mode 100644 graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeCompressionSupport.java diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java index e4190b919a..fa7c098f5a 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -41,35 +41,39 @@ package com.oracle.graal.python.builtins.modules.zlib; import com.oracle.graal.python.runtime.NFIZlibSupport; -import com.oracle.truffle.api.CompilerDirectives; +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.object.Shape; -// Note: some IDEs mark this class as inaccessible in PFactory, but changing this to -// public will cause a warning: [this-escape] possible 'this' escape before subclass is fully -// initialized public final class NativeZlibCompObject extends ZLibCompObject { private NFIZlibSupport.Pointer pointer; byte[] lastInput; - public NativeZlibCompObject(Object cls, Shape instanceShape, Object zst, NFIZlibSupport zlibSupport) { + public NativeZlibCompObject(Object cls, Shape instanceShape, long zst, NFIZlibSupport zlibSupport) { super(cls, instanceShape); this.pointer = new NFIZlibSupport.Pointer(this, zst, zlibSupport); - this.lastInput = null; } - public Object getZst() { - assert pointer != null; - return pointer.getReference(); + public long getZst() { + NFIZlibSupport.Pointer p = pointer; + assert p != null && !p.isReleased(); + return p.getPointer(); } - @CompilerDirectives.TruffleBoundary + @TruffleBoundary public void markReleased() { - if (isInitialized) { - synchronized (this) { - isInitialized = false; - pointer.markReleased(); - pointer = null; + NFIZlibSupport.Pointer p; + synchronized (this) { + if (!isInitialized) { + assert pointer == null || pointer.isReleased(); + return; } + assert pointer != null && !pointer.isReleased(); + isInitialized = false; + p = pointer; + pointer = null; + assert !isInitialized; } + boolean markedReleased = p.markReleased(); + assert markedReleased || p.isReleased(); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibCompObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibCompObject.java index bc4f27c7b5..954ffd8868 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibCompObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibCompObject.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -51,7 +51,7 @@ public abstract class ZLibCompObject extends PythonBuiltinObject { private PBytes unusedData; private PBytes unconsumedTail; - public ZLibCompObject(Object cls, Shape instanceShape) { + protected ZLibCompObject(Object cls, Shape instanceShape) { super(cls, instanceShape); this.isInitialized = true; this.eof = false; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java index 829bb32ed3..b732e55d70 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -46,7 +46,6 @@ import static com.oracle.graal.python.nodes.ErrorMessages.EXPECTED_BYTESLIKE_GOT_P; import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; import static com.oracle.graal.python.runtime.exception.PythonErrorType.ZLibError; -import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; import static com.oracle.graal.python.util.PythonUtils.crc32; import static com.oracle.graal.python.util.PythonUtils.tsLiteral; import static java.util.zip.Deflater.DEFAULT_COMPRESSION; @@ -57,9 +56,9 @@ import com.oracle.graal.python.PythonLanguage; import com.oracle.graal.python.annotations.ArgumentClinic; +import com.oracle.graal.python.annotations.Builtin; import com.oracle.graal.python.annotations.ClinicConverterFactory; import com.oracle.graal.python.annotations.ClinicConverterFactory.UseDefaultForNone; -import com.oracle.graal.python.annotations.Builtin; import com.oracle.graal.python.builtins.CoreFunctions; import com.oracle.graal.python.builtins.Python3Core; import com.oracle.graal.python.builtins.PythonBuiltins; @@ -85,7 +84,6 @@ import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; import com.oracle.graal.python.runtime.NFIZlibSupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -103,8 +101,6 @@ import com.oracle.truffle.api.dsl.NonIdempotent; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.frame.VirtualFrame; -import com.oracle.truffle.api.interop.InteropLibrary; -import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.strings.TruffleString; @@ -186,20 +182,20 @@ public void initialize(Python3Core core) { @Override public void postInitialize(Python3Core core) { super.postInitialize(core); - NFIZlibSupport zlibSupport = core.getContext().getNFIZlibSupport(); + NFIZlibSupport zlibSupport = core.getContext().getNativeZlibSupport(); PythonModule zlibModule = core.lookupBuiltinModule(T_ZLIB); // isAvailable() checked already if native access is allowed TruffleString ver = T_JDK_ZLIB_VERSION; TruffleString rtver = T_JDK_ZLIB_VERSION; if (zlibSupport.isAvailable()) { try { - ver = asString(zlibSupport.zlibVersion()); - rtver = asString(zlibSupport.zlibRuntimeVersion()); + ver = zlibSupport.zlibVersion(); + rtver = zlibSupport.zlibRuntimeVersion(); zlibModule.setAttribute(tsLiteral("Z_PARTIAL_FLUSH"), Z_PARTIAL_FLUSH); zlibModule.setAttribute(tsLiteral("Z_BLOCK"), Z_BLOCK); zlibModule.setAttribute(tsLiteral("Z_TREES"), Z_TREES); - } catch (NativeLibrary.NativeLibraryCannotBeLoaded e) { - zlibSupport.notAvailable(); + } catch (UnsupportedOperationException e) { + zlibSupport.setNotAvailable(); // ignore and proceed without native zlib support and use jdk's. } } @@ -207,17 +203,6 @@ public void postInitialize(Python3Core core) { zlibModule.setAttribute(tsLiteral("ZLIB_RUNTIME_VERSION"), rtver); } - private static TruffleString asString(Object o) { - if (o != null) { - try { - return InteropLibrary.getUncached().asTruffleString(o).switchEncodingUncached(TS_ENCODING); - } catch (UnsupportedMessageException e) { - // pass through - } - } - return null; - } - @ImportStatic(MathGuards.class) public abstract static class ExpectIntNode extends ArgumentCastNode { private final Object defaultValue; @@ -330,7 +315,7 @@ protected ArgumentClinicProvider getArgumentClinic() { @NonIdempotent protected boolean useNative() { - return PythonContext.get(this).getNFIZlibSupport().isAvailable(); + return PythonContext.get(this).getNativeZlibSupport().isAvailable(); } @Specialization @@ -349,19 +334,17 @@ static long doCRC32(byte[] data) { @Specialization(guards = "useNative()") long doNativeBytes(PBytesLike data, int value, @Bind Node inliningTarget, - @Shared("b") @Cached SequenceStorageNodes.GetInternalBytesNode toBytes, - @Shared @Cached NativeLibrary.InvokeNativeFunction invoke) { + @Shared("b") @Cached SequenceStorageNodes.GetInternalBytesNode toBytes) { byte[] bytes = toBytes.execute(inliningTarget, data); int len = data.getSequenceStorage().length(); - return nativeCrc32(bytes, len, value, invoke); + return nativeCrc32(bytes, len, value); } @Specialization(guards = {"useNative()", "!isBytes(data)"}) long doNativeObject(VirtualFrame frame, Object data, int value, - @Shared("bb") @Cached ToBytesNode toBytesNode, - @Shared @Cached NativeLibrary.InvokeNativeFunction invoke) { + @Shared("bb") @Cached ToBytesNode toBytesNode) { byte[] bytes = toBytesNode.execute(frame, data); - return nativeCrc32(bytes, bytes.length, value, invoke); + return nativeCrc32(bytes, bytes.length, value); } @Specialization(guards = "!useNative()") @@ -386,10 +369,9 @@ static long error(Object data, @SuppressWarnings("unused") Object value, throw PRaiseNode.raiseStatic(inliningTarget, TypeError, EXPECTED_BYTESLIKE_GOT_P, data); } - long nativeCrc32(byte[] bytes, int len, int value, - NativeLibrary.InvokeNativeFunction invoke) { + long nativeCrc32(byte[] bytes, int len, int value) { PythonContext ctxt = getContext(); - int signedVal = (int) ctxt.getNFIZlibSupport().crc32(value, bytes, len, invoke); + int signedVal = (int) ctxt.getNativeZlibSupport().crc32(value, bytes, len); return signedVal & 0xFFFFFFFFL; } } @@ -410,7 +392,7 @@ protected ArgumentClinicProvider getArgumentClinic() { @NonIdempotent protected boolean useNative() { - return getContext().getNFIZlibSupport().isAvailable(); + return getContext().getNativeZlibSupport().isAvailable(); } @Specialization @@ -429,19 +411,17 @@ private static long doAdler32(byte[] bytes) { @Specialization(guards = "useNative()") long doNativeBytes(PBytesLike data, int value, @Bind Node inliningTarget, - @Shared("b") @Cached SequenceStorageNodes.GetInternalBytesNode toBytes, - @Shared @Cached NativeLibrary.InvokeNativeFunction invoke) { + @Shared("b") @Cached SequenceStorageNodes.GetInternalBytesNode toBytes) { byte[] bytes = toBytes.execute(inliningTarget, data); int len = data.getSequenceStorage().length(); - return nativeAdler32(bytes, len, value, PythonContext.get(this), invoke); + return nativeAdler32(bytes, len, value, PythonContext.get(this)); } @Specialization(guards = {"useNative()", "!isBytes(data)"}) long doNativeObject(VirtualFrame frame, Object data, int value, - @Shared("bb") @Cached ToBytesNode toBytesNode, - @Shared @Cached NativeLibrary.InvokeNativeFunction invoke) { + @Shared("bb") @Cached ToBytesNode toBytesNode) { byte[] bytes = toBytesNode.execute(frame, data); - return nativeAdler32(bytes, bytes.length, value, PythonContext.get(this), invoke); + return nativeAdler32(bytes, bytes.length, value, PythonContext.get(this)); } @Specialization(guards = "!useNative()") @@ -461,9 +441,8 @@ long doJavaObject(VirtualFrame frame, Object data, int value, } long nativeAdler32(byte[] bytes, int len, int value, - PythonContext ctxt, - NativeLibrary.InvokeNativeFunction invoke) { - int signedVal = (int) ctxt.getNFIZlibSupport().adler32(value, bytes, len, invoke); + PythonContext ctxt) { + int signedVal = (int) ctxt.getNativeZlibSupport().adler32(value, bytes, len); return signedVal & 0xFFFFFFFFL; } @@ -523,7 +502,7 @@ abstract static class CompressInnerNode extends Node { @NonIdempotent protected boolean useNative() { - return PythonContext.get(this).getNFIZlibSupport().isAvailable(); + return PythonContext.get(this).getNativeZlibSupport().isAvailable(); } protected static boolean isValidLevel(int level) { @@ -591,7 +570,7 @@ abstract static class DecompressInnerNode extends Node { @NonIdempotent protected boolean useNative() { - return PythonContext.get(this).getNFIZlibSupport().isAvailable(); + return PythonContext.get(this).getNativeZlibSupport().isAvailable(); } @Specialization(guards = "useNative()") @@ -628,7 +607,7 @@ protected ArgumentClinicProvider getArgumentClinic() { @NonIdempotent protected boolean useNative() { - return PythonContext.get(this).getNFIZlibSupport().isAvailable(); + return PythonContext.get(this).getNativeZlibSupport().isAvailable(); } protected static boolean isValidWBitRange(int wbits) { @@ -639,18 +618,16 @@ protected static boolean isValidWBitRange(int wbits) { static Object doNative(int level, int method, int wbits, int memLevel, int strategy, byte[] zdict, @Bind Node inliningTarget, @Bind PythonLanguage language, - @Cached NativeLibrary.InvokeNativeFunction createCompObject, - @Cached NativeLibrary.InvokeNativeFunction compressObjInit, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = PythonContext.get(inliningTarget).getNFIZlibSupport(); - Object zst = zlibSupport.createCompObject(createCompObject); + NFIZlibSupport zlibSupport = PythonContext.get(inliningTarget).getNativeZlibSupport(); + long zst = zlibSupport.createCompObject(); int err; if (zdict.length > 0) { err = zlibSupport.compressObjInitWithDict(zst, level, method, wbits, memLevel, strategy, - zdict, zdict.length, compressObjInit); + zdict, zdict.length); } else { - err = zlibSupport.compressObjInit(zst, level, method, wbits, memLevel, strategy, compressObjInit); + err = zlibSupport.compressObjInit(zst, level, method, wbits, memLevel, strategy); } if (err != Z_OK) { errorHandling.execute(inliningTarget, zst, err, zlibSupport, true); @@ -700,7 +677,7 @@ protected ArgumentClinicProvider getArgumentClinic() { @NonIdempotent protected boolean useNative() { - return PythonContext.get(this).getNFIZlibSupport().isAvailable(); + return PythonContext.get(this).getNativeZlibSupport().isAvailable(); } protected static boolean isValidWBitRange(int wbits) { @@ -711,17 +688,15 @@ protected static boolean isValidWBitRange(int wbits) { static Object doNative(int wbits, byte[] zdict, @Bind Node inliningTarget, @Bind PythonContext context, - @Cached NativeLibrary.InvokeNativeFunction createCompObject, - @Cached NativeLibrary.InvokeNativeFunction decompressObjInit, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - Object zst = zlibSupport.createCompObject(createCompObject); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + long zst = zlibSupport.createCompObject(); int err; if (zdict.length > 0) { - err = zlibSupport.decompressObjInitWithDict(zst, wbits, zdict, zdict.length, decompressObjInit); + err = zlibSupport.decompressObjInitWithDict(zst, wbits, zdict, zdict.length); } else { - err = zlibSupport.decompressObjInit(zst, wbits, decompressObjInit); + err = zlibSupport.decompressObjInit(zst, wbits); } if (err != Z_OK) { errorHandling.execute(inliningTarget, zst, err, zlibSupport, true); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java index 4e28ad4269..3e761da6c3 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -72,7 +72,6 @@ import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; import com.oracle.graal.python.runtime.NFIZlibSupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -155,17 +154,14 @@ abstract static class BaseCopyNode extends PNodeWithContext { @Specialization(guards = "self.isInitialized()") static Object doNative(Node inliningTarget, NativeZlibCompObject self, @Bind PythonContext context, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction createCompObject, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction compressObjCopy, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction deallocateStream, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - Object zstNewCopy = zlibSupport.createCompObject(createCompObject); - int err = zlibSupport.compressObjCopy(self.getZst(), zstNewCopy, compressObjCopy); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + long zstNewCopy = zlibSupport.createCompObject(); + int err = zlibSupport.compressObjCopy(self.getZst(), zstNewCopy); if (err != Z_OK) { - zlibSupport.deallocateStream(zstNewCopy, deallocateStream); + zlibSupport.deallocateStream(zstNewCopy); errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); } return PFactory.createNativeZLibCompObjectCompress(context.getLanguage(inliningTarget), zstNewCopy, zlibSupport); @@ -240,15 +236,13 @@ static PBytes empty(ZLibCompObject self, int mode, @Specialization(guards = {"mode != Z_NO_FLUSH", "self.isInitialized()"}) static PBytes doit(NativeZlibCompObject self, int mode, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction compressObjFlush, @Cached ZlibNodes.GetNativeBufferNode getBuffer, - @Cached NativeLibrary.InvokeNativeFunction getIsInitialised, @Cached ZlibNodes.NativeDeallocation processDeallocation, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); PythonContext context = PythonContext.get(inliningTarget); - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] lastInput; if (self.lastInput == null) { // all previous input data has been processed or nothing has been compressed. @@ -259,12 +253,12 @@ static PBytes doit(NativeZlibCompObject self, int mode, // stored in the native stream. lastInput = self.lastInput; } - int err = zlibSupport.compressObjFlush(self.getZst(), lastInput, DEF_BUF_SIZE, mode, compressObjFlush); + int err = zlibSupport.compressObjFlush(self.getZst(), lastInput, DEF_BUF_SIZE, mode); if (err != Z_OK) { errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); } byte[] resultArray = getBuffer.getOutputBuffer(inliningTarget, self.getZst(), context); - if (zlibSupport.getIsInitialised(self.getZst(), getIsInitialised) == 0) { + if (zlibSupport.getIsInitialised(self.getZst()) == 0) { processDeallocation.execute(inliningTarget, self, context, true); } return PFactory.createBytes(context.getLanguage(inliningTarget), resultArray); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java index 480ce32bbe..7a0090a802 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -74,7 +74,6 @@ import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; import com.oracle.graal.python.runtime.NFIZlibSupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.exception.PException; import com.oracle.graal.python.runtime.object.PFactory; @@ -161,17 +160,14 @@ abstract static class BaseCopyNode extends PNodeWithContext { @Specialization(guards = "self.isInitialized()") static Object doNative(Node inliningTarget, NativeZlibCompObject self, @Bind PythonContext context, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction createCompObject, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction decompressObjCopy, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction deallocateStream, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - Object zstNewCopy = zlibSupport.createCompObject(createCompObject); - int err = zlibSupport.decompressObjCopy(self.getZst(), zstNewCopy, decompressObjCopy); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + long zstNewCopy = zlibSupport.createCompObject(); + int err = zlibSupport.decompressObjCopy(self.getZst(), zstNewCopy); if (err != Z_OK) { - zlibSupport.deallocateStream(zstNewCopy, deallocateStream); + zlibSupport.deallocateStream(zstNewCopy); errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); } ZLibCompObject copy = PFactory.createNativeZLibCompObjectDecompress(context.getLanguage(inliningTarget), zstNewCopy, zlibSupport); @@ -241,20 +237,18 @@ protected ArgumentClinicProvider getArgumentClinic() { static PBytes doit(NativeZlibCompObject self, int length, @Bind Node inliningTarget, @Bind PythonContext context, - @Cached NativeLibrary.InvokeNativeFunction decompressObjFlush, @Cached ZlibNodes.GetNativeBufferNode getBuffer, - @Cached NativeLibrary.InvokeNativeFunction getIsInitialised, @Cached ZlibNodes.NativeDeallocation processDeallocation, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - int err = zlibSupport.decompressObjFlush(self.getZst(), length, decompressObjFlush); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + int err = zlibSupport.decompressObjFlush(self.getZst(), length); if (err != Z_OK) { errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); } byte[] resultArray = getBuffer.getOutputBuffer(inliningTarget, self.getZst(), context); - if (zlibSupport.getIsInitialised(self.getZst(), getIsInitialised) == 0) { + if (zlibSupport.getIsInitialised(self.getZst()) == 0) { processDeallocation.execute(inliningTarget, self, context, false); } return PFactory.createBytes(context.getLanguage(inliningTarget), resultArray); @@ -359,12 +353,11 @@ static boolean doit(NativeZlibCompObject self) { } @Specialization(guards = {"!self.isEof()", "self.isInitialized()"}) - boolean getit(NativeZlibCompObject self, - @Cached NativeLibrary.InvokeNativeFunction getEOF) { + boolean getit(NativeZlibCompObject self) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = PythonContext.get(this).getNFIZlibSupport(); - self.setEof(zlibSupport.getEOF(self.getZst(), getEOF) == 1); + NFIZlibSupport zlibSupport = PythonContext.get(this).getNativeZlibSupport(); + self.setEof(zlibSupport.getEOF(self.getZst()) == 1); return self.isEof(); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java index 934112ba79..b842d741ac 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -71,7 +71,6 @@ import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; import com.oracle.graal.python.runtime.NFIZlibSupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -114,7 +113,7 @@ protected ArgumentClinicProvider getArgumentClinic() { @NonIdempotent protected boolean useNative() { - return PythonContext.get(this).getNFIZlibSupport().isAvailable(); + return PythonContext.get(this).getNativeZlibSupport().isAvailable(); } protected static boolean isValidWBitRange(int wbits) { @@ -125,17 +124,15 @@ protected static boolean isValidWBitRange(int wbits) { static Object doNative(@SuppressWarnings("unused") Object type, int wbits, byte[] zdict, @Bind Node inliningTarget, @Bind PythonContext context, - @Cached NativeLibrary.InvokeNativeFunction createCompObject, - @Cached NativeLibrary.InvokeNativeFunction decompressObjInit, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - Object zst = zlibSupport.createCompObject(createCompObject); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + long zst = zlibSupport.createCompObject(); int err; if (zdict.length > 0) { - err = zlibSupport.decompressObjInitWithDict(zst, wbits, zdict, zdict.length, decompressObjInit); + err = zlibSupport.decompressObjInitWithDict(zst, wbits, zdict, zdict.length); } else { - err = zlibSupport.decompressObjInit(zst, wbits, decompressObjInit); + err = zlibSupport.decompressObjInit(zst, wbits); } if (err != Z_OK) { errorHandling.execute(inliningTarget, zst, err, zlibSupport, true); @@ -262,12 +259,11 @@ static boolean doit(ZlibDecompressorObject self) { } @Specialization(guards = {"!self.isEof()", "self.isInitialized()", "self.isNativeDecompressor()"}) - boolean doNative(ZlibDecompressorObject self, - @Cached NativeLibrary.InvokeNativeFunction getEOF) { + boolean doNative(ZlibDecompressorObject self) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = PythonContext.get(this).getNFIZlibSupport(); - self.setEof(zlibSupport.getEOF(self.getZst(), getEOF) == 1); + NFIZlibSupport zlibSupport = PythonContext.get(this).getNativeZlibSupport(); + self.setEof(zlibSupport.getEOF(self.getZst()) == 1); return self.isEof(); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java index 40179e5921..98a5e5784e 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -58,7 +58,7 @@ public class ZlibDecompressorObject extends PythonBuiltinObject { private boolean needsInput; // native - private ZlibDecompressorObject(Object cls, Shape instanceShape, Object zst, NFIZlibSupport zlibSupport) { + private ZlibDecompressorObject(Object cls, Shape instanceShape, long zst, NFIZlibSupport zlibSupport) { super(cls, instanceShape); this.compObject = new NativeZlibCompObject(cls, instanceShape, zst, zlibSupport); this.availInReal = 0; @@ -85,7 +85,7 @@ public void setEof(boolean eof) { compObject.setEof(eof); } - public Object getZst() { + public long getZst() { assert isNativeDecompressor(); return ((NativeZlibCompObject) compObject).getZst(); } @@ -131,7 +131,7 @@ public boolean isNativeDecompressor() { return compObject instanceof NativeZlibCompObject; } - public static ZlibDecompressorObject createNative(Object cls, Shape instanceShape, Object zst, NFIZlibSupport zlibSupport) { + public static ZlibDecompressorObject createNative(Object cls, Shape instanceShape, long zst, NFIZlibSupport zlibSupport) { return new ZlibDecompressorObject(cls, instanceShape, zst, zlibSupport); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java index dbfca15a08..942026545f 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -72,13 +72,11 @@ import com.oracle.graal.python.nodes.PNodeWithContext; import com.oracle.graal.python.nodes.PRaiseNode; import com.oracle.graal.python.runtime.NFIZlibSupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; import com.oracle.truffle.api.dsl.Bind; import com.oracle.truffle.api.dsl.Cached; -import com.oracle.truffle.api.dsl.Cached.Exclusive; import com.oracle.truffle.api.dsl.Cached.Shared; import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.GenerateCached; @@ -103,9 +101,9 @@ public class ZlibNodes { public static final int Z_BUF_ERROR = -5; public static final int Z_VERSION_ERROR = -6; - protected static void deallocateStream(Object zst, NFIZlibSupport zlibSupport, NativeLibrary.InvokeNativeFunction deallocateStream, boolean deallocate) { + protected static void deallocateStream(long zst, NFIZlibSupport zlibSupport, boolean deallocate) { if (deallocate) { - zlibSupport.deallocateStream(zst, deallocateStream); + zlibSupport.deallocateStream(zst); } } @@ -117,12 +115,11 @@ public abstract static class ZlibNativeCompressObj extends PNodeWithContext { @Specialization static byte[] nativeCompress(Node inliningTarget, NativeZlibCompObject self, PythonContext context, byte[] bytes, int len, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction compressObj, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); self.lastInput = bytes; - int err = zlibSupport.compressObj(self.getZst(), self.lastInput, len, DEF_BUF_SIZE, compressObj); + int err = zlibSupport.compressObj(self.getZst(), self.lastInput, len, DEF_BUF_SIZE); if (err != Z_OK) { errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); } @@ -139,21 +136,18 @@ public abstract static class ZlibNativeCompress extends PNodeWithContext { @Specialization static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int level, int wbits, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction createStream, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction deallocateStream, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction deflateOffHeap, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { PythonContext context = PythonContext.get(inliningTarget); - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] in = bytes; - Object zst = zlibSupport.createStream(createStream); - int err = zlibSupport.deflateOffHeap(zst, in, len, DEF_BUF_SIZE, level, wbits, deflateOffHeap); + long zst = zlibSupport.createStream(); + int err = zlibSupport.deflateOffHeap(zst, in, len, DEF_BUF_SIZE, level, wbits); if (err != Z_OK) { errorHandling.execute(inliningTarget, zst, err, zlibSupport, true); } byte[] resultArray = getBuffer.getOutputBuffer(inliningTarget, zst, context); - zlibSupport.deallocateStream(zst, deallocateStream); + zlibSupport.deallocateStream(zst); return resultArray; } } @@ -166,12 +160,11 @@ public abstract static class ZlibNativeDecompressObj extends PNodeWithContext { @Specialization static byte[] nativeDecompress(Node inliningTarget, NativeZlibCompObject self, PythonContext context, byte[] bytes, int len, int maxLength, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction decompressObj, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] in = bytes; - int err = zlibSupport.decompressObj(self.getZst(), in, len, DEF_BUF_SIZE, maxLength, decompressObj); + int err = zlibSupport.decompressObj(self.getZst(), in, len, DEF_BUF_SIZE, maxLength); if (err != Z_OK) { errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); } @@ -188,12 +181,11 @@ public abstract static class ZlibNativeDecompressor extends PNodeWithContext { @Specialization static byte[] nativeDecompressBuf(Node inliningTarget, ZlibDecompressorObject self, PythonContext context, byte[] bytes, int len, int maxLength, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction decompressor, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] in = bytes; - int ret = zlibSupport.decompressor(self.getZst(), in, len, maxLength, decompressor); + int ret = zlibSupport.decompressor(self.getZst(), in, len, maxLength); if (ret < 0) { errorHandling.execute(inliningTarget, self.getZst(), ret, zlibSupport, false); } @@ -210,20 +202,17 @@ public abstract static class ZlibNativeDecompress extends PNodeWithContext { @Specialization static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int wbits, int bufsize, PythonContext context, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction createStream, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction deallocateStream, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction inflateOffHeap, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - Object zst = zlibSupport.createStream(createStream); + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + long zst = zlibSupport.createStream(); byte[] in = bytes; - int err = zlibSupport.inflateOffHeap(zst, in, len, bufsize, wbits, inflateOffHeap); + int err = zlibSupport.inflateOffHeap(zst, in, len, bufsize, wbits); if (err != Z_OK) { errorHandling.execute(inliningTarget, zst, err, zlibSupport, true); } byte[] resultArray = getBuffer.getOutputBuffer(inliningTarget, zst, context); - zlibSupport.deallocateStream(zst, deallocateStream); + zlibSupport.deallocateStream(zst); return resultArray; } } @@ -233,13 +222,12 @@ static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int wbi @GenerateCached(false) public abstract static class ZlibNativeErrorHandling extends Node { - public abstract void execute(Node inliningTarget, Object zst, int err, NFIZlibSupport zlibSupport, boolean deallocate); + public abstract void execute(Node inliningTarget, long zst, int err, NFIZlibSupport zlibSupport, boolean deallocate); @Specialization - static void doError(Object zst, int err, NFIZlibSupport zlibSupport, boolean deallocate, - @Cached(inline = false) ZlibFunctionNativeErrorHandling errorHandling, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getErrorFunction) { - errorHandling.execute(zst, zlibSupport.getErrorFunction(zst, getErrorFunction), err, zlibSupport, deallocate); + static void doError(long zst, int err, NFIZlibSupport zlibSupport, boolean deallocate, + @Cached(inline = false) ZlibFunctionNativeErrorHandling errorHandling) { + errorHandling.execute(zst, zlibSupport.getErrorFunction(zst), err, zlibSupport, deallocate); } } @@ -248,32 +236,28 @@ static void doError(Object zst, int err, NFIZlibSupport zlibSupport, boolean dea @GenerateInline(false) public abstract static class ZlibNativeErrorMsg extends Node { - public abstract void execute(Object zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate); + public abstract void execute(long zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate); @SuppressWarnings("unused") @Specialization(guards = "err == Z_VERSION_ERROR") - static void doVersionError(Object zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate, + static void doVersionError(long zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("r") @Cached PRaiseNode raise) { /* * In case of a version mismatch, comp.msg won't be initialized. Check for this case * first, before looking at comp->zst.msg. */ - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ZLibError, ERROR_D_S_S, err, msg, LIBRARY_VERSION_MISMATCH); } @Specialization(guards = "err != Z_VERSION_ERROR") - static void doError(Object zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate, + static void doError(long zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, - @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction hasStreamErrorMsg, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction getStreamErrorMsg) { + @Shared("r") @Cached PRaiseNode raise) { TruffleString zmsg = null; - if (zlibSupport.hasStreamErrorMsg(zst, hasStreamErrorMsg) == 1) { - zmsg = zlibSupport.getStreamErrorMsg(zst, getStreamErrorMsg); + if (zlibSupport.hasStreamErrorMsg(zst) == 1) { + zmsg = zlibSupport.getStreamErrorMsg(zst); } else { switch (err) { case Z_BUF_ERROR: @@ -287,7 +271,7 @@ static void doError(Object zst, int err, TruffleString msg, NFIZlibSupport zlibS break; } } - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); if (zmsg == null) { throw raise.raise(inliningTarget, ZLibError, ERROR_D_S, err, msg); } else { @@ -301,75 +285,71 @@ static void doError(Object zst, int err, TruffleString msg, NFIZlibSupport zlibS @GenerateInline(false) public abstract static class ZlibFunctionNativeErrorHandling extends Node { - public abstract void execute(Object zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate); + public abstract void execute(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate); @Specialization(guards = "function == DEFLATE_INIT_ERROR") - static void deflateInitError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { if (err == Z_MEM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError, OUT_OF_MEMORY_WHILE_S_DATA, "compressing"); } if (err == Z_STREAM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ZLibError, ErrorMessages.BAD_COMPRESSION_LEVEL); } zlibError.execute(zst, err, formatNode.format(WHILE_S_DATA, "compressing"), zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_OBJ_ERROR") - static void deflateObjInitError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateObjInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { if (err == Z_MEM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError, CANT_ALLOCATE_MEMORY_FOR_S_OBJECT, "compression"); } if (err == Z_STREAM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ValueError, INVALID_INITIALIZATION_OPTION); } zlibError.execute(zst, err, formatNode.format(WHILE_CREATING_S_OBJECT, "compression"), zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_COPY_ERROR") - static void deflateCopyError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateCopyError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { if (err == Z_MEM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError, CANT_ALLOCATE_MEMORY_FOR_S_OBJECT, "compression"); } if (err == Z_STREAM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ValueError, INCONSISTENT_STREAM_STATE); } zlibError.execute(zst, err, formatNode.format(WHILE_COPYING_S_OBJECT, "compression"), zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_COPY_ERROR") - static void inflateCopyError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateCopyError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { if (err == Z_MEM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError, CANT_ALLOCATE_MEMORY_FOR_S_OBJECT, "decompression"); } if (err == Z_STREAM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ValueError, INCONSISTENT_STREAM_STATE); } zlibError.execute(zst, err, formatNode.format(WHILE_COPYING_S_OBJECT, "compression"), zlibSupport, deallocate); @@ -377,12 +357,11 @@ static void inflateCopyError(Object zst, @SuppressWarnings("unused") int functio @SuppressWarnings("unused") @Specialization(guards = "function == DEFLATE_DICT_ERROR") - static void deflateDictError(Object zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateDictError(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("r") @Cached PRaiseNode raise) { if (err == Z_STREAM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ValueError, INVALID_DICTIONARY); } throw raise.raise(inliningTarget, ValueError, ErrorMessages.DEFLATED_SET_DICT); @@ -390,14 +369,13 @@ static void deflateDictError(Object zst, int function, int err, NFIZlibSupport z } @Specialization(guards = "function == INFLATE_INIT_ERROR") - static void inflateInitError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { if (err == Z_MEM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError, OUT_OF_MEMORY_WHILE_S_DATA, "decompressing"); } zlibError.execute(zst, err, formatNode.format(WHILE_PREPARING_TO_S_DATA, "decompress"), zlibSupport, deallocate); @@ -405,82 +383,80 @@ static void inflateInitError(Object zst, @SuppressWarnings("unused") int functio } @Specialization(guards = "function == INFLATE_OBJ_ERROR") - static void inflateObjInitError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateObjInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { if (err == Z_MEM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError, CANT_ALLOCATE_MEMORY_FOR_S_OBJECT, "decompression"); } if (err == Z_STREAM_ERROR) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, ValueError, INVALID_INITIALIZATION_OPTION); } zlibError.execute(zst, err, formatNode.format(WHILE_CREATING_S_OBJECT, "decompression"), zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_DICT_ERROR") - static void inflateDictError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateDictError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError) { zlibError.execute(zst, err, WHILE_SETTING_ZDICT, zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_END_ERROR") - static void deflateEndError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateEndError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_FINISHING_S, "compression"), zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_END_ERROR") - static void inflateEndError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateEndError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_FINISHING_S, "decompression"), zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_ERROR") - static void deflateError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_S_DATA, "compressing"), zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_ERROR") - static void inflateError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_S_DATA, "decompressing"), zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_FLUSH_ERROR") - static void deflateFlushError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateFlushError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError) { zlibError.execute(zst, err, WHILE_FLUSHING, zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_FLUSH_ERROR") - static void inflateFlushError(Object zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateFlushError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError) { zlibError.execute(zst, err, WHILE_FLUSHING, zlibSupport, deallocate); } @SuppressWarnings("unused") @Specialization(guards = "function == MEMORY_ERROR") - static void memError(Object zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void memError(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, - @Shared("d") @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Shared("r") @Cached PRaiseNode raise) { - deallocateStream(zst, zlibSupport, deallocateStream, deallocate); + deallocateStream(zst, zlibSupport, deallocate); throw raise.raise(inliningTarget, MemoryError); } @SuppressWarnings("unused") @Fallback - void fallback(Object zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate) { + void fallback(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate) { throw PRaiseNode.raiseStatic(this, SystemError, ErrorMessages.UNHANDLED_ERROR); } } @@ -494,9 +470,8 @@ public abstract static class NativeDeallocation extends PNodeWithContext { @Specialization(guards = "isCompressObj") static void doCompressObj(NativeZlibCompObject self, PythonContext context, - @SuppressWarnings("unused") boolean isCompressObj, - @Shared @Cached(inline = false) NativeLibrary.InvokeNativeFunction deallocateStream) { - context.getNFIZlibSupport().deallocateStream(self.getZst(), deallocateStream); + @SuppressWarnings("unused") boolean isCompressObj) { + context.getNativeZlibSupport().deallocateStream(self.getZst()); self.setEof(true); self.markReleased(); } @@ -505,14 +480,13 @@ static void doCompressObj(NativeZlibCompObject self, PythonContext context, static void doDecompressObj(Node inliningTarget, NativeZlibCompObject self, PythonContext context, @SuppressWarnings("unused") boolean isCompressObj, @Cached GetNativeBufferNode getUnusedDataBuffer, - @Cached GetNativeBufferNode getUnconsumedBuffer, - @Shared @Cached(inline = false) NativeLibrary.InvokeNativeFunction deallocateStream) { + @Cached GetNativeBufferNode getUnconsumedBuffer) { byte[] unusedData = getUnusedDataBuffer.getUnusedDataBuffer(inliningTarget, self.getZst(), context); PythonLanguage language = context.getLanguage(inliningTarget); self.setUnusedData(PFactory.createBytes(language, unusedData)); byte[] unconsumed = getUnconsumedBuffer.getUnconsumedTailBuffer(inliningTarget, self.getZst(), context); self.setUnconsumedTail(PFactory.createBytes(language, unconsumed)); - context.getNFIZlibSupport().deallocateStream(self.getZst(), deallocateStream); + context.getNativeZlibSupport().deallocateStream(self.getZst()); self.setEof(true); self.markReleased(); } @@ -523,31 +497,29 @@ static void doDecompressObj(Node inliningTarget, NativeZlibCompObject self, Pyth @GenerateCached(false) public abstract static class GetNativeBufferNode extends PNodeWithContext { - public abstract byte[] execute(Node inliningTarget, Object zst, int option, PythonContext context); + public abstract byte[] execute(Node inliningTarget, long zst, int option, PythonContext context); - public byte[] getOutputBuffer(Node inliningTarget, Object zst, PythonContext context) { + public byte[] getOutputBuffer(Node inliningTarget, long zst, PythonContext context) { return execute(inliningTarget, zst, OUTPUT_OPTION, context); } - public byte[] getUnusedDataBuffer(Node inliningTarget, Object zst, PythonContext context) { + public byte[] getUnusedDataBuffer(Node inliningTarget, long zst, PythonContext context) { return execute(inliningTarget, zst, UNUSED_DATA_OPTION, context); } - public byte[] getUnconsumedTailBuffer(Node inliningTarget, Object zst, PythonContext context) { + public byte[] getUnconsumedTailBuffer(Node inliningTarget, long zst, PythonContext context) { return execute(inliningTarget, zst, UNCONSUMED_TAIL_OPTION, context); } @Specialization - static byte[] getBuffer(Object zst, int option, PythonContext context, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getBufferSize, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getBuffer) { - NFIZlibSupport zlibSupport = context.getNFIZlibSupport(); - int size = zlibSupport.getBufferSize(zst, option, getBufferSize); + static byte[] getBuffer(long zst, int option, PythonContext context) { + NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + int size = zlibSupport.getBufferSize(zst, option); if (size == 0) { return PythonUtils.EMPTY_BYTE_ARRAY; } byte[] resultArray = new byte[size]; - zlibSupport.getBuffer(zst, option, resultArray, getBuffer); + zlibSupport.getBuffer(zst, option, resultArray); return resultArray; } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java index c185d60248..58bebd4dd5 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java @@ -521,6 +521,13 @@ public FinalizableReference(Object referent, Object reference, SharedFinalizer s addLiveReference(sharedFinalizer, this); } + @SuppressWarnings("this-escape") + protected FinalizableReference(Object referent, SharedFinalizer sharedFinalizer) { + super(referent, sharedFinalizer.queue); + this.reference = null; + addLiveReference(sharedFinalizer, this); + } + /** * We'll keep a reference for the FinalizableReference object until the async handler * schedule the collect process. diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java index 767d3d6399..863de1ffef 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -40,21 +40,25 @@ */ package com.oracle.graal.python.runtime; +import static com.oracle.graal.python.annotations.NativeSimpleType.DOUBLE; +import static com.oracle.graal.python.annotations.NativeSimpleType.POINTER; +import static com.oracle.graal.python.annotations.NativeSimpleType.SINT32; +import static com.oracle.graal.python.annotations.NativeSimpleType.SINT64; +import static com.oracle.graal.python.annotations.NativeSimpleType.VOID; + import com.oracle.graal.python.PythonLanguage; -import com.oracle.truffle.api.CompilerAsserts; -import com.oracle.truffle.api.CompilerDirectives; -import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; +import com.oracle.graal.python.annotations.DowncallSignature; +import com.oracle.graal.python.runtime.nativeaccess.NativeLibrary; +import com.oracle.graal.python.runtime.nativeaccess.NativeMemory; +import com.oracle.graal.python.runtime.nativeaccess.NativeMemory.ZeroTerminatedUtf8ToTruffleStringNode; import com.oracle.truffle.api.ThreadLocalAction.Access; import com.oracle.truffle.api.TruffleLogger; import com.oracle.truffle.api.strings.TruffleString; -/*- - * Generated using: - * scripts/nfi_gen.py -name Zlib -cpath graalpython/com.oracle.graal.python.cext/zlib/zlib.c -lib libzsupport - */ -public class NFIZlibSupport { +public class NFIZlibSupport extends NativeCompressionSupport { private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFIZlibSupport.class); + private static final String SUPPORTING_NATIVE_LIB_NAME = "zsupport"; public static final int NO_ERROR = 0; public static final int DEFLATE_INIT_ERROR = 101; @@ -78,216 +82,108 @@ public class NFIZlibSupport { public static final int UNCONSUMED_TAIL_OPTION = 2; public static final int ZDICT_OPTION = 3; - enum ZlibNativeFunctions implements NativeLibrary.NativeFunction { - - /*- - nfi_function: name('zlibVersion') static(true) - char *zlib_get_version() - */ - zlib_get_version("(): STRING"), - - /*- - nfi_function: name('zlibRuntimeVersion') static(true) - char *zlib_get_runtime_version() - */ - zlib_get_runtime_version("(): STRING"), - - /*- - nfi_function: name('crc32') - uLong zlib_crc32(uLong crc, Byte *buf, uInt len) - */ - zlib_crc32("(UINT64, [UINT8], UINT32): UINT64"), - - /*- - nfi_function: name('adler32') - uLong zlib_adler32(uLong crc, Byte *buf, uInt len) - */ - zlib_adler32("(UINT64, [UINT8], UINT32): UINT64"), - - /*- - nfi_function: name('createStream') map('zlib_stream*', 'POINTER') - zlib_stream *zlib_create_zlib_stream() - */ - zlib_create_zlib_stream("(): POINTER"), - - /*- - nfi_function: name('getTimeElapsed') map('zlib_stream*', 'POINTER') static(true) - double zlib_get_timeElapsed(zlib_stream* zst) - */ - zlib_get_timeElapsed("(POINTER): DOUBLE"), - - /*- - nfi_function: name('deallocateStream') map('zlib_stream*', 'POINTER') - void zlib_free_stream(zlib_stream* zst) - */ - zlib_free_stream("(POINTER): VOID"), - - /*- - nfi_function: name('gcReleaseHelper') map('zlib_stream*', 'POINTER') release(true) - void zlib_gc_helper(zlib_stream* zst) - */ - zlib_gc_helper("(POINTER): VOID"), - - /*- - nfi_function: name('getErrorFunction') map('zlib_stream*', 'POINTER') - int zlib_get_error_type(zlib_stream *zst) - */ - zlib_get_error_type("(POINTER): SINT32"), - - /*- - nfi_function: name('getStreamErrorMsg') map('zlib_stream*', 'POINTER') - char *zlib_get_stream_msg(zlib_stream *zst) - */ - zlib_get_stream_msg("(POINTER): STRING"), - - /*- - nfi_function: name('hasStreamErrorMsg') map('zlib_stream*', 'POINTER') - int zlib_has_stream_msg(zlib_stream *zst) - */ - zlib_has_stream_msg("(POINTER): SINT32"), - - /*- - nfi_function: name('getEOF') map('zlib_stream*', 'POINTER') - int zlib_get_eof(zlib_stream *zst) - */ - zlib_get_eof("(POINTER): SINT32"), - - /*- - nfi_function: name('getIsInitialised') map('zlib_stream*', 'POINTER') - int zlib_get_is_initialised(zlib_stream *zst) - */ - zlib_get_is_initialised("(POINTER): SINT32"), - - /*- - nfi_function: name('getBufferSize') map('zlib_stream*', 'POINTER') - uInt zlib_get_buffer_size(zlib_stream *zst, int option) - */ - zlib_get_buffer_size("(POINTER, SINT32): UINT32"), - - /*- - nfi_function: name('getBuffer') map('zlib_stream*', 'POINTER') - void zlib_get_off_heap_buffer(zlib_stream *zst, int option, Byte *dest) - */ - zlib_get_off_heap_buffer("(POINTER, SINT32, [UINT8]): VOID"), - - /*- - nfi_function: name('createCompObject') map('zlib_stream*', 'POINTER') - zlib_stream *zlib_create_compobject() - */ - zlib_create_compobject("(): POINTER"), - - /*- - nfi_function: name('deflateOffHeap') map('zlib_stream*', 'POINTER') - int zlib_deflate_off_heap(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size, int level) - */ - zlib_deflate_off_heap("(POINTER, [UINT8], SINT64, SINT64, SINT32, SINT32): SINT32"), - - /*- - nfi_function: name('inflateOffHeap') map('zlib_stream*', 'POINTER') - int zlib_inflate_off_heap(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size, int wbits) - */ - zlib_inflate_off_heap("(POINTER, [UINT8], SINT64, SINT64, SINT32): SINT32"), - - /*- - nfi_function: name('compressObjInitWithDict') map('zlib_stream*', 'POINTER') - int zlib_Compress_init(zlib_stream *zst, int level, int method,int wbits, int memLevel,int strategy, Byte *dict, size_t dict_len) - */ - zlib_Compress_init("(POINTER, SINT32, SINT32, SINT32, SINT32, SINT32, [UINT8], UINT64): SINT32"), - - /*- - nfi_function: name('compressObjInit') map('zlib_stream*', 'POINTER') - int zlib_Compress_init_no_dict(zlib_stream *zst, int level, int method,int wbits, int memLevel,int strategy) - */ - zlib_Compress_init_no_dict("(POINTER, SINT32, SINT32, SINT32, SINT32, SINT32): SINT32"), - - /*- - nfi_function: name('compressObj') map('zlib_stream*', 'POINTER') - int zlib_Compress_obj(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size) - */ - zlib_Compress_obj("(POINTER, [UINT8], SINT64, SINT64): SINT32"), - - /*- - nfi_function: name('compressObjFlush') map('zlib_stream*', 'POINTER') - int zlib_Compress_flush(zlib_stream *zst, Byte *in, ssize_t buf_size, int mode) - */ - zlib_Compress_flush("(POINTER, [UINT8], SINT64, SINT32): SINT32"), - - /*- - nfi_function: name('compressObjCopy') map('zlib_stream*', 'POINTER') - int zlib_Compress_copy(zlib_stream *zst, zlib_stream *new_copy) - */ - zlib_Compress_copy("(POINTER, POINTER): SINT32"), - - /*- - nfi_function: name('decompressObjInitWithDict') map('zlib_stream*', 'POINTER') - int zlib_Decompress_init(zlib_stream *zst, int wbits, Byte *dict, size_t dict_len) - */ - zlib_Decompress_init("(POINTER, SINT32, [UINT8], UINT64): SINT32"), - - /*- - nfi_function: name('decompressObjInit') map('zlib_stream*', 'POINTER') - int zlib_Decompress_init_no_dict(zlib_stream *zst, int wbits) - */ - zlib_Decompress_init_no_dict("(POINTER, SINT32): SINT32"), - - /*- - nfi_function: name('decompressObj') map('zlib_stream*', 'POINTER') - int zlib_Decompress_obj(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size, ssize_t max_length) - */ - zlib_Decompress_obj("(POINTER, [UINT8], SINT64, SINT64, SINT64): SINT32"), - - /*- - nfi_function: name('decompressObjFlush') map('zlib_stream*', 'POINTER') - int zlib_Decompress_flush(zlib_stream *zst, ssize_t length) - */ - zlib_Decompress_flush("(POINTER, SINT64): SINT32"), - - /*- - nfi_function: name('decompressObjCopy') map('zlib_stream*', 'POINTER') - int zlib_Decompress_copy(zlib_stream *zst, zlib_stream *new_copy) - */ - zlib_Decompress_copy("(POINTER, POINTER): SINT32"), - - zlib_decompress("(POINTER, [UINT8], SINT64, SINT64): SINT32"); - - private final String signature; - - ZlibNativeFunctions(String signature) { - this.signature = signature; - } + abstract static class ZlibNativeFunctions { + @DowncallSignature(returnType = POINTER) + abstract long zlib_get_version(); - @Override - public String signature() { - return signature; - } - } + @DowncallSignature(returnType = POINTER) + abstract long zlib_get_runtime_version(); - private static final String SUPPORTING_NATIVE_LIB_NAME = "zsupport"; + @DowncallSignature(returnType = SINT64, argumentTypes = {SINT64, POINTER, SINT32}) + abstract long zlib_crc32(long crc, long buf, int len); + + @DowncallSignature(returnType = SINT64, argumentTypes = {SINT64, POINTER, SINT32}) + abstract long zlib_adler32(long crc, long buf, int len); + + @DowncallSignature(returnType = POINTER) + abstract long zlib_create_zlib_stream(); + + @DowncallSignature(returnType = DOUBLE, argumentTypes = {POINTER}) + abstract double zlib_get_timeElapsed(long zst); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER}) + abstract void zlib_free_stream(long zst); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER}) + abstract void zlib_gc_helper(long zst); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int zlib_get_error_type(long zst); + + @DowncallSignature(returnType = POINTER, argumentTypes = {POINTER}) + abstract long zlib_get_stream_msg(long zst); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int zlib_has_stream_msg(long zst); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int zlib_get_eof(long zst); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int zlib_get_is_initialised(long zst); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32}) + abstract int zlib_get_buffer_size(long zst, int option); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER, SINT32, POINTER}) + abstract void zlib_get_off_heap_buffer(long zst, int option, long dest); + + @DowncallSignature(returnType = POINTER) + abstract long zlib_create_compobject(); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64, SINT32, SINT32}) + abstract int zlib_deflate_off_heap(long zst, long in, long inLen, long bufSize, int level, int wbits); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64, SINT32}) + abstract int zlib_inflate_off_heap(long zst, long in, long inLen, long bufSize, int wbits); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, SINT32, SINT32, SINT32, SINT32, POINTER, SINT64}) + abstract int zlib_Compress_init(long zst, int level, int method, int wbits, int memLevel, int strategy, long dict, long dictLen); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, SINT32, SINT32, SINT32, SINT32}) + abstract int zlib_Compress_init_no_dict(long zst, int level, int method, int wbits, int memLevel, int strategy); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64}) + abstract int zlib_Compress_obj(long zst, long in, long inLen, long bufSize); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT32}) + abstract int zlib_Compress_flush(long zst, long in, long bufSize, int mode); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER}) + abstract int zlib_Compress_copy(long zst, long newCopy); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, POINTER, SINT64}) + abstract int zlib_Decompress_init(long zst, int wbits, long dict, long dictLen); - private final PythonContext pythonContext; - private final NativeLibrary.TypedNativeLibrary typedNativeLib; - - @CompilerDirectives.CompilationFinal private boolean available; - - private NFIZlibSupport(PythonContext context, NativeLibrary.NFIBackend backend, String noNativeAccessHelp) { - if (context.useNativeCompressionModules()) { - this.pythonContext = context; - this.typedNativeLib = NativeLibrary.create(PythonContext.getSupportLibName(SUPPORTING_NATIVE_LIB_NAME), - ZlibNativeFunctions.values(), backend, noNativeAccessHelp, true); - this.available = true; - } else { - this.pythonContext = null; - this.typedNativeLib = null; - this.available = false; + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32}) + abstract int zlib_Decompress_init_no_dict(long zst, int wbits); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64, SINT64}) + abstract int zlib_Decompress_obj(long zst, long in, long inLen, long bufSize, long maxLength); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT64}) + abstract int zlib_Decompress_flush(long zst, long length); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER}) + abstract int zlib_Decompress_copy(long zst, long newCopy); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64}) + abstract int zlib_decompress(long zst, long data, long len, long maxLength); + + static NativeLibrary loadNativeLibrary(PythonContext context) { + return NativeCompressionSupport.loadNativeLibrary(context, SUPPORTING_NATIVE_LIB_NAME); } } - public static NFIZlibSupport createNative(PythonContext context, String noNativeAccessHelp) { - return new NFIZlibSupport(context, NativeLibrary.NFIBackend.NATIVE, noNativeAccessHelp); + private final ZlibNativeFunctions nativeFunctions; + + private NFIZlibSupport(PythonContext context) { + super(context); + this.nativeFunctions = isAvailable() ? new ZlibNativeFunctionsGen(context) : null; } - public static NFIZlibSupport createLLVM(PythonContext context, String noNativeAccessHelp) { - return new NFIZlibSupport(context, NativeLibrary.NFIBackend.LLVM, noNativeAccessHelp); + public static NFIZlibSupport createNative(PythonContext context, String noNativeAccessHelp) { + return new NFIZlibSupport(context); } static class PointerReleaseCallback implements AsyncHandler.AsyncAction { @@ -299,17 +195,15 @@ public PointerReleaseCallback(Pointer pointer) { @Override public void execute(PythonContext context, Access access) { - synchronized (pointer) { - if (pointer.isReleased()) { - return; - } - try { - pointer.doRelease(); - pointer.markReleased(); - LOGGER.finest("NFIZlibSupport pointer has been freed"); - } catch (Exception e) { - LOGGER.severe("Error while trying to free NFIZlibSupport pointer: " + e.getMessage()); - } + if (!pointer.markReleased()) { + assert pointer.isReleased(); + return; + } + try { + pointer.doRelease(); + LOGGER.finest("NFIZlibSupport pointer has been freed"); + } catch (Exception e) { + LOGGER.severe("Error while trying to free NFIZlibSupport pointer: " + e.getMessage()); } } } @@ -317,14 +211,20 @@ public void execute(PythonContext context, Access access) { public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference { private final NFIZlibSupport lib; + private final long pointer; - public Pointer(Object referent, Object ptr, NFIZlibSupport lib) { - super(referent, ptr, lib.pythonContext.getSharedFinalizer()); + public Pointer(Object referent, long pointer, NFIZlibSupport lib) { + super(referent, lib.pythonContext.getSharedFinalizer()); this.lib = lib; + this.pointer = pointer; + } + + public long getPointer() { + return pointer; } protected void doRelease() { - lib.gcReleaseHelper(getReference()); + lib.gcReleaseHelper(pointer); } @Override @@ -336,361 +236,205 @@ public AsyncHandler.AsyncAction release() { } } - @TruffleBoundary - public void notAvailable() { - if (available) { - CompilerDirectives.transferToInterpreterAndInvalidate(); - CompilerAsserts.neverPartOfCompilation("Checking NFIZlibSupport availability should only be done during initialization."); - available = false; + public TruffleString zlibVersion() { + return ZeroTerminatedUtf8ToTruffleStringNode.executeUncached(nativeFunctions.zlib_get_version()); + } + + public TruffleString zlibRuntimeVersion() { + return ZeroTerminatedUtf8ToTruffleStringNode.executeUncached(nativeFunctions.zlib_get_runtime_version()); + } + + public Object getTimeElapsed(long zst) { + return nativeFunctions.zlib_get_timeElapsed(zst); + } + + public Object gcReleaseHelper(long zst) { + nativeFunctions.zlib_gc_helper(zst); + return null; + } + + public long crc32(long crc, byte[] buf, int len) { + if (len == 0) { + return crc; + } + long nativeBuf = copyToNativeByteArray(buf, len); + try { + return nativeFunctions.zlib_crc32(crc, nativeBuf, len); + } finally { + if (nativeBuf != NativeMemory.NULLPTR) { + NativeMemory.free(nativeBuf); + } + } + } + + public long adler32(long crc, byte[] buf, int len) { + if (len == 0) { + return crc; + } + long nativeBuf = copyToNativeByteArray(buf, len); + try { + return nativeFunctions.zlib_adler32(crc, nativeBuf, len); + } finally { + if (nativeBuf != NativeMemory.NULLPTR) { + NativeMemory.free(nativeBuf); + } } } - public boolean isAvailable() { - return available; + public long createStream() { + return nativeFunctions.zlib_create_zlib_stream(); } - @TruffleBoundary - public void setAvailable() { - if (!available && typedNativeLib != null) { - CompilerDirectives.transferToInterpreterAndInvalidate(); - CompilerAsserts.neverPartOfCompilation("Checking NFIZlibSupport availability should only be done during initialization."); - available = true; + public void deallocateStream(long zst) { + nativeFunctions.zlib_free_stream(zst); + } + + public int getErrorFunction(long zst) { + return nativeFunctions.zlib_get_error_type(zst); + } + + public TruffleString getStreamErrorMsg(long zst) { + return ZeroTerminatedUtf8ToTruffleStringNode.executeUncached(nativeFunctions.zlib_get_stream_msg(zst)); + } + + public int hasStreamErrorMsg(long zst) { + return nativeFunctions.zlib_has_stream_msg(zst); + } + + public int getEOF(long zst) { + return nativeFunctions.zlib_get_eof(zst); + } + + public int getIsInitialised(long zst) { + return nativeFunctions.zlib_get_is_initialised(zst); + } + + public int getBufferSize(long zst, int option) { + return nativeFunctions.zlib_get_buffer_size(zst, option); + } + + public void getBuffer(long zst, int option, byte[] dest) { + if (dest.length == 0) { + return; + } + long nativeDest = NativeMemory.mallocByteArray(dest.length); + try { + nativeFunctions.zlib_get_off_heap_buffer(zst, option, nativeDest); + NativeMemory.readByteArrayElements(nativeDest, 0, dest, 0, dest.length); + } finally { + NativeMemory.free(nativeDest); + } + } + + public long createCompObject() { + return nativeFunctions.zlib_create_compobject(); + } + + public int deflateOffHeap(long zst, byte[] in, long inLen, long bufSize, int level, int wbits) { + long nativeIn = copyToNativeByteArray(in, (int) inLen); + try { + return nativeFunctions.zlib_deflate_off_heap(zst, nativeIn, inLen, bufSize, level, wbits); + } finally { + if (nativeIn != NativeMemory.NULLPTR) { + NativeMemory.free(nativeIn); + } + } + } + + public int inflateOffHeap(long zst, byte[] in, long inLen, long bufSize, int wbits) { + long nativeIn = copyToNativeByteArray(in, (int) inLen); + try { + return nativeFunctions.zlib_inflate_off_heap(zst, nativeIn, inLen, bufSize, wbits); + } finally { + if (nativeIn != NativeMemory.NULLPTR) { + NativeMemory.free(nativeIn); + } + } + } + + public int compressObjInitWithDict(long zst, int level, int method, int wbits, int memLevel, int strategy, byte[] dict, long dictLen) { + long nativeDict = copyToNativeByteArray(dict, (int) dictLen); + try { + return nativeFunctions.zlib_Compress_init(zst, level, method, wbits, memLevel, strategy, nativeDict, dictLen); + } finally { + if (nativeDict != NativeMemory.NULLPTR) { + NativeMemory.free(nativeDict); + } } } - /** - * - * - * @return char* - */ - public Object zlibVersion() { - return typedNativeLib.callUncached(pythonContext, ZlibNativeFunctions.zlib_get_version); - } - - /** - * - * - * @return char* - */ - public Object zlibRuntimeVersion() { - return typedNativeLib.callUncached(pythonContext, ZlibNativeFunctions.zlib_get_runtime_version); - } - - /** - * - * @param zst zlib_stream* zst - * @return double - */ - public Object getTimeElapsed(Object zst) { - return typedNativeLib.callUncached(pythonContext, ZlibNativeFunctions.zlib_get_timeElapsed, zst); - } - - /** - * - * @param zst zlib_stream* zst - * - */ - public Object gcReleaseHelper(Object zst) { - return typedNativeLib.callUncached(pythonContext, ZlibNativeFunctions.zlib_gc_helper, zst); - } - - /** - * - * @param crc uLong crc - * @param buf Byte *buf - * @param len uInt len - * @return uLong - */ - public long crc32(long crc, byte[] buf, int len, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, ZlibNativeFunctions.zlib_crc32, crc, buf, len); - } - - /** - * - * @param crc uLong crc - * @param buf Byte *buf - * @param len uInt len - * @return uLong - */ - public long adler32(long crc, byte[] buf, int len, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, ZlibNativeFunctions.zlib_adler32, crc, buf, len); - } - - /** - * - * - * @return zlib_stream* - */ - public Object createStream( - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.call(typedNativeLib, ZlibNativeFunctions.zlib_create_zlib_stream); - } - - /** - * - * @param zst zlib_stream* zst - * - */ - public void deallocateStream(Object zst, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, ZlibNativeFunctions.zlib_free_stream, zst); - } - - /** - * - * @param zst zlib_stream *zst - * @return int - */ - public int getErrorFunction(Object zst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_get_error_type, zst); - } - - /** - * - * @param zst zlib_stream *zst - * @return char* - */ - public TruffleString getStreamErrorMsg(Object zst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callString(typedNativeLib, ZlibNativeFunctions.zlib_get_stream_msg, zst); - } - - /** - * - * @param zst zlib_stream *zst - * @return int - */ - public int hasStreamErrorMsg(Object zst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_has_stream_msg, zst); - } - - /** - * - * @param zst zlib_stream *zst - * @return int - */ - public int getEOF(Object zst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_get_eof, zst); - } - - /** - * - * @param zst zlib_stream *zst - * @return int - */ - public int getIsInitialised(Object zst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_get_is_initialised, zst); - } - - /** - * - * @param zst zlib_stream *zst - * @param option int option - * @return uInt - */ - public int getBufferSize(Object zst, int option, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_get_buffer_size, zst, option); - } - - /** - * - * @param zst zlib_stream *zst - * @param option int option - * @param dest Byte *dest - * - */ - public void getBuffer(Object zst, int option, byte[] dest, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, ZlibNativeFunctions.zlib_get_off_heap_buffer, zst, option, dest); - } - - /** - * - * - * @return zlib_stream* - */ - public Object createCompObject( - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.call(typedNativeLib, ZlibNativeFunctions.zlib_create_compobject); - } - - /** - * - * @param zst zlib_stream *zst - * @param in Byte *in - * @param in_len ssize_t in_len - * @param buf_size ssize_t buf_size - * @param level int level - * @param wbits int window bits - * @return int - */ - public int deflateOffHeap(Object zst, byte[] in, long in_len, long buf_size, int level, int wbits, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_deflate_off_heap, zst, in, in_len, buf_size, level, wbits); - } - - /** - * - * @param zst zlib_stream *zst - * @param in Byte *in - * @param in_len ssize_t in_len - * @param buf_size ssize_t buf_size - * @param wbits int wbits - * @return int - */ - public int inflateOffHeap(Object zst, byte[] in, long in_len, long buf_size, int wbits, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_inflate_off_heap, zst, in, in_len, buf_size, wbits); - } - - /** - * - * @param zst zlib_stream *zst - * @param level int level - * @param method int method - * @param wbits int wbits - * @param memLevel int memLevel - * @param strategy int strategy - * @param dict Byte *dict - * @param dict_len size_t dict_len - * @return int - */ - public int compressObjInitWithDict(Object zst, int level, int method, int wbits, int memLevel, int strategy, byte[] dict, long dict_len, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Compress_init, zst, level, method, wbits, memLevel, strategy, dict, dict_len); - } - - /** - * - * @param zst zlib_stream *zst - * @param level int level - * @param method int method - * @param wbits int wbits - * @param memLevel int memLevel - * @param strategy int strategy - * @return int - */ - public int compressObjInit(Object zst, int level, int method, int wbits, int memLevel, int strategy, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Compress_init_no_dict, zst, level, method, wbits, memLevel, strategy); - } - - /** - * - * @param zst zlib_stream *zst - * @param in Byte *in - * @param in_len ssize_t in_len - * @param buf_size ssize_t buf_size - * @return int - */ - public int compressObj(Object zst, Object in, long in_len, long buf_size, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Compress_obj, zst, in, in_len, buf_size); - } - - /** - * - * @param zst zlib_stream *zst - * @param in Byte *in - * @param buf_size ssize_t buf_size - * @param mode int mode - * @return int - */ - public int compressObjFlush(Object zst, byte[] in, long buf_size, int mode, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Compress_flush, zst, in, buf_size, mode); - } - - /** - * - * @param zst zlib_stream *zst - * @param new_copy zlib_stream *new_copy - * @return int - */ - public int compressObjCopy(Object zst, Object new_copy, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Compress_copy, zst, new_copy); - } - - /** - * - * @param zst zlib_stream *zst - * @param wbits int wbits - * @param dict Byte *dict - * @param dict_len size_t dict_len - * @return int - */ - public int decompressObjInitWithDict(Object zst, int wbits, byte[] dict, long dict_len, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Decompress_init, zst, wbits, dict, dict_len); - } - - /** - * - * @param zst zlib_stream *zst - * @param wbits int wbits - * @return int - */ - public int decompressObjInit(Object zst, int wbits, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Decompress_init_no_dict, zst, wbits); - } - - /** - * - * @param zst zlib_stream *zst - * @param in Byte *in - * @param in_len ssize_t in_len - * @param buf_size ssize_t buf_size - * @param max_length ssize_t max_length - * @return int - */ - public int decompressObj(Object zst, byte[] in, long in_len, long buf_size, long max_length, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Decompress_obj, zst, in, in_len, buf_size, max_length); - } - - /** - * - * @param zst zlib_stream *zst - * @param length ssize_t length - * @return int - */ - public int decompressObjFlush(Object zst, long length, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Decompress_flush, zst, length); - } - - /** - * - * @param zst zlib_stream *zst - * @param new_copy zlib_stream *new_copy - * @return int - */ - public int decompressObjCopy(Object zst, Object new_copy, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_Decompress_copy, zst, new_copy); - } - - /** - * returns needs_input flag if the value isn't less than 0, otherwise it returns the zlib error - * code. - * - * @param zst zlib_stream *zst - * @param data Byte *data - * @param len size_t len - * @param max_length ssize_t max_length - * @return int - */ - public int decompressor(Object zst, byte[] data, long len, long max_length, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, ZlibNativeFunctions.zlib_decompress, zst, data, len, max_length); + public int compressObjInit(long zst, int level, int method, int wbits, int memLevel, int strategy) { + return nativeFunctions.zlib_Compress_init_no_dict(zst, level, method, wbits, memLevel, strategy); + } + + public int compressObj(long zst, Object in, long inLen, long bufSize) { + long nativeIn = copyToNativeByteArray((byte[]) in, (int) inLen); + try { + return nativeFunctions.zlib_Compress_obj(zst, nativeIn, inLen, bufSize); + } finally { + if (nativeIn != NativeMemory.NULLPTR) { + NativeMemory.free(nativeIn); + } + } + } + + public int compressObjFlush(long zst, byte[] in, long bufSize, int mode) { + long nativeIn = copyToNativeByteArray(in); + try { + return nativeFunctions.zlib_Compress_flush(zst, nativeIn, bufSize, mode); + } finally { + if (nativeIn != NativeMemory.NULLPTR) { + NativeMemory.free(nativeIn); + } + } + } + + public int compressObjCopy(long zst, long newCopy) { + return nativeFunctions.zlib_Compress_copy(zst, newCopy); + } + + public int decompressObjInitWithDict(long zst, int wbits, byte[] dict, long dictLen) { + long nativeDict = copyToNativeByteArray(dict, (int) dictLen); + try { + return nativeFunctions.zlib_Decompress_init(zst, wbits, nativeDict, dictLen); + } finally { + if (nativeDict != NativeMemory.NULLPTR) { + NativeMemory.free(nativeDict); + } + } + } + + public int decompressObjInit(long zst, int wbits) { + return nativeFunctions.zlib_Decompress_init_no_dict(zst, wbits); + } + + public int decompressObj(long zst, byte[] in, long inLen, long bufSize, long maxLength) { + long nativeIn = copyToNativeByteArray(in, (int) inLen); + try { + return nativeFunctions.zlib_Decompress_obj(zst, nativeIn, inLen, bufSize, maxLength); + } finally { + if (nativeIn != NativeMemory.NULLPTR) { + NativeMemory.free(nativeIn); + } + } + } + + public int decompressObjFlush(long zst, long length) { + return nativeFunctions.zlib_Decompress_flush(zst, length); + } + + public int decompressObjCopy(long zst, long newCopy) { + return nativeFunctions.zlib_Decompress_copy(zst, newCopy); + } + + public int decompressor(long zst, byte[] data, long len, long maxLength) { + long nativeData = copyToNativeByteArray(data, (int) len); + try { + return nativeFunctions.zlib_decompress(zst, nativeData, len, maxLength); + } finally { + if (nativeData != NativeMemory.NULLPTR) { + NativeMemory.free(nativeData); + } + } } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeCompressionSupport.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeCompressionSupport.java new file mode 100644 index 0000000000..c7fcae6307 --- /dev/null +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeCompressionSupport.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * The Universal Permissive License (UPL), Version 1.0 + * + * Subject to the condition set forth below, permission is hereby granted to any + * person obtaining a copy of this software, associated documentation and/or + * data (collectively the "Software"), free of charge and under any and all + * copyright rights in the Software, and any and all patent rights owned or + * freely licensable by each licensor hereunder covering either (i) the + * unmodified Software as contributed to or provided by such licensor, or (ii) + * the Larger Works (as defined below), to deal in both + * + * (a) the Software, and + * + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if + * one is included with the Software each a "Larger Work" to which the Software + * is contributed by such licensors), + * + * without restriction, including without limitation the rights to copy, create + * derivative works of, display, perform, and distribute the Software and make, + * use, sell, offer for sale, import, export, have made, and have sold the + * Software and the Larger Work(s), and to sublicense the foregoing rights on + * either these or other terms. + * + * This license is subject to the following condition: + * + * The above copyright notice and either this complete permission notice or at a + * minimum a reference to the UPL must be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.oracle.graal.python.runtime; + +import com.oracle.graal.python.runtime.nativeaccess.NativeLibrary; +import com.oracle.graal.python.runtime.nativeaccess.NativeLibraryLoadException; +import com.oracle.graal.python.runtime.nativeaccess.NativeMemory; +import com.oracle.truffle.api.CompilerAsserts; +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; +import com.oracle.truffle.api.TruffleFile; + +abstract class NativeCompressionSupport { + protected final PythonContext pythonContext; + + @CompilationFinal protected boolean available; + + NativeCompressionSupport(PythonContext context) { + if (context.useNativeCompressionModules()) { + this.pythonContext = context; + this.available = true; + } else { + this.pythonContext = null; + this.available = false; + } + } + + public void setNotAvailable() { + CompilerAsserts.neverPartOfCompilation(); + available = false; + } + + public boolean isAvailable() { + assert !available || PythonContext.get(null).isNativeAccessAllowed(); + return available; + } + + protected static long copyToNativeByteArray(byte[] src, int count) { + if (count <= 0) { + return NativeMemory.NULLPTR; + } + return NativeMemory.copyToNativeByteArray(src, 0, count); + } + + protected static long copyToNativeByteArray(byte[] src) { + return copyToNativeByteArray(src, src.length); + } + + protected static long copyToNativeLongArray(long[] src) { + return src.length == 0 ? NativeMemory.NULLPTR : NativeMemory.copyToNativeLongArray(src); + } + + protected static long copyToNativeIntArray(int[] src) { + return src.length == 0 ? NativeMemory.NULLPTR : NativeMemory.copyToNativeIntArray(src); + } + + @TruffleBoundary + static NativeLibrary loadNativeLibrary(PythonContext context, String name) { + String libName = PythonContext.getSupportLibName(name); + TruffleFile homePath = context.getEnv().getInternalTruffleFile(context.getCAPIHome().toJavaStringUncached()); + TruffleFile file = homePath.resolve(libName); + String path = file.getPath(); + try { + return context.ensureNativeContext().loadLibrary(path, PosixConstants.RTLD_LOCAL.value); + } catch (NativeLibraryLoadException e) { + throw new UnsupportedOperationException(String.format("Could not load compression support library from path '%s'.", path), e); + } + } +} diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java index e1eb2448de..0465731868 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java @@ -1194,15 +1194,15 @@ public static JavaDecompress createJavaZLibCompObjectDecompress(PythonLanguage l return new JavaDecompress(PythonBuiltinClassType.ZlibDecompress, PythonBuiltinClassType.ZlibDecompress.getInstanceShape(language), wbits, zdict); } - public static ZLibCompObject createNativeZLibCompObjectCompress(PythonLanguage language, Object zst, NFIZlibSupport zlibSupport) { + public static ZLibCompObject createNativeZLibCompObjectCompress(PythonLanguage language, long zst, NFIZlibSupport zlibSupport) { return createNativeZLibCompObject(PythonBuiltinClassType.ZlibCompress, PythonBuiltinClassType.ZlibCompress.getInstanceShape(language), zst, zlibSupport); } - public static ZLibCompObject createNativeZLibCompObjectDecompress(PythonLanguage language, Object zst, NFIZlibSupport zlibSupport) { + public static ZLibCompObject createNativeZLibCompObjectDecompress(PythonLanguage language, long zst, NFIZlibSupport zlibSupport) { return createNativeZLibCompObject(PythonBuiltinClassType.ZlibDecompress, PythonBuiltinClassType.ZlibDecompress.getInstanceShape(language), zst, zlibSupport); } - public static ZLibCompObject createNativeZLibCompObject(Object cls, Shape shape, Object zst, NFIZlibSupport zlibSupport) { + public static ZLibCompObject createNativeZLibCompObject(Object cls, Shape shape, long zst, NFIZlibSupport zlibSupport) { return new NativeZlibCompObject(cls, shape, zst, zlibSupport); } @@ -1210,7 +1210,7 @@ public static ZlibDecompressorObject createJavaZlibDecompressorObject(PythonLang return ZlibDecompressorObject.createJava(PythonBuiltinClassType.ZlibDecompressor, PythonBuiltinClassType.ZlibDecompressor.getInstanceShape(language), wbits, zdict); } - public static ZlibDecompressorObject createNativeZlibDecompressorObject(PythonLanguage language, Object zst, NFIZlibSupport zlibSupport) { + public static ZlibDecompressorObject createNativeZlibDecompressorObject(PythonLanguage language, long zst, NFIZlibSupport zlibSupport) { return ZlibDecompressorObject.createNative(PythonBuiltinClassType.ZlibDecompressor, PythonBuiltinClassType.ZlibDecompressor.getInstanceShape(language), zst, zlibSupport); } diff --git a/graalpython/python-libzsupport/src/zlib.c b/graalpython/python-libzsupport/src/zlib.c index d6e0b520e1..26d3338d72 100644 --- a/graalpython/python-libzsupport/src/zlib.c +++ b/graalpython/python-libzsupport/src/zlib.c @@ -180,50 +180,42 @@ typedef struct #endif } zlib_stream; -#define NO_ERROR 0 // nfi_var - -#define DEFLATE_INIT_ERROR 101 // nfi_var -#define DEFLATE_END_ERROR 102 // nfi_var -#define DEFLATE_DICT_ERROR 103 // nfi_var -#define DEFLATE_OBJ_ERROR 104 // nfi_var -#define DEFLATE_FLUSH_ERROR 105 // nfi_var -#define DEFLATE_COPY_ERROR 106 // nfi_var -#define DEFLATE_ERROR 107 // nfi_var - -#define INFLATE_INIT_ERROR 201 // nfi_var -#define INFLATE_END_ERROR 202 // nfi_var -#define INFLATE_DICT_ERROR 203 // nfi_var -#define INFLATE_OBJ_ERROR 204 // nfi_var -#define INFLATE_FLUSH_ERROR 205 // nfi_var -#define INFLATE_COPY_ERROR 206 // nfi_var -#define INFLATE_ERROR 207 // nfi_var - -#define INCOMPLETE_ERROR 99 // nfi_var -#define MEMORY_ERROR 999 // nfi_var +#define NO_ERROR 0 + +#define DEFLATE_INIT_ERROR 101 +#define DEFLATE_END_ERROR 102 +#define DEFLATE_DICT_ERROR 103 +#define DEFLATE_OBJ_ERROR 104 +#define DEFLATE_FLUSH_ERROR 105 +#define DEFLATE_COPY_ERROR 106 +#define DEFLATE_ERROR 107 + +#define INFLATE_INIT_ERROR 201 +#define INFLATE_END_ERROR 202 +#define INFLATE_DICT_ERROR 203 +#define INFLATE_OBJ_ERROR 204 +#define INFLATE_FLUSH_ERROR 205 +#define INFLATE_COPY_ERROR 206 +#define INFLATE_ERROR 207 + +#define INCOMPLETE_ERROR 99 +#define MEMORY_ERROR 999 // options to get buffer from the native side -#define OUTPUT_OPTION 0 // nfi_var -#define UNUSED_DATA_OPTION 1 // nfi_var -#define UNCONSUMED_TAIL_OPTION 2 // nfi_var -#define ZDICT_OPTION 3 // nfi_var - -// nfi_function: name('zlibVersion') static(true) +#define OUTPUT_OPTION 0 +#define UNUSED_DATA_OPTION 1 +#define UNCONSUMED_TAIL_OPTION 2 +#define ZDICT_OPTION 3 const char *zlib_get_version() { return ZLIB_VERSION; } - -// nfi_function: name('zlibRuntimeVersion') static(true) const char *zlib_get_runtime_version() { return zlibVersion(); } - -// nfi_function: name('crc32') uLong zlib_crc32(uLong crc, const Byte *buf, uInt len) { return crc32(crc, buf, len); } - -// nfi_function: name('adler32') uLong zlib_adler32(uLong crc, const Byte *buf, uInt len) { return adler32(crc, buf, len); @@ -302,8 +294,6 @@ static off_heap_buffer* zlib_get_ref(off_heap_buffer* o) { } return o; } - -// nfi_function: name('createStream') map('zlib_stream*', 'POINTER') zlib_stream *zlib_create_zlib_stream() { zlib_stream *zst = (zlib_stream *) malloc(sizeof(zlib_stream)); zst->zst_type = NOT_INITIALIZED; @@ -344,8 +334,6 @@ void zlib_release_compobject(compobject *comp) { free(comp); } - -// nfi_function: name('getTimeElapsed') map('zlib_stream*', 'POINTER') static(true) double zlib_get_timeElapsed(zlib_stream* zst) { #ifdef BENCHMARK double t = zst->timeElapsed; @@ -356,8 +344,6 @@ double zlib_get_timeElapsed(zlib_stream* zst) { return -1.0; #endif } - -// nfi_function: name('deallocateStream') map('zlib_stream*', 'POINTER') void zlib_free_stream(zlib_stream* zst) { if (!zst) { return; @@ -377,41 +363,27 @@ void zlib_free_stream(zlib_stream* zst) { LOG_INFO("free zlib_stream(%p)\n", zst); free(zst); } - -// nfi_function: name('gcReleaseHelper') map('zlib_stream*', 'POINTER') release(true) void zlib_gc_helper(zlib_stream* zst) { zlib_free_stream(zst); } - -// nfi_function: name('getErrorFunction') map('zlib_stream*', 'POINTER') int zlib_get_error_type(zlib_stream *zst) { return zst->error_function; } - -// nfi_function: name('getStreamErrorMsg') map('zlib_stream*', 'POINTER') const char *zlib_get_stream_msg(zlib_stream *zst) { return zst->zst.msg; } - -// nfi_function: name('hasStreamErrorMsg') map('zlib_stream*', 'POINTER') int zlib_has_stream_msg(zlib_stream *zst) { if (zst->zst.msg == Z_NULL) { return 0; } return 1; } - -// nfi_function: name('getEOF') map('zlib_stream*', 'POINTER') int zlib_get_eof(zlib_stream *zst) { return zst->comp->eof; } - -// nfi_function: name('getIsInitialised') map('zlib_stream*', 'POINTER') int zlib_get_is_initialised(zlib_stream *zst) { return zst->comp->is_initialised; } - -// nfi_function: name('getBufferSize') map('zlib_stream*', 'POINTER') uInt zlib_get_buffer_size(zlib_stream *zst, int option) { LOG_INFO("zlib_get_buffer_size(%p)\n", zst); size_t size = 0; @@ -438,9 +410,6 @@ uInt zlib_get_buffer_size(zlib_stream *zst, int option) { } return size; } - - -// nfi_function: name('getBuffer') map('zlib_stream*', 'POINTER') void zlib_get_off_heap_buffer(zlib_stream *zst, int option, Byte *dest) { LOG_INFO("zlib_get_off_heap_buffer(%p)\n", zst); off_heap_buffer *buffer = NULL; @@ -474,8 +443,6 @@ void zlib_get_off_heap_buffer(zlib_stream *zst, int option, Byte *dest) { assert(buffer->buf != NULL); memcpy(dest, buffer->buf, size); } - -// nfi_function: name('createCompObject') map('zlib_stream*', 'POINTER') zlib_stream *zlib_create_compobject() { zlib_stream *zst = zlib_create_zlib_stream(); compobject *comp = (compobject *) malloc(sizeof(compobject)); @@ -590,8 +557,6 @@ static ssize_t arrange_output_buffer(zlib_stream *zst, ssize_t length) { return arrange_output_buffer_with_maximum(zst, length, GRAALPYTHON_MAX_SIZE); } - -// nfi_function: name('deflateOffHeap') map('zlib_stream*', 'POINTER') int zlib_deflate_off_heap(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size, int level, int wbits) { LOG_INFO("zlib_deflate_off_heap(%p)\n", zst); int err, flush; @@ -655,8 +620,6 @@ int zlib_deflate_off_heap(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t bu /************************************************ * Decompress * ************************************************/ - -// nfi_function: name('inflateOffHeap') map('zlib_stream*', 'POINTER') int zlib_inflate_off_heap(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size, int wbits) { LOG_INFO("zlib_inflate_off_heap(%p)\n", zst); int err, flush; @@ -731,8 +694,6 @@ int zlib_inflate_off_heap(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t bu /************************************************ * Compress Object * ************************************************/ - -// nfi_function: name('compressObjInitWithDict') map('zlib_stream*', 'POINTER') int zlib_Compress_init(zlib_stream *zst, int level, int method, int wbits, int memLevel, @@ -756,16 +717,12 @@ int zlib_Compress_init(zlib_stream *zst, } return Z_OK; } - -// nfi_function: name('compressObjInit') map('zlib_stream*', 'POINTER') int zlib_Compress_init_no_dict(zlib_stream *zst, int level, int method, int wbits, int memLevel, int strategy) { return zlib_Compress_init(zst, level, method, wbits, memLevel, strategy, NULL, 0); } - -// nfi_function: name('compressObj') map('zlib_stream*', 'POINTER') int zlib_Compress_obj(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size) { LOG_INFO("zlib_Compress_obj(%p, in_len: %zd, buf_size: %zd)\n", zst, in_len, buf_size); clear_output(zst); @@ -807,8 +764,6 @@ int zlib_Compress_obj(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_si } return Z_OK; } - -// nfi_function: name('compressObjFlush') map('zlib_stream*', 'POINTER') int zlib_Compress_flush(zlib_stream *zst, Byte *in, ssize_t buf_size, int mode) { LOG_INFO("zlib_Compress_flush(%p, buf_size: %zd)\n", zst, buf_size); clear_output(zst); @@ -868,9 +823,6 @@ int zlib_Compress_flush(zlib_stream *zst, Byte *in, ssize_t buf_size, int mode) } return Z_OK; } - - -// nfi_function: name('compressObjCopy') map('zlib_stream*', 'POINTER') int zlib_Compress_copy(zlib_stream *zst, zlib_stream *new_copy) { LOG_INFO("zlib_Compress_copy(%p)\n", zst); int err = deflateCopy(&new_copy->zst, &zst->zst); @@ -893,8 +845,6 @@ int zlib_Compress_copy(zlib_stream *zst, zlib_stream *new_copy) { /************************************************ * Decompress Object * ************************************************/ - -// nfi_function: name('decompressObjInitWithDict') map('zlib_stream*', 'POINTER') int zlib_Decompress_init(zlib_stream *zst, int wbits, Byte *dict, size_t dict_len) { LOG_INFO("zlib_Decompress_init(%p)\n", zst); @@ -918,8 +868,6 @@ int zlib_Decompress_init(zlib_stream *zst, int wbits, Byte *dict, size_t dict_le } return Z_OK; } - -// nfi_function: name('decompressObjInit') map('zlib_stream*', 'POINTER') int zlib_Decompress_init_no_dict(zlib_stream *zst, int wbits) { return zlib_Decompress_init(zst, wbits, NULL, 0); } @@ -969,8 +917,6 @@ static int save_unconsumed_input(zlib_stream *zst, Byte *in, size_t in_len, int return Z_OK; } - -// nfi_function: name('decompressObj') map('zlib_stream*', 'POINTER') int zlib_Decompress_obj(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_size, ssize_t max_length) { LOG_INFO("zlib_Decompress_obj(%p, in_len: %zd, buf_size: %zd, max_length: %zd)\n", zst, in_len, buf_size, max_length); clear_output(zst); @@ -1067,8 +1013,6 @@ int zlib_Decompress_obj(zlib_stream *zst, Byte *in, ssize_t in_len, ssize_t buf_ } return Z_OK; } - -// nfi_function: name('decompressObjFlush') map('zlib_stream*', 'POINTER') int zlib_Decompress_flush(zlib_stream *zst, ssize_t length) { LOG_INFO("zlib_Decompress_flush(%p, length: %zd)\n", zst, length); clear_output(zst); @@ -1148,8 +1092,6 @@ int zlib_Decompress_flush(zlib_stream *zst, ssize_t length) { } return Z_OK; } - -// nfi_function: name('decompressObjCopy') map('zlib_stream*', 'POINTER') int zlib_Decompress_copy(zlib_stream *zst, zlib_stream *new_copy) { LOG_INFO("zlib_Decompress_copy(%p)\n", zst); int err = inflateCopy(&new_copy->zst, &zst->zst); @@ -1273,9 +1215,6 @@ decompress_buf(zlib_stream *zst, ssize_t max_length) return Z_OK; } - - -// nfi_function: name('decompressor') map('zlib_stream*', 'POINTER') int zlib_decompress(zlib_stream *zst, Byte *data, size_t len, ssize_t max_length) { From bc62aa2f0ff612e29b93f60df1ca3f222f4bd553 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Fri, 29 May 2026 14:53:05 +0200 Subject: [PATCH 3/8] Migrate bz2 native support to NativeAccess --- .../src/tests/test_bz2.py | 59 +++ .../src/tests/util.py | 4 + .../graal/python/builtins/Python3Core.java | 2 +- .../modules/bz2/BZ2CompressorBuiltins.java | 11 +- .../modules/bz2/BZ2DecompressorBuiltins.java | 11 +- .../builtins/modules/bz2/BZ2Object.java | 43 +- .../python/builtins/modules/bz2/Bz2Nodes.java | 23 +- .../graal/python/runtime/NFIBz2Support.java | 384 ++++++------------ .../graal/python/runtime/object/PFactory.java | 6 +- graalpython/python-libbz2/src/bz2.c | 26 -- 10 files changed, 229 insertions(+), 340 deletions(-) create mode 100644 graalpython/com.oracle.graal.python.test/src/tests/test_bz2.py diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_bz2.py b/graalpython/com.oracle.graal.python.test/src/tests/test_bz2.py new file mode 100644 index 0000000000..8ba9a30631 --- /dev/null +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_bz2.py @@ -0,0 +1,59 @@ +# Copyright (c) 2026, 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +import bz2 +import unittest + +from tests.util import is_native_compression_backend + +DATA = (b"native bz2 support migration test data\n" * 128) + bytes(range(256)) + + +@unittest.skipUnless(is_native_compression_backend(), "requires native compression backend") +def test_bz2_stream_roundtrip_and_unused_data(): + compressor = bz2.BZ2Compressor(5) + compressed = compressor.compress(DATA[:97]) + compressed += compressor.compress(DATA[97:]) + compressed += compressor.flush() + + assert bz2.decompress(compressed) == DATA + + decompressor = bz2.BZ2Decompressor() + assert decompressor.decompress(compressed + b"tail") == DATA + assert decompressor.eof + assert decompressor.unused_data == b"tail" diff --git a/graalpython/com.oracle.graal.python.test/src/tests/util.py b/graalpython/com.oracle.graal.python.test/src/tests/util.py index 2a3824fb21..13997bef03 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/util.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/util.py @@ -59,6 +59,10 @@ def wrapper(test): return wrapper +def is_native_compression_backend(): + return sys.implementation.name != 'graalpy' or __graalpython__.zlib_module_backend() == 'native' + + def _jdk_major_version(): version = __graalpython__.get_jdk_version() return int(version.split(".", 1)[0].split("-", 1)[0]) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java index 1993cfd065..4c2c2b9d57 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java @@ -1068,7 +1068,7 @@ public final void postInitialize(Env env) { /* * Special case for _bz2: If native access is not allowed, we cannot use the built-in - * implementation that would call libbz2 via NFI. Therefore, we remove it from the + * implementation that would call libbz2 via NativeAccess. Therefore, we remove it from the * built-in modules map (and also from sys.modules if already loaded). This will cause a * fallback to another _bz2 implementation (e.g. LLVM or maybe some Java lib). This * needs to be done here and cannot be done in 'initializeBuiltins' because then we diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java index e1963476f4..ffba3fdbc5 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -74,7 +74,6 @@ import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.GilNode; import com.oracle.graal.python.runtime.NFIBz2Support; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.truffle.api.dsl.Bind; @@ -123,15 +122,13 @@ protected ArgumentClinicProvider getArgumentClinic() { @Specialization(guards = {"compresslevel >= 1", "compresslevel <= 9"}) PNone init(BZ2Object.BZ2Compressor self, int compresslevel, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction createStream, - @Cached NativeLibrary.InvokeNativeFunction compressInit, @Cached GilNode gil, @Cached PRaiseNode raiseNode) { gil.release(true); try { - NFIBz2Support bz2Support = PythonContext.get(this).getNFIBz2Support(); - Object bzst = bz2Support.createStream(createStream); - int err = bz2Support.compressInit(bzst, compresslevel, compressInit); + NFIBz2Support bz2Support = PythonContext.get(this).getNativeBz2Support(); + long bzst = bz2Support.createStream(); + int err = bz2Support.compressInit(bzst, compresslevel); if (err != BZ_OK) { errorHandling(inliningTarget, err, raiseNode); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java index 56a7b84b9c..3f7025e0e4 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -69,7 +69,6 @@ import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.NFIBz2Support; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.truffle.api.dsl.Bind; @@ -111,12 +110,10 @@ public abstract static class InitNode extends PythonUnaryBuiltinNode { @Specialization static PNone init(BZ2Object.BZ2Decompressor self, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction createStream, - @Cached NativeLibrary.InvokeNativeFunction compressInit, @Cached PRaiseNode raiseNode) { - NFIBz2Support bz2Support = PythonContext.get(inliningTarget).getNFIBz2Support(); - Object bzst = bz2Support.createStream(createStream); - int err = bz2Support.decompressInit(bzst, compressInit); + NFIBz2Support bz2Support = PythonContext.get(inliningTarget).getNativeBz2Support(); + long bzst = bz2Support.createStream(); + int err = bz2Support.decompressInit(bzst); if (err != BZ_OK) { errorHandling(inliningTarget, err, raiseNode); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java index 72e88d8640..f849870276 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -47,37 +47,42 @@ import com.oracle.graal.python.runtime.NFIBz2Support; import com.oracle.graal.python.util.OverflowException; import com.oracle.graal.python.util.PythonUtils; -import com.oracle.truffle.api.CompilerDirectives; +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.object.Shape; public abstract class BZ2Object extends PythonBuiltinObject { private NFIBz2Support.Pointer pointer; - public BZ2Object(Object cls, Shape instanceShape) { + BZ2Object(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public final void init(Object bzst, NFIBz2Support lib) { + public final void init(long bzst, NFIBz2Support lib) { this.pointer = new NFIBz2Support.Pointer(this, bzst, lib); + assert !pointer.isReleased(); } - public final Object getBzs() { - assert pointer != null; - return pointer.getReference(); + public final long getBzs() { + NFIBz2Support.Pointer p = pointer; + assert p != null && !p.isReleased(); + return p.getPointer(); } - @CompilerDirectives.TruffleBoundary + @TruffleBoundary public final void markReleased() { - if (pointer != null) { - synchronized (this) { - pointer.markReleased(); - pointer = null; - } + NFIBz2Support.Pointer p; + synchronized (this) { + p = pointer; + pointer = null; + } + if (p != null) { + boolean markedReleased = p.markReleased(); + assert markedReleased || p.isReleased(); } } - public static class BZ2Compressor extends BZ2Object { + public static final class BZ2Compressor extends BZ2Object { private boolean flushed; @@ -95,7 +100,7 @@ public void setFlushed() { } } - public static class BZ2Decompressor extends BZ2Object { + public static final class BZ2Decompressor extends BZ2Object { private boolean eof; private byte[] unusedData; @@ -221,12 +226,4 @@ public void setBzsAvailInReal(long bzsAvailInReal) throws OverflowException { this.bzsAvailInReal = PInt.intValueExact(bzsAvailInReal); } } - - public static BZ2Compressor createCompressor(Object cls, Shape instanceShape) { - return new BZ2Compressor(cls, instanceShape); - } - - public static BZ2Decompressor createDecompressor(Object cls, Shape instanceShape) { - return new BZ2Decompressor(cls, instanceShape); - } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java index 984ba4be56..16f288a4bd 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java @@ -58,7 +58,6 @@ import com.oracle.graal.python.builtins.objects.ints.PInt; import com.oracle.graal.python.nodes.PRaiseNode; import com.oracle.graal.python.runtime.NFIBz2Support; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.util.OverflowException; import com.oracle.graal.python.util.PythonUtils; @@ -111,11 +110,10 @@ public byte[] flush(BZ2Object.BZ2Compressor self, PythonContext context) { @Specialization static byte[] nativeCompress(BZ2Object.BZ2Compressor self, PythonContext context, byte[] bytes, int len, int action, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction compress, @Cached GetOutputNativeBufferNode getBuffer, @Cached PRaiseNode raiseNode) { NFIBz2Support bz2Support = context.getNFIBz2Support(); - int err = bz2Support.compress(self.getBzs(), bytes, len, action, INITIAL_BUFFER_SIZE, compress); + int err = bz2Support.compress(self.getBzs(), bytes, len, action, INITIAL_BUFFER_SIZE); if (err != BZ_OK) { errorHandling(inliningTarget, err, raiseNode); } @@ -230,9 +228,6 @@ public abstract static class Bz2NativeInternalDecompress extends Node { @Specialization static byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, int maxLength, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction decompress, - @Cached NativeLibrary.InvokeNativeFunction getBzsAvailInReal, - @Cached NativeLibrary.InvokeNativeFunction getNextInIndex, @Cached GetOutputNativeBufferNode getBuffer, @Cached InlinedConditionProfile errProfile, @Cached InlinedBranchProfile ofProfile, @@ -241,9 +236,9 @@ static byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, int maxLe NFIBz2Support bz2Support = context.getNFIBz2Support(); byte[] in = self.getNextIn(); int offset = self.getNextInIndex(); - int err = bz2Support.decompress(self.getBzs(), in, offset, maxLength, INITIAL_BUFFER_SIZE, self.getBzsAvailInReal(), decompress); - long nextInIdx = bz2Support.getNextInIndex(self.getBzs(), getNextInIndex); - long bzsAvailInReal = bz2Support.getBzsAvailInReal(self.getBzs(), getBzsAvailInReal); + int err = bz2Support.decompress(self.getBzs(), in, offset, maxLength, INITIAL_BUFFER_SIZE, self.getBzsAvailInReal()); + long nextInIdx = bz2Support.getNextInIndex(self.getBzs()); + long bzsAvailInReal = bz2Support.getBzsAvailInReal(self.getBzs()); try { self.setNextInIndex(nextInIdx); self.setBzsAvailInReal(bzsAvailInReal); @@ -264,18 +259,16 @@ static byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, int maxLe @GenerateCached(false) public abstract static class GetOutputNativeBufferNode extends Node { - public abstract byte[] execute(Node inliningTarget, Object bzst, PythonContext context); + public abstract byte[] execute(Node inliningTarget, long bzst, PythonContext context); @Specialization - static byte[] getBuffer(Node inliningTarget, Object bzst, PythonContext context, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getBufferSize, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getBuffer, + static byte[] getBuffer(Node inliningTarget, long bzst, PythonContext context, @Cached InlinedBranchProfile ofProfile, @Cached PRaiseNode raiseNode) { NFIBz2Support bz2Support = context.getNFIBz2Support(); int size; try { - size = PInt.intValueExact(bz2Support.getOutputBufferSize(bzst, getBufferSize)); + size = PInt.intValueExact(bz2Support.getOutputBufferSize(bzst)); } catch (OverflowException of) { ofProfile.enter(inliningTarget); throw raiseNode.raise(inliningTarget, SystemError, VALUE_TOO_LARGE_TO_FIT_INTO_INDEX); @@ -285,7 +278,7 @@ static byte[] getBuffer(Node inliningTarget, Object bzst, PythonContext context, } byte[] resultArray = new byte[size]; /* this will clear the native output once retrieved */ - bz2Support.getOutputBuffer(bzst, resultArray, getBuffer); + bz2Support.getOutputBuffer(bzst, resultArray); return resultArray; } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java index eda48b05e1..627410b295 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -40,138 +40,78 @@ */ package com.oracle.graal.python.runtime; +import static com.oracle.graal.python.annotations.NativeSimpleType.DOUBLE; +import static com.oracle.graal.python.annotations.NativeSimpleType.POINTER; +import static com.oracle.graal.python.annotations.NativeSimpleType.SINT32; +import static com.oracle.graal.python.annotations.NativeSimpleType.SINT64; +import static com.oracle.graal.python.annotations.NativeSimpleType.VOID; + import com.oracle.graal.python.PythonLanguage; -import com.oracle.truffle.api.CompilerAsserts; -import com.oracle.truffle.api.CompilerDirectives; +import com.oracle.graal.python.annotations.DowncallSignature; +import com.oracle.graal.python.runtime.nativeaccess.NativeLibrary; +import com.oracle.graal.python.runtime.nativeaccess.NativeMemory; import com.oracle.truffle.api.ThreadLocalAction.Access; import com.oracle.truffle.api.TruffleLogger; -/*- - * Generated using: - * scripts/nfi_gen.py -name Bz2 -cpath graalpython/com.oracle.graal.python.cext/bz2/bz2.c -lib libbz2support - */ -public class NFIBz2Support { +public class NFIBz2Support extends NativeCompressionSupport { private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFIBz2Support.class); + private static final String SUPPORTING_NATIVE_LIB_NAME = "bz2support"; - enum Bz2NativeFunctions implements NativeLibrary.NativeFunction { - - /*- - nfi_function: name('createStream') map('bzst_stream*', 'POINTER') - bzst_stream *bz_create_bzst_stream() - */ - bz_create_bzst_stream("(): POINTER"), - - /*- - nfi_function: name('getTimeElapsed') map('bzst_stream*', 'POINTER') static(true) - double bz_get_timeElapsed(bzst_stream* zst) - */ - bz_get_timeElapsed("(POINTER): DOUBLE"), - - /*- - nfi_function: name('deallocateStream') map('bzst_stream*', 'POINTER') - void bz_free_stream(bzst_stream* bzst) - */ - bz_free_stream("(POINTER): VOID"), - - /*- - nfi_function: name('gcReleaseHelper') map('bzst_stream*', 'POINTER') release(true) - void bz_gc_helper(bzst_stream* bzst) - */ - bz_gc_helper("(POINTER): VOID"), - - /*- - nfi_function: name('getNextInIndex') map('bzst_stream*', 'POINTER') - ssize_t bz_get_next_in_index(bzst_stream *bzst) - */ - bz_get_next_in_index("(POINTER): SINT64"), - - /*- - nfi_function: name('getBzsAvailInReal') map('bzst_stream*', 'POINTER') - ssize_t bz_get_bzs_avail_in_real(bzst_stream *bzst) - */ - bz_get_bzs_avail_in_real("(POINTER): SINT64"), - - /*- - nfi_function: name('setBzsAvailInReal') map('bzst_stream*', 'POINTER') - void bz_set_bzs_avail_in_real(bzst_stream *bzst, ssize_t v) - */ - bz_set_bzs_avail_in_real("(POINTER, SINT64): VOID"), - - /*- - nfi_function: name('getOutputBufferSize') map('bzst_stream*', 'POINTER') - size_t bz_get_output_buffer_size(bzst_stream *bzst) - */ - bz_get_output_buffer_size("(POINTER): UINT64"), - - /*- - nfi_function: name('getOutputBuffer') map('bzst_stream*', 'POINTER') - void bz_get_output_buffer(bzst_stream *bzst, Byte *dest) - */ - bz_get_output_buffer("(POINTER, [UINT8]): VOID"), - - /*- - nfi_function: name('compressInit') map('bzst_stream*', 'POINTER') - int bz_compressor_init(bzst_stream *bzst, int compresslevel) - */ - bz_compressor_init("(POINTER, SINT32): SINT32"), - - /*- - nfi_function: name('compress') map('bzst_stream*', 'POINTER') - int bz_compress(bzst_stream *bzst, Byte *data, ssize_t len, int action, ssize_t bufsize) - */ - bz_compress("(POINTER, [UINT8], SINT64, SINT32, SINT64): SINT32"), - - /*- - nfi_function: name('decompressInit') map('bzst_stream*', 'POINTER') - int bz_decompress_init(bzst_stream *bzst) - */ - bz_decompress_init("(POINTER): SINT32"), - - /*- - nfi_function: name('decompress') map('bzst_stream*', 'POINTER') - int bz_decompress(bzst_stream *bzst, Byte *input_buffer, ssize_t offset,ssize_t max_length,ssize_t bufsize, ssize_t bzs_avail_in_real) - */ - bz_decompress("(POINTER, [UINT8], SINT64, SINT64, SINT64, SINT64): SINT32"); - - private final String signature; - - Bz2NativeFunctions(String signature) { - this.signature = signature; - } + abstract static class Bz2NativeFunctions { + @DowncallSignature(returnType = POINTER) + abstract long bz_create_bzst_stream(); - @Override - public String signature() { - return signature; - } - } + @DowncallSignature(returnType = DOUBLE, argumentTypes = {POINTER}) + abstract double bz_get_timeElapsed(long bzst); - private static final String SUPPORTING_NATIVE_LIB_NAME = "bz2support"; + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER}) + abstract void bz_free_stream(long bzst); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER}) + abstract void bz_gc_helper(long bzst); + + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long bz_get_next_in_index(long bzst); - private final PythonContext pythonContext; - private final NativeLibrary.TypedNativeLibrary typedNativeLib; - - @CompilerDirectives.CompilationFinal private boolean available; - - private NFIBz2Support(PythonContext context, NativeLibrary.NFIBackend backend, String noNativeAccessHelp) { - if (context.useNativeCompressionModules()) { - this.pythonContext = context; - this.typedNativeLib = NativeLibrary.create(PythonContext.getSupportLibName(SUPPORTING_NATIVE_LIB_NAME), Bz2NativeFunctions.values(), - backend, noNativeAccessHelp, false); - this.available = true; - } else { - this.pythonContext = null; - this.typedNativeLib = null; - this.available = false; + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long bz_get_bzs_avail_in_real(long bzst); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER, SINT64}) + abstract void bz_set_bzs_avail_in_real(long bzst, long v); + + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long bz_get_output_buffer_size(long bzst); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER, POINTER}) + abstract void bz_get_output_buffer(long bzst, long dest); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32}) + abstract int bz_compressor_init(long bzst, int compresslevel); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT32, SINT64}) + abstract int bz_compress(long bzst, long data, long len, int action, long bufsize); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int bz_decompress_init(long bzst); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64, SINT64, SINT64}) + abstract int bz_decompress(long bzst, long inputBuffer, long offset, long maxLength, long bufsize, long bzsAvailInReal); + + static NativeLibrary loadNativeLibrary(PythonContext context) { + return NativeCompressionSupport.loadNativeLibrary(context, SUPPORTING_NATIVE_LIB_NAME); } } - public static NFIBz2Support createNative(PythonContext context, String noNativeAccessHelp) { - return new NFIBz2Support(context, NativeLibrary.NFIBackend.NATIVE, noNativeAccessHelp); + private final Bz2NativeFunctions nativeFunctions; + + private NFIBz2Support(PythonContext context) { + super(context); + this.nativeFunctions = isAvailable() ? new Bz2NativeFunctionsGen(context) : null; } - public static NFIBz2Support createLLVM(PythonContext context, String noNativeAccessHelp) { - return new NFIBz2Support(context, NativeLibrary.NFIBackend.LLVM, noNativeAccessHelp); + public static NFIBz2Support createNative(PythonContext context, String noNativeAccessHelp) { + return new NFIBz2Support(context); } static class PointerReleaseCallback implements AsyncHandler.AsyncAction { @@ -183,17 +123,15 @@ public PointerReleaseCallback(Pointer pointer) { @Override public void execute(PythonContext context, Access access) { - synchronized (pointer) { - if (pointer.isReleased()) { - return; - } - try { - pointer.doRelease(); - pointer.markReleased(); - LOGGER.finest("NFIBz2Support pointer has been freed"); - } catch (Exception e) { - LOGGER.severe("Error while trying to free NFIBz2Support pointer: " + e.getMessage()); - } + if (!pointer.markReleased()) { + assert pointer.isReleased(); + return; + } + try { + pointer.doRelease(); + LOGGER.finest("NFIBz2Support pointer has been freed"); + } catch (Exception e) { + LOGGER.severe("Error while trying to free NFIBz2Support pointer: " + e.getMessage()); } } } @@ -201,14 +139,20 @@ public void execute(PythonContext context, Access access) { public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference { private final NFIBz2Support lib; + private final long pointer; - public Pointer(Object referent, Object ptr, NFIBz2Support lib) { - super(referent, ptr, lib.pythonContext.getSharedFinalizer()); + public Pointer(Object referent, long pointer, NFIBz2Support lib) { + super(referent, lib.pythonContext.getSharedFinalizer()); this.lib = lib; + this.pointer = pointer; + } + + public long getPointer() { + return pointer; } protected void doRelease() { - lib.gcReleaseHelper(getReference()); + lib.gcReleaseHelper(pointer); } @Override @@ -220,155 +164,79 @@ public AsyncHandler.AsyncAction release() { } } - public void notAvailable() { - if (available) { - CompilerAsserts.neverPartOfCompilation("Checking NFIBz2Support availability should only be done during initialization."); - available = false; - } - } - - public boolean isAvailable() { - return available; + public Object getTimeElapsed(long zst) { + return nativeFunctions.bz_get_timeElapsed(zst); } - /** - * - * @param zst bzst_stream* zst - * @return double - */ - public Object getTimeElapsed(Object zst) { - return typedNativeLib.callUncached(pythonContext, Bz2NativeFunctions.bz_get_timeElapsed, zst); + public Object gcReleaseHelper(long bzst) { + nativeFunctions.bz_gc_helper(bzst); + return null; } - /** - * - * @param bzst bzst_stream* bzst - * - */ - public Object gcReleaseHelper(Object bzst) { - return typedNativeLib.callUncached(pythonContext, Bz2NativeFunctions.bz_gc_helper, bzst); + public long createStream() { + return nativeFunctions.bz_create_bzst_stream(); } - /** - * - * - * @return bzst_stream* - */ - public Object createStream( - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.call(typedNativeLib, Bz2NativeFunctions.bz_create_bzst_stream); + public void deallocateStream(long bzst) { + nativeFunctions.bz_free_stream(bzst); } - /** - * - * @param bzst bzst_stream* bzst - * - */ - public void deallocateStream(Object bzst, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, Bz2NativeFunctions.bz_free_stream, bzst); + public long getNextInIndex(long bzst) { + return nativeFunctions.bz_get_next_in_index(bzst); } - /** - * - * @param bzst bzst_stream *bzst - * @return ssize_t - */ - public long getNextInIndex(Object bzst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, Bz2NativeFunctions.bz_get_next_in_index, bzst); + public long getBzsAvailInReal(long bzst) { + return nativeFunctions.bz_get_bzs_avail_in_real(bzst); } - /** - * - * @param bzst bzst_stream *bzst - * @return ssize_t - */ - public long getBzsAvailInReal(Object bzst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, Bz2NativeFunctions.bz_get_bzs_avail_in_real, bzst); + public void setBzsAvailInReal(long bzst, long v) { + nativeFunctions.bz_set_bzs_avail_in_real(bzst, v); } - /** - * - * @param bzst bzst_stream *bzst - * @param v ssize_t v - * - */ - public void setBzsAvailInReal(Object bzst, long v, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, Bz2NativeFunctions.bz_set_bzs_avail_in_real, bzst, v); + public long getOutputBufferSize(long bzst) { + return nativeFunctions.bz_get_output_buffer_size(bzst); } - /** - * - * @param bzst bzst_stream *bzst - * @return size_t - */ - public long getOutputBufferSize(Object bzst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, Bz2NativeFunctions.bz_get_output_buffer_size, bzst); - } - - /** - * - * @param bzst bzst_stream *bzst - * @param dest Byte *dest - * - */ - public void getOutputBuffer(Object bzst, byte[] dest, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, Bz2NativeFunctions.bz_get_output_buffer, bzst, dest); + public void getOutputBuffer(long bzst, byte[] dest) { + if (dest.length == 0) { + return; + } + long nativeDest = NativeMemory.mallocByteArray(dest.length); + try { + nativeFunctions.bz_get_output_buffer(bzst, nativeDest); + NativeMemory.readByteArrayElements(nativeDest, 0, dest, 0, dest.length); + } finally { + NativeMemory.free(nativeDest); + } } - /** - * - * @param bzst bzst_stream *bzst - * @param compresslevel int compresslevel - * @return int - */ - public int compressInit(Object bzst, int compresslevel, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, Bz2NativeFunctions.bz_compressor_init, bzst, compresslevel); + public int compressInit(long bzst, int compresslevel) { + return nativeFunctions.bz_compressor_init(bzst, compresslevel); } - /** - * - * @param bzst bzst_stream *bzst - * @param data Byte *data - * @param len ssize_t len - * @param action int action - * @param bufsize ssize_t bufsize - * @return int - */ - public int compress(Object bzst, byte[] data, long len, int action, long bufsize, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, Bz2NativeFunctions.bz_compress, bzst, data, len, action, bufsize); + public int compress(long bzst, byte[] data, long len, int action, long bufsize) { + long nativeData = copyToNativeByteArray(data, (int) len); + try { + return nativeFunctions.bz_compress(bzst, nativeData, len, action, bufsize); + } finally { + if (nativeData != NativeMemory.NULLPTR) { + NativeMemory.free(nativeData); + } + } } - /** - * - * @param bzst bzst_stream *bzst - * @return int - */ - public int decompressInit(Object bzst, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, Bz2NativeFunctions.bz_decompress_init, bzst); + public int decompressInit(long bzst) { + return nativeFunctions.bz_decompress_init(bzst); } - /** - * - * @param bzst bzst_stream *bzst - * @param input_buffer Byte *input_buffer - * @param offset ssize_t offset - * @param max_length ssize_t max_length - * @param bufsize ssize_t bufsize - * @param bzs_avail_in_real ssize_t bzs_avail_in_real - * @return int - */ - public int decompress(Object bzst, byte[] input_buffer, long offset, long max_length, long bufsize, long bzs_avail_in_real, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, Bz2NativeFunctions.bz_decompress, bzst, input_buffer, offset, max_length, bufsize, bzs_avail_in_real); + public int decompress(long bzst, byte[] inputBuffer, long offset, long maxLength, long bufsize, long bzsAvailInReal) { + long nativeInputBuffer = copyToNativeByteArray(inputBuffer); + try { + return nativeFunctions.bz_decompress(bzst, nativeInputBuffer, offset, maxLength, bufsize, bzsAvailInReal); + } finally { + if (nativeInputBuffer != NativeMemory.NULLPTR) { + NativeMemory.free(nativeInputBuffer); + } + } } - } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java index 0465731868..3951fa8d2b 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java @@ -25,8 +25,8 @@ */ package com.oracle.graal.python.runtime.object; -import static com.oracle.graal.python.runtime.nativeaccess.NativeMemory.NULLPTR; import static com.oracle.graal.python.nodes.SpecialMethodNames.T___NEW__; +import static com.oracle.graal.python.runtime.nativeaccess.NativeMemory.NULLPTR; import static com.oracle.graal.python.util.PythonUtils.EMPTY_OBJECT_ARRAY; import java.lang.ref.ReferenceQueue; @@ -1175,7 +1175,7 @@ public static BZ2Object.BZ2Compressor createBZ2Compressor(PythonLanguage languag } public static BZ2Object.BZ2Compressor createBZ2Compressor(Object cls, Shape shape) { - return BZ2Object.createCompressor(cls, shape); + return new BZ2Object.BZ2Compressor(cls, shape); } public static BZ2Object.BZ2Decompressor createBZ2Decompressor(PythonLanguage language) { @@ -1183,7 +1183,7 @@ public static BZ2Object.BZ2Decompressor createBZ2Decompressor(PythonLanguage lan } public static BZ2Object.BZ2Decompressor createBZ2Decompressor(Object cls, Shape shape) { - return BZ2Object.createDecompressor(cls, shape); + return new BZ2Object.BZ2Decompressor(cls, shape); } public static JavaCompress createJavaZLibCompObjectCompress(PythonLanguage language, int level, int wbits, int strategy, byte[] zdict) { diff --git a/graalpython/python-libbz2/src/bz2.c b/graalpython/python-libbz2/src/bz2.c index 5b6c116694..f608a6f183 100644 --- a/graalpython/python-libbz2/src/bz2.c +++ b/graalpython/python-libbz2/src/bz2.c @@ -222,8 +222,6 @@ static void bz_release_buffer(off_heap_buffer *o) { free(o->buf); free(o); } - -// nfi_function: name('createStream') map('bzst_stream*', 'POINTER') API_FUNC bzst_stream *bz_create_bzst_stream() { bzst_stream *bzst = (bzst_stream *) malloc(sizeof(bzst_stream)); bzst->bzs.opaque = NULL; @@ -242,8 +240,6 @@ API_FUNC bzst_stream *bz_create_bzst_stream() { LOG_INFO("bzst_stream(%p)\n", bzst); return bzst; } - -// nfi_function: name('getTimeElapsed') map('bzst_stream*', 'POINTER') static(true) API_FUNC double bz_get_timeElapsed(bzst_stream* zst) { #ifdef BENCHMARK double t = bzst->timeElapsed; @@ -254,8 +250,6 @@ API_FUNC double bz_get_timeElapsed(bzst_stream* zst) { return -1.0; #endif } - -// nfi_function: name('deallocateStream') map('bzst_stream*', 'POINTER') API_FUNC void bz_free_stream(bzst_stream* bzst) { if (!bzst) { return; @@ -271,28 +265,18 @@ API_FUNC void bz_free_stream(bzst_stream* bzst) { LOG_INFO("free bzst_stream(%p)\n", bzst); free(bzst); } - -// nfi_function: name('gcReleaseHelper') map('bzst_stream*', 'POINTER') release(true) API_FUNC void bz_gc_helper(bzst_stream* bzst) { bz_free_stream(bzst); } - -// nfi_function: name('getNextInIndex') map('bzst_stream*', 'POINTER') API_FUNC ssize_t bz_get_next_in_index(bzst_stream *bzst) { return bzst->next_in_index; } - -// nfi_function: name('getBzsAvailInReal') map('bzst_stream*', 'POINTER') API_FUNC ssize_t bz_get_bzs_avail_in_real(bzst_stream *bzst) { return bzst->bzs_avail_in_real; } - -// nfi_function: name('setBzsAvailInReal') map('bzst_stream*', 'POINTER') API_FUNC void bz_set_bzs_avail_in_real(bzst_stream *bzst, ssize_t v) { bzst->bzs_avail_in_real = v; } - -// nfi_function: name('getOutputBufferSize') map('bzst_stream*', 'POINTER') API_FUNC size_t bz_get_output_buffer_size(bzst_stream *bzst) { LOG_INFO("bz_get_output_buffer_size(%p)\n", bzst); size_t size = bzst->output_size; @@ -309,8 +293,6 @@ static void clear_output(bzst_stream *bzst) { bzst->output->size = 0; bzst->output_size = 0; } - -// nfi_function: name('getOutputBuffer') map('bzst_stream*', 'POINTER') API_FUNC void bz_get_output_buffer(bzst_stream *bzst, Byte *dest) { LOG_INFO("bz_get_off_heap_buffer(%p)\n", bzst); off_heap_buffer *buffer = bzst->output; @@ -381,9 +363,6 @@ grow_buffer(bzst_stream *bzst, ssize_t max_length) { /************************************************ * Compress Object * ************************************************/ - - -// nfi_function: name('compressInit') map('bzst_stream*', 'POINTER') API_FUNC int bz_compressor_init(bzst_stream *bzst, int compresslevel) { LOG_INFO("bz_compressor_init(%p, %d)\n", bzst, compresslevel); int bzerror = BZ2_bzCompressInit(&bzst->bzs, compresslevel, 0, 0); @@ -393,8 +372,6 @@ API_FUNC int bz_compressor_init(bzst_stream *bzst, int compresslevel) { bzst->bzs_type = COMPRESS_TYPE; return BZ_OK; } - -// nfi_function: name('compress') map('bzst_stream*', 'POINTER') API_FUNC int bz_compress(bzst_stream *bzst, Byte *data, ssize_t len, int action, ssize_t bufsize) { LOG_INFO("bz_compress(%p, %zd, %d, %zd)\n", bzst, len, action, bufsize); size_t data_size = 0; @@ -468,8 +445,6 @@ API_FUNC int bz_compress(bzst_stream *bzst, Byte *data, ssize_t len, int action, /************************************************ * Decompress Object * ************************************************/ - -// nfi_function: name('decompressInit') map('bzst_stream*', 'POINTER') API_FUNC int bz_decompress_init(bzst_stream *bzst) { LOG_INFO("bz_decompress_init(%p)\n", bzst); int bzerror = BZ2_bzDecompressInit(&bzst->bzs, 0, 0); @@ -486,7 +461,6 @@ API_FUNC int bz_decompress_init(bzst_stream *bzst) { buffer is allocated dynamically and returned. At most max_length bytes are returned, so some of the input may not be consumed. d->bzs.next_in and d->bzs_avail_in_real are updated to reflect the consumed input. */ -// nfi_function: name('decompress') map('bzst_stream*', 'POINTER') API_FUNC int bz_decompress(bzst_stream *bzst, Byte *input_buffer, ssize_t offset, ssize_t max_length, From ea4811d358eaa40d7a05fccee96ef9b2f33f79b8 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Fri, 29 May 2026 14:53:23 +0200 Subject: [PATCH 4/8] Migrate lzma native support to NativeAccess --- .../src/tests/test_lzma.py | 71 ++ .../modules/lzma/LZMACompressorBuiltins.java | 4 +- .../lzma/LZMADecompressorBuiltins.java | 4 +- .../modules/lzma/LZMAModuleBuiltins.java | 5 +- .../builtins/modules/lzma/LZMANodes.java | 160 ++-- .../builtins/modules/lzma/LZMAObject.java | 126 +-- .../graal/python/runtime/NFILZMASupport.java | 866 +++++++----------- graalpython/python-liblzma/src/lzma.c | 168 ++-- 8 files changed, 571 insertions(+), 833 deletions(-) create mode 100644 graalpython/com.oracle.graal.python.test/src/tests/test_lzma.py diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_lzma.py b/graalpython/com.oracle.graal.python.test/src/tests/test_lzma.py new file mode 100644 index 0000000000..50a156f490 --- /dev/null +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_lzma.py @@ -0,0 +1,71 @@ +# Copyright (c) 2026, 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +import lzma +import unittest + +from tests.util import is_native_compression_backend + +DATA = (b"native lzma support migration test data\n" * 128) + bytes(range(256)) + + +@unittest.skipUnless(is_native_compression_backend(), "requires native compression backend") +def test_lzma_stream_roundtrip_and_unused_data(): + compressor = lzma.LZMACompressor(format=lzma.FORMAT_XZ, check=lzma.CHECK_CRC64) + compressed = compressor.compress(DATA[:113]) + compressed += compressor.compress(DATA[113:]) + compressed += compressor.flush() + + assert lzma.decompress(compressed) == DATA + + decompressor = lzma.LZMADecompressor() + assert decompressor.decompress(compressed + b"tail") == DATA + assert decompressor.eof + assert decompressor.unused_data == b"tail" + + +@unittest.skipUnless(is_native_compression_backend(), "requires native compression backend") +def test_lzma_raw_filter_roundtrip(): + filters = [{"id": lzma.FILTER_LZMA2, "preset": 1}] + compressor = lzma.LZMACompressor(format=lzma.FORMAT_RAW, filters=filters) + compressed = compressor.compress(DATA) + compressed += compressor.flush() + + decompressor = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters) + assert decompressor.decompress(compressed) == DATA + assert decompressor.eof diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java index 9e2ab7f7be..47147fc2ef 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -57,11 +57,11 @@ import com.oracle.graal.python.PythonLanguage; import com.oracle.graal.python.annotations.ArgumentClinic; import com.oracle.graal.python.annotations.ArgumentClinic.ClinicConversion; +import com.oracle.graal.python.annotations.Builtin; import com.oracle.graal.python.annotations.ClinicConverterFactory; import com.oracle.graal.python.annotations.Slot; import com.oracle.graal.python.annotations.Slot.SlotKind; import com.oracle.graal.python.annotations.Slot.SlotSignature; -import com.oracle.graal.python.annotations.Builtin; import com.oracle.graal.python.builtins.CoreFunctions; import com.oracle.graal.python.builtins.PythonBuiltinClassType; import com.oracle.graal.python.builtins.PythonBuiltins; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java index 9c63aaf441..482a3f49f8 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -57,10 +57,10 @@ import com.oracle.graal.python.PythonLanguage; import com.oracle.graal.python.annotations.ArgumentClinic; +import com.oracle.graal.python.annotations.Builtin; import com.oracle.graal.python.annotations.Slot; import com.oracle.graal.python.annotations.Slot.SlotKind; import com.oracle.graal.python.annotations.Slot.SlotSignature; -import com.oracle.graal.python.annotations.Builtin; import com.oracle.graal.python.builtins.CoreFunctions; import com.oracle.graal.python.builtins.PythonBuiltinClassType; import com.oracle.graal.python.builtins.PythonBuiltins; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java index a7a53a5abb..cd0f0a8278 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java @@ -93,7 +93,6 @@ import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode; import com.oracle.graal.python.runtime.NFILZMASupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; import com.oracle.truffle.api.dsl.Bind; @@ -213,8 +212,8 @@ public void postInitialize(Python3Core c) { MODE_NORMAL = modes[MODE_NORMAL_INDEX]; PRESET_DEFAULT = preset[PRESET_DEFAULT_INDEX]; PRESET_EXTREME = preset[PRESET_EXTREME_INDEX]; - } catch (NativeLibrary.NativeLibraryCannotBeLoaded e) { - lzmaSupport.notAvailable(); + } catch (UnsupportedOperationException e) { + lzmaSupport.setNotAvailable(); // ignore and proceed without native lzma support and use the java port. } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java index f95782af19..39d69cda40 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -118,7 +118,6 @@ import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode; import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode; import com.oracle.graal.python.runtime.NFILZMASupport; -import com.oracle.graal.python.runtime.NativeLibrary; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.exception.PythonErrorType; import com.oracle.graal.python.util.OverflowException; @@ -489,35 +488,29 @@ long[][] parseFilter(VirtualFrame frame, Object filterSpecs, @GenerateInline(false) // footprint reduction 40 -> 21 protected abstract static class NativeFilterChain extends Node { - public abstract void execute(VirtualFrame frame, Object lzmast, PythonContext context, Object filterSpecs); + public abstract void execute(VirtualFrame frame, long lzmast, PythonContext context, Object filterSpecs); @Specialization - static void parseFilterChainSpec(VirtualFrame frame, Object lzmast, PythonContext context, Object filterSpecs, + static void parseFilterChainSpec(VirtualFrame frame, long lzmast, PythonContext context, Object filterSpecs, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction setFilterSpecLZMA, - @Cached NativeLibrary.InvokeNativeFunction setFilterSpecDelta, - @Cached NativeLibrary.InvokeNativeFunction setFilterSpecBCJ, @Cached LZMAParseFilterChain parseFilterChain, @Cached PRaiseNode raiseNode) { long[][] filters = parseFilterChain.execute(frame, filterSpecs); for (int i = 0; i < filters.length; i++) { - setFilterOptions(inliningTarget, lzmast, filters[i], i, context, setFilterSpecLZMA, setFilterSpecDelta, setFilterSpecBCJ, raiseNode); + setFilterOptions(inliningTarget, lzmast, filters[i], i, context, raiseNode); } } - private static void setFilterOptions(Node inliningTarget, Object lzmast, long[] filter, int fidx, PythonContext context, - NativeLibrary.InvokeNativeFunction setFilterSpecLZMA, - NativeLibrary.InvokeNativeFunction setFilterSpecDelta, - NativeLibrary.InvokeNativeFunction setFilterSpecBCJ, + private static void setFilterOptions(Node inliningTarget, long lzmast, long[] filter, int fidx, PythonContext context, PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = context.getNFILZMASupport(); + NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); int err; long id = filter[0]; switch (LZMAFilter.from(id)) { case LZMA_FILTER_LZMA1: case LZMA_FILTER_LZMA2: assert filter.length == 10; - err = lzmaSupport.setFilterSpecLZMA(lzmast, fidx, filter, setFilterSpecLZMA); + err = lzmaSupport.setFilterSpecLZMA(lzmast, fidx, filter); if (err != LZMA_OK) { if (err == LZMA_PRESET_ERROR) { throw raiseNode.raise(inliningTarget, LZMAError, INVALID_COMPRESSION_PRESET, filter[LZMAOption.preset.ordinal()]); @@ -527,7 +520,7 @@ private static void setFilterOptions(Node inliningTarget, Object lzmast, long[] return; case LZMA_FILTER_DELTA: assert filter.length == 2; - err = lzmaSupport.setFilterSpecDelta(lzmast, fidx, filter, setFilterSpecDelta); + err = lzmaSupport.setFilterSpecDelta(lzmast, fidx, filter); if (err != LZMA_OK) { errorHandling(inliningTarget, err, raiseNode); } @@ -539,7 +532,7 @@ private static void setFilterOptions(Node inliningTarget, Object lzmast, long[] case LZMA_FILTER_ARMTHUMB: case LZMA_FILTER_SPARC: assert filter.length == 2; - err = lzmaSupport.setFilterSpecBCJ(lzmast, fidx, filter, setFilterSpecBCJ); + err = lzmaSupport.setFilterSpecBCJ(lzmast, fidx, filter); if (err != LZMA_OK) { errorHandling(inliningTarget, err, raiseNode); } @@ -688,14 +681,12 @@ public abstract static class LZMACompressInit extends Node { @Specialization(guards = "format == FORMAT_XZ") static void xz(LZMACompressor.Native self, @SuppressWarnings("unused") int format, long preset, @SuppressWarnings("unused") PNone filters, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaEasyEncoder, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); - int lzret = lzmaSupport.lzmaEasyEncoder(lzmast, preset, self.getCheck(), lzmaEasyEncoder); + int lzret = lzmaSupport.lzmaEasyEncoder(lzmast, preset, self.getCheck()); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -704,17 +695,15 @@ static void xz(LZMACompressor.Native self, @SuppressWarnings("unused") int forma @Specialization(guards = {"format == FORMAT_XZ", "!isPNone(filters)"}) static void xz(VirtualFrame frame, LZMACompressor.Native self, @SuppressWarnings("unused") int format, @SuppressWarnings("unused") long preset, Object filters, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaStreamEncoder, @Exclusive @Cached NativeFilterChain filterChain, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, ctxt, filters); - int lzret = lzmaSupport.lzmaStreamEncoder(lzmast, self.getCheck(), lzmaStreamEncoder); + int lzret = lzmaSupport.lzmaStreamEncoder(lzmast, self.getCheck()); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -724,14 +713,12 @@ static void xz(VirtualFrame frame, LZMACompressor.Native self, @SuppressWarnings @Specialization(guards = "format == FORMAT_ALONE") static void alone(LZMACompressor.Native self, int format, long preset, PNone filters, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaAloneEncoderPreset, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); - int lzret = lzmaSupport.lzmaAloneEncoderPreset(lzmast, preset, lzmaAloneEncoderPreset); + int lzret = lzmaSupport.lzmaAloneEncoderPreset(lzmast, preset); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -741,17 +728,15 @@ static void alone(LZMACompressor.Native self, int format, long preset, PNone fil @Specialization(guards = {"format == FORMAT_ALONE", "!isPNone(filters)"}) static void alone(VirtualFrame frame, LZMACompressor.Native self, int format, long preset, Object filters, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaAloneEncoder, @Exclusive @Cached NativeFilterChain filterChain, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, ctxt, filters); - int lzret = lzmaSupport.lzmaAloneEncoder(lzmast, lzmaAloneEncoder); + int lzret = lzmaSupport.lzmaAloneEncoder(lzmast); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -761,17 +746,15 @@ static void alone(VirtualFrame frame, LZMACompressor.Native self, int format, lo @Specialization(guards = "format == FORMAT_RAW") static void raw(VirtualFrame frame, LZMACompressor.Native self, int format, long preset, Object filters, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaRawEncoder, @Exclusive @Cached NativeFilterChain filterChain, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, ctxt, filters); - int lzret = lzmaSupport.lzmaRawEncoder(lzmast, lzmaRawEncoder); + int lzret = lzmaSupport.lzmaRawEncoder(lzmast); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -871,12 +854,11 @@ public byte[] flush(Node inliningTarget, LZMACompressor self, PythonContext cont @Specialization static byte[] nativeCompress(Node inliningTarget, LZMACompressor.Native self, PythonContext context, byte[] bytes, int len, int action, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction compress, @Cached GetOutputNativeBufferNode getBuffer, @Cached InlinedConditionProfile errProfile, @Exclusive @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = context.getNFILZMASupport(); - int err = lzmaSupport.compress(self.getLzs(), bytes, len, action, INITIAL_BUFFER_SIZE, compress); + NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + int err = lzmaSupport.compress(self.getLzs(), bytes, len, action, INITIAL_BUFFER_SIZE); if (errProfile.profile(inliningTarget, err != LZMA_OK)) { errorHandling(inliningTarget, err, raiseNode); } @@ -924,15 +906,13 @@ static void init(LZMADecompressor.Java self, int format, int memlimit) { @Specialization(guards = "format == FORMAT_AUTO") static void auto(LZMADecompressor.Native self, @SuppressWarnings("unused") int format, int memlimit, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaAutoDecoder, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int decoderFlags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; - int lzret = lzmaSupport.lzmaAutoDecoder(lzmast, memlimit, decoderFlags, lzmaAutoDecoder); + int lzret = lzmaSupport.lzmaAutoDecoder(lzmast, memlimit, decoderFlags); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -941,15 +921,13 @@ static void auto(LZMADecompressor.Native self, @SuppressWarnings("unused") int f @Specialization(guards = "format == FORMAT_XZ") static void xz(LZMADecompressor.Native self, @SuppressWarnings("unused") int format, int memlimit, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaStreamDecoder, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int decoderFlags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; - int lzret = lzmaSupport.lzmaStreamDecoder(lzmast, memlimit, decoderFlags, lzmaStreamDecoder); + int lzret = lzmaSupport.lzmaStreamDecoder(lzmast, memlimit, decoderFlags); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -958,14 +936,12 @@ static void xz(LZMADecompressor.Native self, @SuppressWarnings("unused") int for @Specialization(guards = "format == FORMAT_ALONE") static void alone(LZMADecompressor.Native self, @SuppressWarnings("unused") int format, int memlimit, @Bind Node inliningTarget, - @Shared("cs") @Cached NativeLibrary.InvokeNativeFunction createStream, - @Exclusive @Cached NativeLibrary.InvokeNativeFunction lzmaAloneDecoder, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); - int lzret = lzmaSupport.lzmaAloneDecoder(lzmast, memlimit, lzmaAloneDecoder); + int lzret = lzmaSupport.lzmaAloneDecoder(lzmast, memlimit); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -988,17 +964,15 @@ static void rawJava(VirtualFrame frame, LZMADecompressor.Java self, Object filte @Specialization static void rawNative(VirtualFrame frame, Node inliningTarget, LZMADecompressor.Native self, Object filters, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction createStream, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction lzmaRawDecoder, @Cached(inline = false) NativeFilterChain filterChain, @Cached InlinedConditionProfile errProfile, @Cached PRaiseNode raiseNode) { PythonContext context = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = context.getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, context, filters); - int lzret = lzmaSupport.lzmaRawDecoder(lzmast, lzmaRawDecoder); + int lzret = lzmaSupport.lzmaRawDecoder(lzmast); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { errorHandling(inliningTarget, lzret, raiseNode); } @@ -1107,23 +1081,18 @@ public abstract static class InternalDecompressNode extends PNodeWithContext { @Specialization static byte[] nativeInternalDecompress(Node inliningTarget, LZMADecompressor.Native self, int maxLength, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction decompress, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getLzsAvailIn, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getLzsAvailOut, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getNextInIndex, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getLzsCheck, @Cached GetOutputNativeBufferNode getBuffer, @Exclusive @Cached PRaiseNode lazyRaiseNode, @Cached InlinedConditionProfile errProfile) { PythonContext context = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = context.getNFILZMASupport(); + NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); byte[] inGuest = self.getNextIn(); int offset = self.getNextInIndex(); - int err = lzmaSupport.decompress(self.getLzs(), inGuest, offset, maxLength, INITIAL_BUFFER_SIZE, self.getLzsAvailIn(), decompress); - long nextInIdx = lzmaSupport.getNextInIndex(self.getLzs(), getNextInIndex); - long lzsAvailIn = lzmaSupport.getLzsAvailIn(self.getLzs(), getLzsAvailIn); - long lzsAvailOut = lzmaSupport.getLzsAvailOut(self.getLzs(), getLzsAvailOut); - self.setCheck(lzmaSupport.getLzsCheck(self.getLzs(), getLzsCheck)); + int err = lzmaSupport.decompress(self.getLzs(), inGuest, offset, maxLength, INITIAL_BUFFER_SIZE, self.getLzsAvailIn()); + long nextInIdx = lzmaSupport.getNextInIndex(self.getLzs()); + long lzsAvailIn = lzmaSupport.getLzsAvailIn(self.getLzs()); + long lzsAvailOut = lzmaSupport.getLzsAvailOut(self.getLzs()); + self.setCheck(lzmaSupport.getLzsCheck(self.getLzs())); try { self.setNextInIndex(nextInIdx); self.setLzsAvailIn(lzsAvailIn); @@ -1222,17 +1191,15 @@ private static void doDecompression(LZMADecompressor.Java self, ByteArrayOutputS @GenerateCached(false) public abstract static class GetOutputNativeBufferNode extends Node { - public abstract byte[] execute(Node inliningTarget, Object lzmast, PythonContext context); + public abstract byte[] execute(Node inliningTarget, long lzmast, PythonContext context); @Specialization - static byte[] getBuffer(Node inliningTarget, Object lzmast, PythonContext context, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getBufferSize, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction getBuffer, + static byte[] getBuffer(Node inliningTarget, long lzmast, PythonContext context, @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = context.getNFILZMASupport(); + NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); int size; try { - size = PInt.intValueExact(lzmaSupport.getOutputBufferSize(lzmast, getBufferSize)); + size = PInt.intValueExact(lzmaSupport.getOutputBufferSize(lzmast)); } catch (OverflowException of) { throw raiseNode.raise(inliningTarget, SystemError, VALUE_TOO_LARGE_TO_FIT_INTO_INDEX); } @@ -1241,7 +1208,7 @@ static byte[] getBuffer(Node inliningTarget, Object lzmast, PythonContext contex } byte[] resultArray = new byte[size]; /* this will clear the native output once retrieved */ - lzmaSupport.getOutputBuffer(lzmast, resultArray, getBuffer); + lzmaSupport.getOutputBuffer(lzmast, resultArray); return resultArray; } } @@ -1254,13 +1221,12 @@ public abstract static class IsCheckSupported extends Node { @NonIdempotent protected boolean useNativeContext() { - return PythonContext.get(this).getNFILZMASupport().isAvailable(); + return PythonContext.get(this).getNativeLZMASupport().isAvailable(); } @Specialization(guards = "useNativeContext()") - static boolean checkNative(Node inliningTarget, int checkId, - @Cached(inline = false) NativeLibrary.InvokeNativeFunction checkIsSupported) { - return PythonContext.get(inliningTarget).getNFILZMASupport().checkIsSupported(checkId, checkIsSupported) == 1; + static boolean checkNative(Node inliningTarget, int checkId) { + return PythonContext.get(inliningTarget).getNativeLZMASupport().checkIsSupported(checkId) == 1; } @TruffleBoundary @@ -1282,7 +1248,7 @@ public abstract static class EncodeFilterProperties extends Node { @NonIdempotent protected boolean useNativeContext() { - return PythonContext.get(this).getNFILZMASupport().isAvailable(); + return PythonContext.get(this).getNativeLZMASupport().isAvailable(); } @Specialization(guards = "useNativeContext()") @@ -1290,25 +1256,22 @@ static byte[] encodeNative(VirtualFrame frame, Object filter, @Bind Node inliningTarget, @Cached LZMAFilterConverter filterConverter, @Cached GetOutputNativeBufferNode getBuffer, - @Cached NativeLibrary.InvokeNativeFunction createStream, - @Cached NativeLibrary.InvokeNativeFunction encodeFilter, - @Cached NativeLibrary.InvokeNativeFunction deallocateStream, @Cached InlinedConditionProfile errProfile, @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport(); - Object lzmast = lzmaSupport.createStream(createStream); + NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + long lzmast = lzmaSupport.createStream(); long[] opts = filterConverter.execute(frame, filter); - int lzret = lzmaSupport.encodeFilter(lzmast, opts, encodeFilter); + int lzret = lzmaSupport.encodeFilter(lzmast, opts); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { - lzmaSupport.deallocateStream(lzmast, deallocateStream); + lzmaSupport.deallocateStream(lzmast); if (lzret == LZMA_PRESET_ERROR) { throw raiseNode.raise(inliningTarget, LZMAError, INVALID_COMPRESSION_PRESET, opts[LZMAOption.preset.ordinal()]); } errorHandling(inliningTarget, lzret, raiseNode); } byte[] encoded = getBuffer.execute(inliningTarget, lzmast, ctxt); - lzmaSupport.deallocateStream(lzmast, deallocateStream); + lzmaSupport.deallocateStream(lzmast); return encoded; } @@ -1327,21 +1290,20 @@ public abstract static class DecodeFilterProperties extends Node { @NonIdempotent protected boolean useNativeContext() { - return PythonContext.get(this).getNFILZMASupport().isAvailable(); + return PythonContext.get(this).getNativeLZMASupport().isAvailable(); } @Specialization(guards = "useNativeContext()") static void decodeNative(VirtualFrame frame, long id, byte[] encoded, PDict dict, @Bind Node inliningTarget, - @Cached NativeLibrary.InvokeNativeFunction decodeFilter, @Cached HashingStorageSetItem setItem, @Cached InlinedConditionProfile errProfile, @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport(); + NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); long[] opts = new long[MAX_OPTS_INDEX]; int len = encoded.length; - int lzret = lzmaSupport.decodeFilter(id, encoded, len, opts, decodeFilter); + int lzret = lzmaSupport.decodeFilter(id, encoded, len, opts); if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) { if (lzret == LZMA_ID_ERROR) { throw raiseNode.raise(inliningTarget, LZMAError, INVALID_FILTER, opts[LZMAOption.id.ordinal()]); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java index de4181604f..3aa9334fbe 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -77,11 +77,11 @@ public LZMAObject(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public void setCheck(int check) { + public final void setCheck(int check) { this.check = check; } - public int getCheck() { + public final int getCheck() { return check; } @@ -102,7 +102,7 @@ public void setFlushed() { this.flushed = true; } - public static class Java extends LZMACompressor { + public static final class Java extends LZMACompressor { private FinishableOutputStream lzmaStream; private final ByteArrayOutputStream bos; @@ -151,7 +151,7 @@ public void resetBuffer() { } } - public static class Native extends LZMACompressor { + public static final class Native extends LZMACompressor { private NFILZMASupport.Pointer pointer; @@ -159,22 +159,27 @@ public Native(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public final void init(Object lzmast, NFILZMASupport lib) { + public void init(long lzmast, NFILZMASupport lib) { this.pointer = new NFILZMASupport.Pointer(this, lzmast, lib); + assert !pointer.isReleased(); } - public final Object getLzs() { - assert pointer != null; - return pointer.getReference(); + public long getLzs() { + NFILZMASupport.Pointer p = pointer; + assert p != null && !p.isReleased(); + return p.getPointer(); } @TruffleBoundary - public final void markReleased() { - if (pointer != null) { - synchronized (this) { - pointer.markReleased(); - pointer = null; - } + public void markReleased() { + NFILZMASupport.Pointer p; + synchronized (this) { + p = pointer; + pointer = null; + } + if (p != null) { + boolean markedReleased = p.markReleased(); + assert markedReleased || p.isReleased(); } } } @@ -210,65 +215,65 @@ public LZMADecompressor(Object cls, Shape instanceShape) { this.nextInIndex = 0; } - public void setMemlimit(int memlimit) { + public final void setMemlimit(int memlimit) { this.memlimit = memlimit; } - public int getMemlimit() { + public final int getMemlimit() { return memlimit; } - public void setFormat(int format) { + public final void setFormat(int format) { this.format = format; } - public boolean isEOF() { + public final boolean isEOF() { return eof; } - public void setEOF() { + public final void setEOF() { this.eof = true; } - public void setEOF(boolean b) { + public final void setEOF(boolean b) { this.eof = b; } - public byte[] getUnusedData() { + public final byte[] getUnusedData() { return unusedData; } - public void setUnusedData() { + public final void setUnusedData() { this.unusedData = Arrays.copyOfRange(nextIn, nextInIndex, nextInIndex + lzsAvailIn); } - public boolean needsInput() { + public final boolean needsInput() { return needsInput; } - public void setNeedsInput(boolean needsInput) { + public final void setNeedsInput(boolean needsInput) { this.needsInput = needsInput; } - public byte[] getInputBuffer() { + public final byte[] getInputBuffer() { return inputBuffer; } - public void setInputBuffer(byte[] inputBuffer) { + public final void setInputBuffer(byte[] inputBuffer) { this.inputBuffer = inputBuffer; } - public void createInputBuffer(int size) { + public final void createInputBuffer(int size) { this.inputBuffer = new byte[size]; this.inputBufferSize = size; } - public void discardInputBuffer() { + public final void discardInputBuffer() { this.inputBuffer = null; this.inputBufferSize = 0; } - public void resizeInputBuffer(int size) { + public final void resizeInputBuffer(int size) { assert size >= inputBufferSize; byte[] tmp = new byte[size]; if (inputBuffer != null && lzsAvailIn != 0) { @@ -278,68 +283,68 @@ public void resizeInputBuffer(int size) { this.inputBufferSize = size; } - public int getInputBufferSize() { + public final int getInputBufferSize() { return inputBufferSize; } - public void setInputBufferSize(int inputBufferSize) { + public final void setInputBufferSize(int inputBufferSize) { this.inputBufferSize = inputBufferSize; } - public byte[] getNextIn() { + public final byte[] getNextIn() { return nextIn; } - public void setNextIn(byte[] in) { + public final void setNextIn(byte[] in) { assert in != null; this.nextIn = in; } - public void clearNextIn() { + public final void clearNextIn() { this.nextIn = null; } - public int getNextInIndex() { + public final int getNextInIndex() { return nextInIndex; } - public void setNextInIndex(int nextInIndex) { + public final void setNextInIndex(int nextInIndex) { this.nextInIndex = nextInIndex; } - public void setNextInIndex(long nextInIndex) throws OverflowException { + public final void setNextInIndex(long nextInIndex) throws OverflowException { this.nextInIndex = PInt.intValueExact(nextInIndex); } - public int getLzsAvailIn() { + public final int getLzsAvailIn() { return lzsAvailIn; } - public int getLzsAvailOut() { + public final int getLzsAvailOut() { return lzsAvailOut; } - public void incLzsAvailIn(int size) { + public final void incLzsAvailIn(int size) { this.lzsAvailIn += size; } - public void setLzsAvailIn(int lzsAvailIn) { + public final void setLzsAvailIn(int lzsAvailIn) { this.lzsAvailIn = lzsAvailIn; } - public void setLzsAvailIn(long lzsAvailIn) throws OverflowException { + public final void setLzsAvailIn(long lzsAvailIn) throws OverflowException { this.lzsAvailIn = PInt.intValueExact(lzsAvailIn); } - public void setLzsAvailOut(int lzsAvailOut) { + public final void setLzsAvailOut(int lzsAvailOut) { this.lzsAvailOut = lzsAvailOut; } - public void setLzsAvailOut(long lzsAvailOut) throws OverflowException { + public final void setLzsAvailOut(long lzsAvailOut) throws OverflowException { this.lzsAvailOut = PInt.intValueExact(lzsAvailOut); } - public static class Java extends LZMADecompressor { + public static final class Java extends LZMADecompressor { private LZMANodes.LZMAByteInputStream input; private InputStream lzs; @@ -403,7 +408,7 @@ public void update(int availOut) { } @TruffleBoundary - protected static LZMANodes.LZMAByteInputStream createLZMAByteInputStream(byte[] buf, int offset, int length) { + private static LZMANodes.LZMAByteInputStream createLZMAByteInputStream(byte[] buf, int offset, int length) { return new LZMANodes.LZMAByteInputStream(buf, offset, length); } @@ -453,7 +458,7 @@ public void createLZMA() throws IOException { } } - public static class Native extends LZMADecompressor { + public static final class Native extends LZMADecompressor { private NFILZMASupport.Pointer pointer; @@ -461,22 +466,27 @@ public Native(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public final void init(Object lzmast, NFILZMASupport lib) { + public void init(long lzmast, NFILZMASupport lib) { this.pointer = new NFILZMASupport.Pointer(this, lzmast, lib); + assert !pointer.isReleased(); } - public final Object getLzs() { - assert pointer != null; - return pointer.getReference(); + public long getLzs() { + NFILZMASupport.Pointer p = pointer; + assert p != null && !p.isReleased(); + return p.getPointer(); } @TruffleBoundary - public final void markReleased() { - if (pointer != null) { - synchronized (this) { - pointer.markReleased(); - pointer = null; - } + public void markReleased() { + NFILZMASupport.Pointer p; + synchronized (this) { + p = pointer; + pointer = null; + } + if (p != null) { + boolean markedReleased = p.markReleased(); + assert markedReleased || p.isReleased(); } } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java index 8382844ca4..24ba142f57 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -40,19 +40,23 @@ */ package com.oracle.graal.python.runtime; +import static com.oracle.graal.python.annotations.NativeSimpleType.DOUBLE; +import static com.oracle.graal.python.annotations.NativeSimpleType.POINTER; +import static com.oracle.graal.python.annotations.NativeSimpleType.SINT32; +import static com.oracle.graal.python.annotations.NativeSimpleType.SINT64; +import static com.oracle.graal.python.annotations.NativeSimpleType.VOID; + import com.oracle.graal.python.PythonLanguage; -import com.oracle.truffle.api.CompilerAsserts; -import com.oracle.truffle.api.CompilerDirectives; +import com.oracle.graal.python.annotations.DowncallSignature; +import com.oracle.graal.python.runtime.nativeaccess.NativeLibrary; +import com.oracle.graal.python.runtime.nativeaccess.NativeMemory; import com.oracle.truffle.api.ThreadLocalAction.Access; import com.oracle.truffle.api.TruffleLogger; -/*- - * Generated using: - * scripts/nfi_gen.py -name LZMA -cpath graalpython/com.oracle.graal.python.cext/lzma/lzma.c -lib liblzmasupport - */ -public class NFILZMASupport { +public final class NFILZMASupport extends NativeCompressionSupport { private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFILZMASupport.class); + private static final String SUPPORTING_NATIVE_LIB_NAME = "lzmasupport"; public static final int FORMAT_AUTO_INDEX = 0; public static final int FORMAT_XZ_INDEX = 1; @@ -98,220 +102,108 @@ public class NFILZMASupport { public static final int LZMA_ID_ERROR = 98; public static final int LZMA_PRESET_ERROR = 99; - enum LZMANativeFunctions implements NativeLibrary.NativeFunction { - - /*- - nfi_function: name('getMarcos') static(true) - void get_macros(int* formats, int* checks, uint64_t* filters, int* mfs, int* modes, uint64_t* preset) - */ - get_macros("([SINT32], [SINT32], [UINT64], [SINT32], [SINT32], [UINT64]): VOID"), - - /*- - nfi_function: name('createStream') map('lzmast_stream*', 'POINTER') - lzmast_stream *lzma_create_lzmast_stream() - */ - lzma_create_lzmast_stream("(): POINTER"), - - /*- - nfi_function: name('getTimeElapsed') map('lzmast_stream*', 'POINTER') static(true) - double lzma_get_timeElapsed(lzmast_stream* lzmast) - */ - lzma_get_timeElapsed("(POINTER): DOUBLE"), - - /*- - nfi_function: name('deallocateStream') map('lzmast_stream*', 'POINTER') - void lzma_free_stream(lzmast_stream* lzmast) - */ - lzma_free_stream("(POINTER): VOID"), - - /*- - nfi_function: name('gcReleaseHelper') map('lzmast_stream*', 'POINTER') release(true) - void lzma_gc_helper(lzmast_stream* lzmast) - */ - lzma_gc_helper("(POINTER): VOID"), - - /*- - nfi_function: name('getNextInIndex') map('lzmast_stream*', 'POINTER') - ssize_t lzma_get_next_in_index(lzmast_stream *lzmast) - */ - lzma_get_next_in_index("(POINTER): SINT64"), - - /*- - nfi_function: name('getLzsAvailIn') map('lzmast_stream*', 'POINTER') - size_t lzma_get_lzs_avail_in(lzmast_stream *lzmast) - */ - lzma_get_lzs_avail_in("(POINTER): UINT64"), - - /*- - nfi_function: name('getLzsAvailOut') map('lzmast_stream*', 'POINTER') - size_t lzma_get_lzs_avail_out(lzmast_stream *lzmast) - */ - lzma_get_lzs_avail_out("(POINTER): UINT64"), - - /*- - nfi_function: name('getLzsCheck') map('lzmast_stream*', 'POINTER') - int lzma_lzma_get_check(lzmast_stream *lzmast) - */ - lzma_lzma_get_check("(POINTER): SINT32"), - - /*- - nfi_function: name('setLzsAvailIn') map('lzmast_stream*', 'POINTER') - void lzma_set_lzs_avail_in(lzmast_stream *lzmast, size_t v) - */ - lzma_set_lzs_avail_in("(POINTER, UINT64): VOID"), - - /*- - nfi_function: name('getOutputBufferSize') map('lzmast_stream*', 'POINTER') - size_t lzma_get_output_buffer_size(lzmast_stream *lzmast) - */ - lzma_get_output_buffer_size("(POINTER): UINT64"), - - /*- - nfi_function: name('getOutputBuffer') map('lzmast_stream*', 'POINTER') - void lzma_get_output_buffer(lzmast_stream *lzmast, Byte *dest) - */ - lzma_get_output_buffer("(POINTER, [UINT8]): VOID"), - - /*- - nfi_function: name('checkIsSupported') - int lzma_lzma_check_is_supported(int check_id) - */ - lzma_lzma_check_is_supported("(SINT32): SINT32"), - - /*- - nfi_function: name('setFilterSpecLZMA') map('lzmast_stream*', 'POINTER') - int lzma_set_filter_spec_lzma(lzmast_stream *lzmast, int fidx, int64_t* opts) - */ - lzma_set_filter_spec_lzma("(POINTER, SINT32, [SINT64]): SINT32"), - - /*- - nfi_function: name('setFilterSpecDelta') map('lzmast_stream*', 'POINTER') - int lzma_set_filter_spec_delta(lzmast_stream *lzmast, int fidx, int64_t* opts) - */ - lzma_set_filter_spec_delta("(POINTER, SINT32, [SINT64]): SINT32"), - - /*- - nfi_function: name('setFilterSpecBCJ') map('lzmast_stream*', 'POINTER') - int lzma_set_filter_spec_bcj(lzmast_stream *lzmast, int fidx, int64_t* opts) - */ - lzma_set_filter_spec_bcj("(POINTER, SINT32, [SINT64]): SINT32"), - - /*- - nfi_function: name('encodeFilter') map('lzmast_stream*', 'POINTER') - int lzma_encode_filter_spec(lzmast_stream *lzmast, int64_t* opts) - */ - lzma_encode_filter_spec("(POINTER, [SINT64]): SINT32"), - - /*- - nfi_function: name('decodeFilter') map('lzmast_stream*', 'POINTER') - int lzma_decode_filter_spec(int64_t filter_id, Byte* encoded_props, int len, int64_t *opts) - */ - lzma_decode_filter_spec("(SINT64, [UINT8], SINT32, [SINT64]): SINT32"), - - /*- - nfi_function: name('lzmaEasyEncoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_easy_encoder(lzmast_stream *lzmast, uint32_t preset, int check) - */ - lzma_lzma_easy_encoder("(POINTER, UINT32, SINT32): SINT32"), - - /*- - nfi_function: name('lzmaStreamEncoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_stream_encoder(lzmast_stream *lzmast, int check) - */ - lzma_lzma_stream_encoder("(POINTER, SINT32): SINT32"), - - /*- - nfi_function: name('lzmaAloneEncoderPreset') map('lzmast_stream*', 'POINTER') - int lzma_lzma_alone_encoder_preset(lzmast_stream *lzmast, uint32_t preset) - */ - lzma_lzma_alone_encoder_preset("(POINTER, UINT32): SINT32"), - - /*- - nfi_function: name('lzmaAloneEncoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_alone_encoder(lzmast_stream *lzmast) - */ - lzma_lzma_alone_encoder("(POINTER): SINT32"), - - /*- - nfi_function: name('lzmaRawEncoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_raw_encoder(lzmast_stream *lzmast) - */ - lzma_lzma_raw_encoder("(POINTER): SINT32"), - - /*- - nfi_function: name('compress') map('lzmast_stream*', 'POINTER') - int lzma_compress(lzmast_stream *lzmast, Byte *data, size_t len, int iaction, ssize_t bufsize) - */ - lzma_compress("(POINTER, [UINT8], UINT64, SINT32, SINT64): SINT32"), - - /*- - nfi_function: name('lzmaRawDecoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_raw_decoder(lzmast_stream *lzmast) - */ - lzma_lzma_raw_decoder("(POINTER): SINT32"), - - /*- - nfi_function: name('lzmaAutoDecoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_auto_decoder(lzmast_stream *lzmast, uint64_t memlimit, uint32_t decoder_flags) - */ - lzma_lzma_auto_decoder("(POINTER, UINT64, UINT32): SINT32"), - - /*- - nfi_function: name('lzmaStreamDecoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_stream_decoder(lzmast_stream *lzmast, uint64_t memlimit, uint32_t decoder_flags) - */ - lzma_lzma_stream_decoder("(POINTER, UINT64, UINT32): SINT32"), - - /*- - nfi_function: name('lzmaAloneDecoder') map('lzmast_stream*', 'POINTER') - int lzma_lzma_alone_decoder(lzmast_stream *lzmast, uint64_t memlimit) - */ - lzma_lzma_alone_decoder("(POINTER, UINT64): SINT32"), - - /*- - nfi_function: name('decompress') map('lzmast_stream*', 'POINTER') - int lzma_decompress(lzmast_stream *lzmast, Byte *input_buffer, ssize_t offset,ssize_t max_length,ssize_t bufsize, size_t lzs_avail_in) - */ - lzma_decompress("(POINTER, [UINT8], SINT64, SINT64, SINT64, UINT64): SINT32"); - - private final String signature; - - LZMANativeFunctions(String signature) { - this.signature = signature; - } + abstract static class LZMANativeFunctions { + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER, POINTER, POINTER, POINTER, POINTER, POINTER}) + abstract void get_macros(long formats, long checks, long filters, long mfs, long modes, long preset); - @Override - public String signature() { - return signature; - } - } + @DowncallSignature(returnType = POINTER) + abstract long lzma_create_lzmast_stream(); - private static final String SUPPORTING_NATIVE_LIB_NAME = "lzmasupport"; + @DowncallSignature(returnType = DOUBLE, argumentTypes = {POINTER}) + abstract double lzma_get_timeElapsed(long lzmast); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER}) + abstract void lzma_free_stream(long lzmast); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER}) + abstract void lzma_gc_helper(long lzmast); + + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long lzma_get_next_in_index(long lzmast); + + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long lzma_get_lzs_avail_in(long lzmast); + + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long lzma_get_lzs_avail_out(long lzmast); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int lzma_lzma_get_check(long lzmast); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER, SINT64}) + abstract void lzma_set_lzs_avail_in(long lzmast, long v); + + @DowncallSignature(returnType = SINT64, argumentTypes = {POINTER}) + abstract long lzma_get_output_buffer_size(long lzmast); + + @DowncallSignature(returnType = VOID, argumentTypes = {POINTER, POINTER}) + abstract void lzma_get_output_buffer(long lzmast, long dest); + + @DowncallSignature(returnType = SINT32, argumentTypes = {SINT32}) + abstract int lzma_lzma_check_is_supported(int checkId); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, POINTER}) + abstract int lzma_set_filter_spec_lzma(long lzmast, int fidx, long opts); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, POINTER}) + abstract int lzma_set_filter_spec_delta(long lzmast, int fidx, long opts); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, POINTER}) + abstract int lzma_set_filter_spec_bcj(long lzmast, int fidx, long opts); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER}) + abstract int lzma_encode_filter_spec(long lzmast, long opts); + + @DowncallSignature(returnType = SINT32, argumentTypes = {SINT64, POINTER, SINT32, POINTER}) + abstract int lzma_decode_filter_spec(long filterId, long encodedProps, int len, long opts); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32, SINT32}) + abstract int lzma_lzma_easy_encoder(long lzmast, int preset, int check); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32}) + abstract int lzma_lzma_stream_encoder(long lzmast, int check); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT32}) + abstract int lzma_lzma_alone_encoder_preset(long lzmast, int preset); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int lzma_lzma_alone_encoder(long lzmast); - private final PythonContext pythonContext; - private final NativeLibrary.TypedNativeLibrary typedNativeLib; - - @CompilerDirectives.CompilationFinal private boolean available; - - private NFILZMASupport(PythonContext context, NativeLibrary.NFIBackend backend, String noNativeAccessHelp) { - if (context.useNativeCompressionModules()) { - this.pythonContext = context; - this.typedNativeLib = NativeLibrary.create(PythonContext.getSupportLibName(SUPPORTING_NATIVE_LIB_NAME), LZMANativeFunctions.values(), - backend, noNativeAccessHelp, true); - this.available = true; - } else { - this.pythonContext = null; - this.typedNativeLib = null; - this.available = false; + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int lzma_lzma_raw_encoder(long lzmast); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT32, SINT64}) + abstract int lzma_compress(long lzmast, long data, long len, int iaction, long bufsize); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER}) + abstract int lzma_lzma_raw_decoder(long lzmast); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT64, SINT32}) + abstract int lzma_lzma_auto_decoder(long lzmast, long memlimit, int decoderFlags); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT64, SINT32}) + abstract int lzma_lzma_stream_decoder(long lzmast, long memlimit, int decoderFlags); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, SINT64}) + abstract int lzma_lzma_alone_decoder(long lzmast, long memlimit); + + @DowncallSignature(returnType = SINT32, argumentTypes = {POINTER, POINTER, SINT64, SINT64, SINT64, SINT64}) + abstract int lzma_decompress(long lzmast, long inputBuffer, long offset, long maxLength, long bufsize, long lzsAvailIn); + + static NativeLibrary loadNativeLibrary(PythonContext context) { + return NativeCompressionSupport.loadNativeLibrary(context, SUPPORTING_NATIVE_LIB_NAME); } } - public static NFILZMASupport createNative(PythonContext context, String noNativeAccessHelp) { - return new NFILZMASupport(context, NativeLibrary.NFIBackend.NATIVE, noNativeAccessHelp); + private final LZMANativeFunctions nativeFunctions; + + private NFILZMASupport(PythonContext context) { + super(context); + this.nativeFunctions = isAvailable() ? new LZMANativeFunctionsGen(context) : null; } - public static NFILZMASupport createLLVM(PythonContext context, String noNativeAccessHelp) { - return new NFILZMASupport(context, NativeLibrary.NFIBackend.LLVM, noNativeAccessHelp); + public static NFILZMASupport createNative(PythonContext context, String noNativeAccessHelp) { + return new NFILZMASupport(context); } static class PointerReleaseCallback implements AsyncHandler.AsyncAction { @@ -323,17 +215,15 @@ public PointerReleaseCallback(Pointer pointer) { @Override public void execute(PythonContext context, Access access) { - synchronized (pointer) { - if (pointer.isReleased()) { - return; - } - try { - pointer.doRelease(); - pointer.markReleased(); - LOGGER.finest("NFILZMASupport pointer has been freed"); - } catch (Exception e) { - LOGGER.severe("Error while trying to free NFILZMASupport pointer: " + e.getMessage()); - } + if (!pointer.markReleased()) { + assert pointer.isReleased(); + return; + } + try { + pointer.doRelease(); + LOGGER.finest("NFILZMASupport pointer has been freed"); + } catch (Exception e) { + LOGGER.severe("Error while trying to free NFILZMASupport pointer: " + e.getMessage()); } } } @@ -341,14 +231,20 @@ public void execute(PythonContext context, Access access) { public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference { private final NFILZMASupport lib; + private final long pointer; - public Pointer(Object referent, Object ptr, NFILZMASupport lib) { - super(referent, ptr, lib.pythonContext.getSharedFinalizer()); + public Pointer(Object referent, long pointer, NFILZMASupport lib) { + super(referent, lib.pythonContext.getSharedFinalizer()); this.lib = lib; + this.pointer = pointer; + } + + public long getPointer() { + return pointer; } protected void doRelease() { - lib.gcReleaseHelper(getReference()); + lib.gcReleaseHelper(pointer); } @Override @@ -360,337 +256,195 @@ public AsyncHandler.AsyncAction release() { } } - public void notAvailable() { - if (available) { - CompilerAsserts.neverPartOfCompilation("Checking NFILZMASupport availability should only be done during initialization."); - available = false; + public void getMacros(int[] formats, int[] checks, long[] filters, int[] mfs, int[] modes, long[] preset) { + long nativeFormats = copyToNativeIntArray(formats); + long nativeChecks = copyToNativeIntArray(checks); + long nativeFilters = copyToNativeLongArray(filters); + long nativeMfs = copyToNativeIntArray(mfs); + long nativeModes = copyToNativeIntArray(modes); + long nativePreset = copyToNativeLongArray(preset); + try { + nativeFunctions.get_macros(nativeFormats, nativeChecks, nativeFilters, nativeMfs, nativeModes, nativePreset); + NativeMemory.readIntArrayElements(nativeFormats, 0, formats, 0, formats.length); + NativeMemory.readIntArrayElements(nativeChecks, 0, checks, 0, checks.length); + NativeMemory.readLongArrayElements(nativeFilters, 0, filters, 0, filters.length); + NativeMemory.readIntArrayElements(nativeMfs, 0, mfs, 0, mfs.length); + NativeMemory.readIntArrayElements(nativeModes, 0, modes, 0, modes.length); + NativeMemory.readLongArrayElements(nativePreset, 0, preset, 0, preset.length); + } finally { + NativeMemory.free(nativeFormats); + NativeMemory.free(nativeChecks); + NativeMemory.free(nativeFilters); + NativeMemory.free(nativeMfs); + NativeMemory.free(nativeModes); + NativeMemory.free(nativePreset); + } + } + + public Object getTimeElapsed(long lzmast) { + return nativeFunctions.lzma_get_timeElapsed(lzmast); + } + + public Object gcReleaseHelper(long lzmast) { + nativeFunctions.lzma_gc_helper(lzmast); + return null; + } + + public long createStream() { + return nativeFunctions.lzma_create_lzmast_stream(); + } + + public void deallocateStream(long lzmast) { + nativeFunctions.lzma_free_stream(lzmast); + } + + public long getNextInIndex(long lzmast) { + return nativeFunctions.lzma_get_next_in_index(lzmast); + } + + public long getLzsAvailIn(long lzmast) { + return nativeFunctions.lzma_get_lzs_avail_in(lzmast); + } + + public long getLzsAvailOut(long lzmast) { + return nativeFunctions.lzma_get_lzs_avail_out(lzmast); + } + + public int getLzsCheck(long lzmast) { + return nativeFunctions.lzma_lzma_get_check(lzmast); + } + + public void setLzsAvailIn(long lzmast, long v) { + nativeFunctions.lzma_set_lzs_avail_in(lzmast, v); + } + + public long getOutputBufferSize(long lzmast) { + return nativeFunctions.lzma_get_output_buffer_size(lzmast); + } + + public void getOutputBuffer(long lzmast, byte[] dest) { + if (dest.length == 0) { + return; + } + long nativeDest = NativeMemory.mallocByteArray(dest.length); + try { + nativeFunctions.lzma_get_output_buffer(lzmast, nativeDest); + NativeMemory.readByteArrayElements(nativeDest, 0, dest, 0, dest.length); + } finally { + NativeMemory.free(nativeDest); + } + } + + public int checkIsSupported(int checkId) { + return nativeFunctions.lzma_lzma_check_is_supported(checkId); + } + + public int setFilterSpecLZMA(long lzmast, int fidx, long[] opts) { + long nativeOpts = copyToNativeLongArray(opts); + try { + return nativeFunctions.lzma_set_filter_spec_lzma(lzmast, fidx, nativeOpts); + } finally { + NativeMemory.free(nativeOpts); } } - public boolean isAvailable() { - return available; - } - - /** - * - * @param formats int* formats - * @param checks int* checks - * @param filters uint64_t* filters - * @param mfs int* mfs - * @param modes int* modes - * @param preset uint64_t* preset - * - */ - public Object getMacros(int[] formats, int[] checks, long[] filters, int[] mfs, int[] modes, long[] preset) { - return typedNativeLib.callUncached(pythonContext, LZMANativeFunctions.get_macros, formats, checks, filters, mfs, modes, preset); - } - - /** - * - * @param lzmast lzmast_stream* lzmast - * @return double - */ - public Object getTimeElapsed(Object lzmast) { - return typedNativeLib.callUncached(pythonContext, LZMANativeFunctions.lzma_get_timeElapsed, lzmast); - } - - /** - * - * @param lzmast lzmast_stream* lzmast - * - */ - public Object gcReleaseHelper(Object lzmast) { - return typedNativeLib.callUncached(pythonContext, LZMANativeFunctions.lzma_gc_helper, lzmast); - } - - /** - * - * - * @return lzmast_stream* - */ - public Object createStream( - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.call(typedNativeLib, LZMANativeFunctions.lzma_create_lzmast_stream); - } - - /** - * - * @param lzmast lzmast_stream* lzmast - * - */ - public void deallocateStream(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, LZMANativeFunctions.lzma_free_stream, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return ssize_t - */ - public long getNextInIndex(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, LZMANativeFunctions.lzma_get_next_in_index, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return size_t - */ - public long getLzsAvailIn(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, LZMANativeFunctions.lzma_get_lzs_avail_in, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return size_t - */ - public long getLzsAvailOut(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, LZMANativeFunctions.lzma_get_lzs_avail_out, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return int - */ - public int getLzsCheck(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_get_check, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param v size_t v - * - */ - public void setLzsAvailIn(Object lzmast, long v, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, LZMANativeFunctions.lzma_set_lzs_avail_in, lzmast, v); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return size_t - */ - public long getOutputBufferSize(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callLong(typedNativeLib, LZMANativeFunctions.lzma_get_output_buffer_size, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param dest Byte *dest - * - */ - public void getOutputBuffer(Object lzmast, byte[] dest, - NativeLibrary.InvokeNativeFunction invokeNode) { - invokeNode.call(typedNativeLib, LZMANativeFunctions.lzma_get_output_buffer, lzmast, dest); - } - - /** - * - * @param check_id int check_id - * @return int - */ - public int checkIsSupported(int check_id, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_check_is_supported, check_id); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param fidx int fidx - * @param opts int64_t* opts - * @return int - */ - public int setFilterSpecLZMA(Object lzmast, int fidx, long[] opts, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_set_filter_spec_lzma, lzmast, fidx, opts); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param fidx int fidx - * @param opts int64_t* opts - * @return int - */ - public int setFilterSpecDelta(Object lzmast, int fidx, long[] opts, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_set_filter_spec_delta, lzmast, fidx, opts); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param fidx int fidx - * @param opts int64_t* opts - * @return int - */ - public int setFilterSpecBCJ(Object lzmast, int fidx, long[] opts, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_set_filter_spec_bcj, lzmast, fidx, opts); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param opts int64_t* opts - * @return int - */ - public int encodeFilter(Object lzmast, long[] opts, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_encode_filter_spec, lzmast, opts); - } - - /** - * - * @param filter_id int64_t filter_id - * @param encoded_props Byte* encoded_props - * @param len int len - * @param opts int64_t *opts - * @return int - */ - public int decodeFilter(long filter_id, byte[] encoded_props, int len, long[] opts, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_decode_filter_spec, filter_id, encoded_props, len, opts); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param preset uint32_t preset - * @param check int check - * @return int - */ - public int lzmaEasyEncoder(Object lzmast, long preset, int check, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_easy_encoder, lzmast, preset, check); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param check int check - * @return int - */ - public int lzmaStreamEncoder(Object lzmast, int check, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_stream_encoder, lzmast, check); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param preset uint32_t preset - * @return int - */ - public int lzmaAloneEncoderPreset(Object lzmast, long preset, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_alone_encoder_preset, lzmast, preset); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return int - */ - public int lzmaAloneEncoder(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_alone_encoder, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return int - */ - public int lzmaRawEncoder(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_raw_encoder, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param data Byte *data - * @param len size_t len - * @param iaction int iaction - * @param bufsize ssize_t bufsize - * @return int - */ - public int compress(Object lzmast, byte[] data, long len, int iaction, long bufsize, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_compress, lzmast, data, len, iaction, bufsize); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @return int - */ - public int lzmaRawDecoder(Object lzmast, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_raw_decoder, lzmast); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param memlimit uint64_t memlimit - * @param decoder_flags uint32_t decoder_flags - * @return int - */ - public int lzmaAutoDecoder(Object lzmast, long memlimit, long decoder_flags, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_auto_decoder, lzmast, memlimit, decoder_flags); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param memlimit uint64_t memlimit - * @param decoder_flags uint32_t decoder_flags - * @return int - */ - public int lzmaStreamDecoder(Object lzmast, long memlimit, long decoder_flags, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_stream_decoder, lzmast, memlimit, decoder_flags); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param memlimit uint64_t memlimit - * @return int - */ - public int lzmaAloneDecoder(Object lzmast, long memlimit, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_lzma_alone_decoder, lzmast, memlimit); - } - - /** - * - * @param lzmast lzmast_stream *lzmast - * @param input_buffer Byte *input_buffer - * @param offset ssize_t offset - * @param max_length ssize_t max_length - * @param bufsize ssize_t bufsize - * @param lzs_avail_in size_t lzs_avail_in - * @return int - */ - public int decompress(Object lzmast, byte[] input_buffer, long offset, long max_length, long bufsize, long lzs_avail_in, - NativeLibrary.InvokeNativeFunction invokeNode) { - return invokeNode.callInt(typedNativeLib, LZMANativeFunctions.lzma_decompress, lzmast, input_buffer, offset, max_length, bufsize, lzs_avail_in); + public int setFilterSpecDelta(long lzmast, int fidx, long[] opts) { + long nativeOpts = copyToNativeLongArray(opts); + try { + return nativeFunctions.lzma_set_filter_spec_delta(lzmast, fidx, nativeOpts); + } finally { + NativeMemory.free(nativeOpts); + } + } + + public int setFilterSpecBCJ(long lzmast, int fidx, long[] opts) { + long nativeOpts = copyToNativeLongArray(opts); + try { + return nativeFunctions.lzma_set_filter_spec_bcj(lzmast, fidx, nativeOpts); + } finally { + NativeMemory.free(nativeOpts); + } } + public int encodeFilter(long lzmast, long[] opts) { + long nativeOpts = copyToNativeLongArray(opts); + try { + return nativeFunctions.lzma_encode_filter_spec(lzmast, nativeOpts); + } finally { + NativeMemory.free(nativeOpts); + } + } + + public int decodeFilter(long filterId, byte[] encodedProps, int len, long[] opts) { + long nativeEncodedProps = copyToNativeByteArray(encodedProps, len); + long nativeOpts = copyToNativeLongArray(opts); + try { + int result = nativeFunctions.lzma_decode_filter_spec(filterId, nativeEncodedProps, len, nativeOpts); + NativeMemory.readLongArrayElements(nativeOpts, 0, opts, 0, opts.length); + return result; + } finally { + if (nativeEncodedProps != NativeMemory.NULLPTR) { + NativeMemory.free(nativeEncodedProps); + } + NativeMemory.free(nativeOpts); + } + } + + public int lzmaEasyEncoder(long lzmast, long preset, int check) { + return nativeFunctions.lzma_lzma_easy_encoder(lzmast, (int) preset, check); + } + + public int lzmaStreamEncoder(long lzmast, int check) { + return nativeFunctions.lzma_lzma_stream_encoder(lzmast, check); + } + + public int lzmaAloneEncoderPreset(long lzmast, long preset) { + return nativeFunctions.lzma_lzma_alone_encoder_preset(lzmast, (int) preset); + } + + public int lzmaAloneEncoder(long lzmast) { + return nativeFunctions.lzma_lzma_alone_encoder(lzmast); + } + + public int lzmaRawEncoder(long lzmast) { + return nativeFunctions.lzma_lzma_raw_encoder(lzmast); + } + + public int compress(long lzmast, byte[] data, long len, int iaction, long bufsize) { + long nativeData = copyToNativeByteArray(data, (int) len); + try { + return nativeFunctions.lzma_compress(lzmast, nativeData, len, iaction, bufsize); + } finally { + if (nativeData != NativeMemory.NULLPTR) { + NativeMemory.free(nativeData); + } + } + } + + public int lzmaRawDecoder(long lzmast) { + return nativeFunctions.lzma_lzma_raw_decoder(lzmast); + } + + public int lzmaAutoDecoder(long lzmast, long memlimit, long decoderFlags) { + return nativeFunctions.lzma_lzma_auto_decoder(lzmast, memlimit, (int) decoderFlags); + } + + public int lzmaStreamDecoder(long lzmast, long memlimit, long decoderFlags) { + return nativeFunctions.lzma_lzma_stream_decoder(lzmast, memlimit, (int) decoderFlags); + } + + public int lzmaAloneDecoder(long lzmast, long memlimit) { + return nativeFunctions.lzma_lzma_alone_decoder(lzmast, memlimit); + } + + public int decompress(long lzmast, byte[] inputBuffer, long offset, long maxLength, long bufsize, long lzsAvailIn) { + long nativeInputBuffer = copyToNativeByteArray(inputBuffer); + try { + return nativeFunctions.lzma_decompress(lzmast, nativeInputBuffer, offset, maxLength, bufsize, lzsAvailIn); + } finally { + if (nativeInputBuffer != NativeMemory.NULLPTR) { + NativeMemory.free(nativeInputBuffer); + } + } + } } diff --git a/graalpython/python-liblzma/src/lzma.c b/graalpython/python-liblzma/src/lzma.c index e05e1162df..26a5a88323 100644 --- a/graalpython/python-liblzma/src/lzma.c +++ b/graalpython/python-liblzma/src/lzma.c @@ -62,61 +62,61 @@ typedef uint8_t Byte; /* 8 bits */ #define MAX_FILTERS_SIZE (LZMA_FILTERS_MAX + 1) #define LZMA_CHECK_UNKNOWN (LZMA_CHECK_ID_MAX + 1) -#define FORMAT_AUTO_INDEX 0 // nfi_var -#define FORMAT_XZ_INDEX 1 // nfi_var -#define FORMAT_ALONE_INDEX 2 // nfi_var -#define FORMAT_RAW_INDEX 3 // nfi_var - -#define CHECK_NONE_INDEX 0 // nfi_var -#define CHECK_CRC32_INDEX 1 // nfi_var -#define CHECK_CRC64_INDEX 2 // nfi_var -#define CHECK_SHA256_INDEX 3 // nfi_var -#define CHECK_ID_MAX_INDEX 4 // nfi_var -#define CHECK_UNKNOWN_INDEX 5 // nfi_var - -#define FILTER_LZMA1_INDEX 0 // nfi_var -#define FILTER_LZMA2_INDEX 1 // nfi_var -#define FILTER_DELTA_INDEX 2 // nfi_var -#define FILTER_X86_INDEX 3 // nfi_var -#define FILTER_POWERPC_INDEX 4 // nfi_var -#define FILTER_IA64_INDEX 5 // nfi_var -#define FILTER_ARM_INDEX 6 // nfi_var -#define FILTER_ARMTHUMB_INDEX 7 // nfi_var -#define FILTER_SPARC_INDEX 8 // nfi_var - -#define MF_HC3_INDEX 0 // nfi_var -#define MF_HC4_INDEX 1 // nfi_var -#define MF_BT2_INDEX 2 // nfi_var -#define MF_BT3_INDEX 3 // nfi_var -#define MF_BT4_INDEX 4 // nfi_var - -#define MODE_FAST_INDEX 0 // nfi_var -#define MODE_NORMAL_INDEX 1 // nfi_var - -#define PRESET_DEFAULT_INDEX 0 // nfi_var -#define PRESET_EXTREME_INDEX 1 // nfi_var - - -#define ID_INDEX 0 // nfi_var -#define PRESET_INDEX 1 // nfi_var -#define DICT_SIZE_INDEX 2 // nfi_var -#define LC_INDEX 3 // nfi_var -#define LP_INDEX 4 // nfi_var -#define PB_INDEX 5 // nfi_var -#define MODE_INDEX 6 // nfi_var -#define NICE_LEN_INDEX 7 // nfi_var -#define MF_INDEX 8 // nfi_var -#define DEPTH_INDEX 9 // nfi_var - -#define DIST_INDEX 1 // nfi_var - -#define START_OFFSET_INDEX 1 // nfi_var - -#define MAX_OPTS_INDEX 10 // nfi_var - - -#define LZMA_ID_ERROR 98 // nfi_var -#define LZMA_PRESET_ERROR 99 // nfi_var +#define FORMAT_AUTO_INDEX 0 +#define FORMAT_XZ_INDEX 1 +#define FORMAT_ALONE_INDEX 2 +#define FORMAT_RAW_INDEX 3 + +#define CHECK_NONE_INDEX 0 +#define CHECK_CRC32_INDEX 1 +#define CHECK_CRC64_INDEX 2 +#define CHECK_SHA256_INDEX 3 +#define CHECK_ID_MAX_INDEX 4 +#define CHECK_UNKNOWN_INDEX 5 + +#define FILTER_LZMA1_INDEX 0 +#define FILTER_LZMA2_INDEX 1 +#define FILTER_DELTA_INDEX 2 +#define FILTER_X86_INDEX 3 +#define FILTER_POWERPC_INDEX 4 +#define FILTER_IA64_INDEX 5 +#define FILTER_ARM_INDEX 6 +#define FILTER_ARMTHUMB_INDEX 7 +#define FILTER_SPARC_INDEX 8 + +#define MF_HC3_INDEX 0 +#define MF_HC4_INDEX 1 +#define MF_BT2_INDEX 2 +#define MF_BT3_INDEX 3 +#define MF_BT4_INDEX 4 + +#define MODE_FAST_INDEX 0 +#define MODE_NORMAL_INDEX 1 + +#define PRESET_DEFAULT_INDEX 0 +#define PRESET_EXTREME_INDEX 1 + + +#define ID_INDEX 0 +#define PRESET_INDEX 1 +#define DICT_SIZE_INDEX 2 +#define LC_INDEX 3 +#define LP_INDEX 4 +#define PB_INDEX 5 +#define MODE_INDEX 6 +#define NICE_LEN_INDEX 7 +#define MF_INDEX 8 +#define DEPTH_INDEX 9 + +#define DIST_INDEX 1 + +#define START_OFFSET_INDEX 1 + +#define MAX_OPTS_INDEX 10 + + +#define LZMA_ID_ERROR 98 +#define LZMA_PRESET_ERROR 99 #ifndef NDEBUG #include @@ -225,8 +225,6 @@ typedef struct double timeElapsed; #endif } lzmast_stream; - -// nfi_function: name('getMarcos') static(true) void get_macros(int* formats, int* checks, uint64_t* filters, int* mfs, int* modes, uint64_t* preset) { formats[FORMAT_AUTO_INDEX] = FORMAT_AUTO; formats[FORMAT_XZ_INDEX] = FORMAT_XZ; @@ -320,8 +318,6 @@ static void lzma_release_buffer(off_heap_buffer *o) { free(o->buf); free(o); } - -// nfi_function: name('createStream') map('lzmast_stream*', 'POINTER') lzmast_stream *lzma_create_lzmast_stream() { lzmast_stream *lzmast = (lzmast_stream *) calloc(1, sizeof(lzmast_stream)); lzmast->alloc.opaque = NULL; @@ -342,8 +338,6 @@ lzmast_stream *lzma_create_lzmast_stream() { LOG_INFO("lzmast_stream(%p)\n", lzmast); return lzmast; } - -// nfi_function: name('getTimeElapsed') map('lzmast_stream*', 'POINTER') static(true) double lzma_get_timeElapsed(lzmast_stream* lzmast) { #ifdef BENCHMARK double t = lzmast->timeElapsed; @@ -376,8 +370,6 @@ free_filter_chain(lzmast_stream *lzmast) { lzmast->filters = NULL; } } - -// nfi_function: name('deallocateStream') map('lzmast_stream*', 'POINTER') void lzma_free_stream(lzmast_stream* lzmast) { if (!lzmast) { return; @@ -394,38 +386,24 @@ void lzma_free_stream(lzmast_stream* lzmast) { LOG_INFO("free lzmast_stream(%p)\n", lzmast); free(lzmast); } - -// nfi_function: name('gcReleaseHelper') map('lzmast_stream*', 'POINTER') release(true) void lzma_gc_helper(lzmast_stream* lzmast) { lzma_free_stream(lzmast); } - -// nfi_function: name('getNextInIndex') map('lzmast_stream*', 'POINTER') ssize_t lzma_get_next_in_index(lzmast_stream *lzmast) { return lzmast->next_in_index; } - -// nfi_function: name('getLzsAvailIn') map('lzmast_stream*', 'POINTER') size_t lzma_get_lzs_avail_in(lzmast_stream *lzmast) { return lzmast->lzs.avail_in; } - -// nfi_function: name('getLzsAvailOut') map('lzmast_stream*', 'POINTER') size_t lzma_get_lzs_avail_out(lzmast_stream *lzmast) { return lzmast->lzs.avail_out; } - -// nfi_function: name('getLzsCheck') map('lzmast_stream*', 'POINTER') int lzma_lzma_get_check(lzmast_stream *lzmast) { return lzmast->check; } - -// nfi_function: name('setLzsAvailIn') map('lzmast_stream*', 'POINTER') void lzma_set_lzs_avail_in(lzmast_stream *lzmast, size_t v) { lzmast->lzs.avail_in = v; } - -// nfi_function: name('getOutputBufferSize') map('lzmast_stream*', 'POINTER') size_t lzma_get_output_buffer_size(lzmast_stream *lzmast) { LOG_INFO("lzma_get_output_buffer_size(%p)\n", lzmast); size_t size = lzmast->output_size; @@ -442,8 +420,6 @@ static void clear_output(lzmast_stream *lzmast) { lzmast->output->size = 0; lzmast->output_size = 0; } - -// nfi_function: name('getOutputBuffer') map('lzmast_stream*', 'POINTER') void lzma_get_output_buffer(lzmast_stream *lzmast, Byte *dest) { LOG_INFO("lzma_get_off_heap_buffer(%p)\n", lzmast); off_heap_buffer *buffer = lzmast->output; @@ -509,8 +485,6 @@ grow_buffer(lzmast_stream *lzmast, ssize_t max_length) { return -1; } } - -// nfi_function: name('checkIsSupported') int lzma_lzma_check_is_supported(int check_id) { return lzma_check_is_supported(check_id); } @@ -518,8 +492,6 @@ int lzma_lzma_check_is_supported(int check_id) { /************************************************ * Prepare Filters * ************************************************/ - -// nfi_function: name('setFilterSpecLZMA') map('lzmast_stream*', 'POINTER') int lzma_set_filter_spec_lzma(lzmast_stream *lzmast, int fidx, int64_t* opts) { initFilters(lzmast); lzma_options_lzma *options; @@ -568,8 +540,6 @@ int lzma_set_filter_spec_lzma(lzmast_stream *lzmast, int fidx, int64_t* opts) { lzmast->filters[fidx].options = options; return LZMA_OK; } - -// nfi_function: name('setFilterSpecDelta') map('lzmast_stream*', 'POINTER') int lzma_set_filter_spec_delta(lzmast_stream *lzmast, int fidx, int64_t* opts) { initFilters(lzmast); lzma_options_delta *options; @@ -586,8 +556,6 @@ int lzma_set_filter_spec_delta(lzmast_stream *lzmast, int fidx, int64_t* opts) { lzmast->filters[fidx].options = options; return LZMA_OK; } - -// nfi_function: name('setFilterSpecBCJ') map('lzmast_stream*', 'POINTER') int lzma_set_filter_spec_bcj(lzmast_stream *lzmast, int fidx, int64_t* opts) { initFilters(lzmast); lzma_options_bcj *options; @@ -602,8 +570,6 @@ int lzma_set_filter_spec_bcj(lzmast_stream *lzmast, int fidx, int64_t* opts) { lzmast->filters[fidx].options = options; return LZMA_OK; } - -// nfi_function: name('encodeFilter') map('lzmast_stream*', 'POINTER') int lzma_encode_filter_spec(lzmast_stream *lzmast, int64_t* opts) { lzma_ret lzret = LZMA_PROG_ERROR; uint32_t encoded_size; @@ -653,8 +619,6 @@ int lzma_encode_filter_spec(lzmast_stream *lzmast, int64_t* opts) { lzmast->output_size = encoded_size; return LZMA_OK; } - -// nfi_function: name('decodeFilter') map('lzmast_stream*', 'POINTER') int lzma_decode_filter_spec(int64_t filter_id, Byte* encoded_props, int len, int64_t *opts) { LOG_INFO("lzma_decode_filter_spec(filter_id=%ld, encoded_props=%p, len=%d, opts=%p)\n", filter_id, encoded_props, len, opts); @@ -719,8 +683,6 @@ int lzma_decode_filter_spec(int64_t filter_id, Byte* encoded_props, int len, int /************************************************ * Compress Object * ************************************************/ - -// nfi_function: name('lzmaEasyEncoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_easy_encoder(lzmast_stream *lzmast, uint32_t preset, int check) { LOG_INFO("lzma_lzma_easy_encoder(%p, %d, %d)[lzmast->lzs (%p)]\n", lzmast, preset, check, lzmast->lzs); lzma_ret lzret = lzma_easy_encoder(&lzmast->lzs, preset, check); @@ -730,8 +692,6 @@ int lzma_lzma_easy_encoder(lzmast_stream *lzmast, uint32_t preset, int check) { lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaStreamEncoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_stream_encoder(lzmast_stream *lzmast, int check) { LOG_INFO("lzma_lzma_stream_encoder(%p, %d)\n", lzmast, check); lzma_ret lzret = lzma_stream_encoder(&lzmast->lzs, lzmast->filters, check); @@ -741,8 +701,6 @@ int lzma_lzma_stream_encoder(lzmast_stream *lzmast, int check) { lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaAloneEncoderPreset') map('lzmast_stream*', 'POINTER') int lzma_lzma_alone_encoder_preset(lzmast_stream *lzmast, uint32_t preset) { LOG_INFO("lzma_lzma_alone_encoder_preset(%p, %d)\n", lzmast, preset); lzma_options_lzma options; @@ -756,8 +714,6 @@ int lzma_lzma_alone_encoder_preset(lzmast_stream *lzmast, uint32_t preset) { lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaAloneEncoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_alone_encoder(lzmast_stream *lzmast) { lzma_ret lzret = lzma_alone_encoder(&lzmast->lzs, lzmast->filters[0].options); free_filter_chain(lzmast); @@ -767,8 +723,6 @@ int lzma_lzma_alone_encoder(lzmast_stream *lzmast) { lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaRawEncoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_raw_encoder(lzmast_stream *lzmast) { lzma_ret lzret = lzma_raw_encoder(&lzmast->lzs, lzmast->filters); free_filter_chain(lzmast); @@ -778,8 +732,6 @@ int lzma_lzma_raw_encoder(lzmast_stream *lzmast) { lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('compress') map('lzmast_stream*', 'POINTER') int lzma_compress(lzmast_stream *lzmast, Byte *data, size_t len, int iaction, ssize_t bufsize) { lzma_action action = (lzma_action) iaction; LOG_INFO("lzma_compress(%p, %p, %zd, %d, %zd)\n", lzmast, data, len, action, bufsize); @@ -828,8 +780,6 @@ int lzma_compress(lzmast_stream *lzmast, Byte *data, size_t len, int iaction, ss /************************************************ * Decompress Object * ************************************************/ - -// nfi_function: name('lzmaRawDecoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_raw_decoder(lzmast_stream *lzmast) { lzmast->check = LZMA_CHECK_NONE; lzma_ret lzret = lzma_raw_decoder(&lzmast->lzs, lzmast->filters); @@ -840,8 +790,6 @@ int lzma_lzma_raw_decoder(lzmast_stream *lzmast) { lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaAutoDecoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_auto_decoder(lzmast_stream *lzmast, uint64_t memlimit, uint32_t decoder_flags) { lzma_ret lzret = lzma_auto_decoder(&lzmast->lzs, memlimit, decoder_flags); if (!isOK(lzret)) { @@ -850,8 +798,6 @@ int lzma_lzma_auto_decoder(lzmast_stream *lzmast, uint64_t memlimit, uint32_t de lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaStreamDecoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_stream_decoder(lzmast_stream *lzmast, uint64_t memlimit, uint32_t decoder_flags) { lzma_ret lzret = lzma_stream_decoder(&lzmast->lzs, memlimit, decoder_flags); if (!isOK(lzret)) { @@ -860,8 +806,6 @@ int lzma_lzma_stream_decoder(lzmast_stream *lzmast, uint64_t memlimit, uint32_t lzmast->lzs_type = INITIALIZED; return LZMA_OK; } - -// nfi_function: name('lzmaAloneDecoder') map('lzmast_stream*', 'POINTER') int lzma_lzma_alone_decoder(lzmast_stream *lzmast, uint64_t memlimit) { LOG_INFO("lzma_decompress(%p, %ld)\n", lzmast, memlimit); lzmast->check = LZMA_CHECK_NONE; @@ -877,8 +821,6 @@ int lzma_lzma_alone_decoder(lzmast_stream *lzmast, uint64_t memlimit) { buffer is allocated dynamically and returned. At most max_length bytes are returned, so some of the input may not be consumed. d->lzs.next_in and d->lzs.avail_in are updated to reflect the consumed input. */ - -// nfi_function: name('decompress') map('lzmast_stream*', 'POINTER') int lzma_decompress(lzmast_stream *lzmast, Byte *input_buffer, ssize_t offset, ssize_t max_length, From 6e7006af9e753abb11dedc5ce49ba64fa0820b0c Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Fri, 29 May 2026 15:00:01 +0200 Subject: [PATCH 5/8] Rename native compression support classes --- .../modules/GraalPythonModuleBuiltins.java | 2 +- .../modules/bz2/BZ2CompressorBuiltins.java | 4 +- .../modules/bz2/BZ2DecompressorBuiltins.java | 4 +- .../builtins/modules/bz2/BZ2Object.java | 12 ++-- .../python/builtins/modules/bz2/Bz2Nodes.java | 10 +-- .../modules/lzma/LZMACompressorBuiltins.java | 2 +- .../lzma/LZMADecompressorBuiltins.java | 2 +- .../modules/lzma/LZMAModuleBuiltins.java | 62 ++++++++--------- .../builtins/modules/lzma/LZMANodes.java | 38 +++++------ .../builtins/modules/lzma/LZMAObject.java | 22 +++--- .../modules/zlib/NativeZlibCompObject.java | 12 ++-- .../modules/zlib/ZLibModuleBuiltins.java | 8 +-- .../modules/zlib/ZlibCompressBuiltins.java | 6 +- .../modules/zlib/ZlibDecompressBuiltins.java | 8 +-- .../zlib/ZlibDecompressorBuiltins.java | 6 +- .../modules/zlib/ZlibDecompressorObject.java | 6 +- .../builtins/modules/zlib/ZlibNodes.java | 68 +++++++++---------- ...IBz2Support.java => NativeBz2Support.java} | 18 ++--- ...ZMASupport.java => NativeLZMASupport.java} | 18 ++--- ...libSupport.java => NativeZlibSupport.java} | 18 ++--- .../graal/python/runtime/PythonContext.java | 18 ++--- .../graal/python/runtime/object/PFactory.java | 10 +-- graalpython/python-libbz2/src/bz2.c | 2 +- graalpython/python-liblzma/src/lzma.c | 2 +- graalpython/python-libzsupport/src/zlib.c | 2 +- 25 files changed, 180 insertions(+), 180 deletions(-) rename graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/{NFIBz2Support.java => NativeBz2Support.java} (94%) rename graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/{NFILZMASupport.java => NativeLZMASupport.java} (96%) rename graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/{NFIZlibSupport.java => NativeZlibSupport.java} (96%) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java index 688202ff33..738dfd95a9 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java @@ -954,7 +954,7 @@ TruffleString posixModuleBackend( public abstract static class ZlibModuleBackendNode extends PythonBuiltinNode { @Specialization TruffleString zlibModuleBackend() { - return getContext().getNFIZlibSupport().isAvailable() ? T_NATIVE : T_JAVA; + return getContext().getNativeZlibSupport().isAvailable() ? T_NATIVE : T_JAVA; } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java index ffba3fdbc5..7f5a86cf00 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java @@ -73,7 +73,7 @@ import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.GilNode; -import com.oracle.graal.python.runtime.NFIBz2Support; +import com.oracle.graal.python.runtime.NativeBz2Support; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.truffle.api.dsl.Bind; @@ -126,7 +126,7 @@ PNone init(BZ2Object.BZ2Compressor self, int compresslevel, @Cached PRaiseNode raiseNode) { gil.release(true); try { - NFIBz2Support bz2Support = PythonContext.get(this).getNativeBz2Support(); + NativeBz2Support bz2Support = PythonContext.get(this).getNativeBz2Support(); long bzst = bz2Support.createStream(); int err = bz2Support.compressInit(bzst, compresslevel); if (err != BZ_OK) { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java index 3f7025e0e4..999ab0d369 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java @@ -68,7 +68,7 @@ import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; -import com.oracle.graal.python.runtime.NFIBz2Support; +import com.oracle.graal.python.runtime.NativeBz2Support; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.truffle.api.dsl.Bind; @@ -111,7 +111,7 @@ public abstract static class InitNode extends PythonUnaryBuiltinNode { static PNone init(BZ2Object.BZ2Decompressor self, @Bind Node inliningTarget, @Cached PRaiseNode raiseNode) { - NFIBz2Support bz2Support = PythonContext.get(inliningTarget).getNativeBz2Support(); + NativeBz2Support bz2Support = PythonContext.get(inliningTarget).getNativeBz2Support(); long bzst = bz2Support.createStream(); int err = bz2Support.decompressInit(bzst); if (err != BZ_OK) { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java index f849870276..6e90f4c6df 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java @@ -44,7 +44,7 @@ import com.oracle.graal.python.builtins.objects.ints.PInt; import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject; -import com.oracle.graal.python.runtime.NFIBz2Support; +import com.oracle.graal.python.runtime.NativeBz2Support; import com.oracle.graal.python.util.OverflowException; import com.oracle.graal.python.util.PythonUtils; import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; @@ -52,26 +52,26 @@ public abstract class BZ2Object extends PythonBuiltinObject { - private NFIBz2Support.Pointer pointer; + private NativeBz2Support.Pointer pointer; BZ2Object(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public final void init(long bzst, NFIBz2Support lib) { - this.pointer = new NFIBz2Support.Pointer(this, bzst, lib); + public final void init(long bzst, NativeBz2Support lib) { + this.pointer = new NativeBz2Support.Pointer(this, bzst, lib); assert !pointer.isReleased(); } public final long getBzs() { - NFIBz2Support.Pointer p = pointer; + NativeBz2Support.Pointer p = pointer; assert p != null && !p.isReleased(); return p.getPointer(); } @TruffleBoundary public final void markReleased() { - NFIBz2Support.Pointer p; + NativeBz2Support.Pointer p; synchronized (this) { p = pointer; pointer = null; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java index 16f288a4bd..392e3b6ca2 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -57,7 +57,7 @@ import com.oracle.graal.python.builtins.objects.ints.PInt; import com.oracle.graal.python.nodes.PRaiseNode; -import com.oracle.graal.python.runtime.NFIBz2Support; +import com.oracle.graal.python.runtime.NativeBz2Support; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.util.OverflowException; import com.oracle.graal.python.util.PythonUtils; @@ -112,7 +112,7 @@ static byte[] nativeCompress(BZ2Object.BZ2Compressor self, PythonContext context @Bind Node inliningTarget, @Cached GetOutputNativeBufferNode getBuffer, @Cached PRaiseNode raiseNode) { - NFIBz2Support bz2Support = context.getNFIBz2Support(); + NativeBz2Support bz2Support = context.getNativeBz2Support(); int err = bz2Support.compress(self.getBzs(), bytes, len, action, INITIAL_BUFFER_SIZE); if (err != BZ_OK) { errorHandling(inliningTarget, err, raiseNode); @@ -233,7 +233,7 @@ static byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, int maxLe @Cached InlinedBranchProfile ofProfile, @Cached PRaiseNode raiseNode) { PythonContext context = PythonContext.get(inliningTarget); - NFIBz2Support bz2Support = context.getNFIBz2Support(); + NativeBz2Support bz2Support = context.getNativeBz2Support(); byte[] in = self.getNextIn(); int offset = self.getNextInIndex(); int err = bz2Support.decompress(self.getBzs(), in, offset, maxLength, INITIAL_BUFFER_SIZE, self.getBzsAvailInReal()); @@ -265,7 +265,7 @@ public abstract static class GetOutputNativeBufferNode extends Node { static byte[] getBuffer(Node inliningTarget, long bzst, PythonContext context, @Cached InlinedBranchProfile ofProfile, @Cached PRaiseNode raiseNode) { - NFIBz2Support bz2Support = context.getNFIBz2Support(); + NativeBz2Support bz2Support = context.getNativeBz2Support(); int size; try { size = PInt.intValueExact(bz2Support.getOutputBufferSize(bzst)); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java index 47147fc2ef..b7b8487192 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMACompressorBuiltins.java @@ -119,7 +119,7 @@ LZMAObject doNew(Object cls, @SuppressWarnings("unused") Object arg, @Cached TypeNodes.GetInstanceShape getInstanceShape) { // data filled in subsequent __init__ call - see LZMACompressorBuiltins.InitNode PythonContext context = getContext(); - return PFactory.createLZMACompressor(cls, getInstanceShape.execute(cls), context.getNFILZMASupport().isAvailable()); + return PFactory.createLZMACompressor(cls, getInstanceShape.execute(cls), context.getNativeLZMASupport().isAvailable()); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java index 482a3f49f8..4b330e66a4 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMADecompressorBuiltins.java @@ -116,7 +116,7 @@ LZMAObject doNew(Object cls, @SuppressWarnings("unused") Object arg, @Cached TypeNodes.GetInstanceShape getInstanceShape) { // data filled in subsequent __init__ call - see LZMADecompressorBuiltins.InitNode PythonContext context = getContext(); - return PFactory.createLZMADecompressor(cls, getInstanceShape.execute(cls), context.getNFILZMASupport().isAvailable()); + return PFactory.createLZMADecompressor(cls, getInstanceShape.execute(cls), context.getNativeLZMASupport().isAvailable()); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java index cd0f0a8278..fd6feb5169 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -41,34 +41,34 @@ package com.oracle.graal.python.builtins.modules.lzma; import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError; -import static com.oracle.graal.python.runtime.NFILZMASupport.CHECK_CRC32_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.CHECK_CRC64_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.CHECK_ID_MAX_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.CHECK_NONE_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.CHECK_SHA256_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.CHECK_UNKNOWN_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_ARMTHUMB_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_ARM_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_DELTA_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_IA64_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_LZMA1_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_LZMA2_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_POWERPC_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_SPARC_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FILTER_X86_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FORMAT_ALONE_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FORMAT_AUTO_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FORMAT_RAW_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.FORMAT_XZ_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MF_BT2_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MF_BT3_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MF_BT4_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MF_HC3_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MF_HC4_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MODE_FAST_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.MODE_NORMAL_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.PRESET_DEFAULT_INDEX; -import static com.oracle.graal.python.runtime.NFILZMASupport.PRESET_EXTREME_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.CHECK_CRC32_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.CHECK_CRC64_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.CHECK_ID_MAX_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.CHECK_NONE_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.CHECK_SHA256_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.CHECK_UNKNOWN_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_ARMTHUMB_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_ARM_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_DELTA_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_IA64_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_LZMA1_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_LZMA2_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_POWERPC_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_SPARC_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FILTER_X86_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FORMAT_ALONE_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FORMAT_AUTO_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FORMAT_RAW_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.FORMAT_XZ_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MF_BT2_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MF_BT3_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MF_BT4_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MF_HC3_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MF_HC4_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MODE_FAST_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MODE_NORMAL_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.PRESET_DEFAULT_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.PRESET_EXTREME_INDEX; import static com.oracle.graal.python.util.PythonUtils.tsLiteral; import java.util.List; @@ -92,7 +92,7 @@ import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode; -import com.oracle.graal.python.runtime.NFILZMASupport; +import com.oracle.graal.python.runtime.NativeLZMASupport; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; import com.oracle.truffle.api.dsl.Bind; @@ -181,7 +181,7 @@ public void initialize(Python3Core core) { @Override public void postInitialize(Python3Core c) { super.postInitialize(c); - NFILZMASupport lzmaSupport = c.getContext().getNFILZMASupport(); + NativeLZMASupport lzmaSupport = c.getContext().getNativeLZMASupport(); PythonModule lzmaModule = c.lookupBuiltinModule(T__LZMA); int[] formats = new int[4]; int[] checks = new int[6]; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java index 39d69cda40..04bc5f667e 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java @@ -65,9 +65,9 @@ import static com.oracle.graal.python.nodes.ErrorMessages.INVALID_FILTER_CHAIN_FOR_FORMAT; import static com.oracle.graal.python.nodes.ErrorMessages.VALUE_TOO_LARGE_TO_FIT_INTO_INDEX; import static com.oracle.graal.python.nodes.StringLiterals.T_ID; -import static com.oracle.graal.python.runtime.NFILZMASupport.LZMA_ID_ERROR; -import static com.oracle.graal.python.runtime.NFILZMASupport.LZMA_PRESET_ERROR; -import static com.oracle.graal.python.runtime.NFILZMASupport.MAX_OPTS_INDEX; +import static com.oracle.graal.python.runtime.NativeLZMASupport.LZMA_ID_ERROR; +import static com.oracle.graal.python.runtime.NativeLZMASupport.LZMA_PRESET_ERROR; +import static com.oracle.graal.python.runtime.NativeLZMASupport.MAX_OPTS_INDEX; import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; import static com.oracle.graal.python.util.PythonUtils.tsLiteral; @@ -117,7 +117,7 @@ import com.oracle.graal.python.nodes.util.CannotCastException; import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode; import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode; -import com.oracle.graal.python.runtime.NFILZMASupport; +import com.oracle.graal.python.runtime.NativeLZMASupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.exception.PythonErrorType; import com.oracle.graal.python.util.OverflowException; @@ -503,7 +503,7 @@ static void parseFilterChainSpec(VirtualFrame frame, long lzmast, PythonContext private static void setFilterOptions(Node inliningTarget, long lzmast, long[] filter, int fidx, PythonContext context, PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = context.getNativeLZMASupport(); int err; long id = filter[0]; switch (LZMAFilter.from(id)) { @@ -683,7 +683,7 @@ static void xz(LZMACompressor.Native self, @SuppressWarnings("unused") int forma @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int lzret = lzmaSupport.lzmaEasyEncoder(lzmast, preset, self.getCheck()); @@ -699,7 +699,7 @@ static void xz(VirtualFrame frame, LZMACompressor.Native self, @SuppressWarnings @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, ctxt, filters); @@ -715,7 +715,7 @@ static void alone(LZMACompressor.Native self, int format, long preset, PNone fil @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int lzret = lzmaSupport.lzmaAloneEncoderPreset(lzmast, preset); @@ -732,7 +732,7 @@ static void alone(VirtualFrame frame, LZMACompressor.Native self, int format, lo @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, ctxt, filters); @@ -750,7 +750,7 @@ static void raw(VirtualFrame frame, LZMACompressor.Native self, int format, long @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, ctxt, filters); @@ -857,7 +857,7 @@ static byte[] nativeCompress(Node inliningTarget, LZMACompressor.Native self, Py @Cached GetOutputNativeBufferNode getBuffer, @Cached InlinedConditionProfile errProfile, @Exclusive @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = context.getNativeLZMASupport(); int err = lzmaSupport.compress(self.getLzs(), bytes, len, action, INITIAL_BUFFER_SIZE); if (errProfile.profile(inliningTarget, err != LZMA_OK)) { errorHandling(inliningTarget, err, raiseNode); @@ -908,7 +908,7 @@ static void auto(LZMADecompressor.Native self, @SuppressWarnings("unused") int f @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int decoderFlags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; @@ -923,7 +923,7 @@ static void xz(LZMADecompressor.Native self, @SuppressWarnings("unused") int for @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int decoderFlags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; @@ -938,7 +938,7 @@ static void alone(LZMADecompressor.Native self, @SuppressWarnings("unused") int @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile errProfile, @Shared @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = PythonContext.get(inliningTarget).getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); int lzret = lzmaSupport.lzmaAloneDecoder(lzmast, memlimit); @@ -968,7 +968,7 @@ static void rawNative(VirtualFrame frame, Node inliningTarget, LZMADecompressor. @Cached InlinedConditionProfile errProfile, @Cached PRaiseNode raiseNode) { PythonContext context = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = context.getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); self.init(lzmast, lzmaSupport); filterChain.execute(frame, lzmast, context, filters); @@ -1085,7 +1085,7 @@ static byte[] nativeInternalDecompress(Node inliningTarget, LZMADecompressor.Nat @Exclusive @Cached PRaiseNode lazyRaiseNode, @Cached InlinedConditionProfile errProfile) { PythonContext context = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = context.getNativeLZMASupport(); byte[] inGuest = self.getNextIn(); int offset = self.getNextInIndex(); int err = lzmaSupport.decompress(self.getLzs(), inGuest, offset, maxLength, INITIAL_BUFFER_SIZE, self.getLzsAvailIn()); @@ -1196,7 +1196,7 @@ public abstract static class GetOutputNativeBufferNode extends Node { @Specialization static byte[] getBuffer(Node inliningTarget, long lzmast, PythonContext context, @Cached PRaiseNode raiseNode) { - NFILZMASupport lzmaSupport = context.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = context.getNativeLZMASupport(); int size; try { size = PInt.intValueExact(lzmaSupport.getOutputBufferSize(lzmast)); @@ -1259,7 +1259,7 @@ static byte[] encodeNative(VirtualFrame frame, Object filter, @Cached InlinedConditionProfile errProfile, @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); long lzmast = lzmaSupport.createStream(); long[] opts = filterConverter.execute(frame, filter); int lzret = lzmaSupport.encodeFilter(lzmast, opts); @@ -1300,7 +1300,7 @@ static void decodeNative(VirtualFrame frame, long id, byte[] encoded, PDict dict @Cached InlinedConditionProfile errProfile, @Cached PRaiseNode raiseNode) { PythonContext ctxt = PythonContext.get(inliningTarget); - NFILZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); + NativeLZMASupport lzmaSupport = ctxt.getNativeLZMASupport(); long[] opts = new long[MAX_OPTS_INDEX]; int len = encoded.length; int lzret = lzmaSupport.decodeFilter(id, encoded, len, opts); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java index 3aa9334fbe..6b98ac4554 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAObject.java @@ -63,7 +63,7 @@ import com.oracle.graal.python.builtins.objects.ints.PInt; import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject; -import com.oracle.graal.python.runtime.NFILZMASupport; +import com.oracle.graal.python.runtime.NativeLZMASupport; import com.oracle.graal.python.util.OverflowException; import com.oracle.graal.python.util.PythonUtils; import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; @@ -153,26 +153,26 @@ public void resetBuffer() { public static final class Native extends LZMACompressor { - private NFILZMASupport.Pointer pointer; + private NativeLZMASupport.Pointer pointer; public Native(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public void init(long lzmast, NFILZMASupport lib) { - this.pointer = new NFILZMASupport.Pointer(this, lzmast, lib); + public void init(long lzmast, NativeLZMASupport lib) { + this.pointer = new NativeLZMASupport.Pointer(this, lzmast, lib); assert !pointer.isReleased(); } public long getLzs() { - NFILZMASupport.Pointer p = pointer; + NativeLZMASupport.Pointer p = pointer; assert p != null && !p.isReleased(); return p.getPointer(); } @TruffleBoundary public void markReleased() { - NFILZMASupport.Pointer p; + NativeLZMASupport.Pointer p; synchronized (this) { p = pointer; pointer = null; @@ -460,26 +460,26 @@ public void createLZMA() throws IOException { public static final class Native extends LZMADecompressor { - private NFILZMASupport.Pointer pointer; + private NativeLZMASupport.Pointer pointer; public Native(Object cls, Shape instanceShape) { super(cls, instanceShape); } - public void init(long lzmast, NFILZMASupport lib) { - this.pointer = new NFILZMASupport.Pointer(this, lzmast, lib); + public void init(long lzmast, NativeLZMASupport lib) { + this.pointer = new NativeLZMASupport.Pointer(this, lzmast, lib); assert !pointer.isReleased(); } public long getLzs() { - NFILZMASupport.Pointer p = pointer; + NativeLZMASupport.Pointer p = pointer; assert p != null && !p.isReleased(); return p.getPointer(); } @TruffleBoundary public void markReleased() { - NFILZMASupport.Pointer p; + NativeLZMASupport.Pointer p; synchronized (this) { p = pointer; pointer = null; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java index fa7c098f5a..dfbcdbd308 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java @@ -40,28 +40,28 @@ */ package com.oracle.graal.python.builtins.modules.zlib; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.object.Shape; public final class NativeZlibCompObject extends ZLibCompObject { - private NFIZlibSupport.Pointer pointer; + private NativeZlibSupport.Pointer pointer; byte[] lastInput; - public NativeZlibCompObject(Object cls, Shape instanceShape, long zst, NFIZlibSupport zlibSupport) { + public NativeZlibCompObject(Object cls, Shape instanceShape, long zst, NativeZlibSupport zlibSupport) { super(cls, instanceShape); - this.pointer = new NFIZlibSupport.Pointer(this, zst, zlibSupport); + this.pointer = new NativeZlibSupport.Pointer(this, zst, zlibSupport); } public long getZst() { - NFIZlibSupport.Pointer p = pointer; + NativeZlibSupport.Pointer p = pointer; assert p != null && !p.isReleased(); return p.getPointer(); } @TruffleBoundary public void markReleased() { - NFIZlibSupport.Pointer p; + NativeZlibSupport.Pointer p; synchronized (this) { if (!isInitialized) { assert pointer == null || pointer.isReleased(); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java index b732e55d70..55dc6d940a 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java @@ -83,7 +83,7 @@ import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -182,7 +182,7 @@ public void initialize(Python3Core core) { @Override public void postInitialize(Python3Core core) { super.postInitialize(core); - NFIZlibSupport zlibSupport = core.getContext().getNativeZlibSupport(); + NativeZlibSupport zlibSupport = core.getContext().getNativeZlibSupport(); PythonModule zlibModule = core.lookupBuiltinModule(T_ZLIB); // isAvailable() checked already if native access is allowed TruffleString ver = T_JDK_ZLIB_VERSION; @@ -619,7 +619,7 @@ static Object doNative(int level, int method, int wbits, int memLevel, int strat @Bind Node inliningTarget, @Bind PythonLanguage language, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = PythonContext.get(inliningTarget).getNativeZlibSupport(); + NativeZlibSupport zlibSupport = PythonContext.get(inliningTarget).getNativeZlibSupport(); long zst = zlibSupport.createCompObject(); int err; @@ -689,7 +689,7 @@ static Object doNative(int wbits, byte[] zdict, @Bind Node inliningTarget, @Bind PythonContext context, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); long zst = zlibSupport.createCompObject(); int err; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java index 3e761da6c3..cea367d152 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java @@ -71,7 +71,7 @@ import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -157,7 +157,7 @@ static Object doNative(Node inliningTarget, NativeZlibCompObject self, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); long zstNewCopy = zlibSupport.createCompObject(); int err = zlibSupport.compressObjCopy(self.getZst(), zstNewCopy); if (err != Z_OK) { @@ -242,7 +242,7 @@ static PBytes doit(NativeZlibCompObject self, int mode, synchronized (self) { assert self.isInitialized(); PythonContext context = PythonContext.get(inliningTarget); - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] lastInput; if (self.lastInput == null) { // all previous input data has been processed or nothing has been compressed. diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java index 7a0090a802..9b8311f4df 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java @@ -73,7 +73,7 @@ import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.exception.PException; import com.oracle.graal.python.runtime.object.PFactory; @@ -163,7 +163,7 @@ static Object doNative(Node inliningTarget, NativeZlibCompObject self, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); long zstNewCopy = zlibSupport.createCompObject(); int err = zlibSupport.decompressObjCopy(self.getZst(), zstNewCopy); if (err != Z_OK) { @@ -242,7 +242,7 @@ static PBytes doit(NativeZlibCompObject self, int length, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); int err = zlibSupport.decompressObjFlush(self.getZst(), length); if (err != Z_OK) { errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false); @@ -356,7 +356,7 @@ static boolean doit(NativeZlibCompObject self) { boolean getit(NativeZlibCompObject self) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = PythonContext.get(this).getNativeZlibSupport(); + NativeZlibSupport zlibSupport = PythonContext.get(this).getNativeZlibSupport(); self.setEof(zlibSupport.getEOF(self.getZst()) == 1); return self.isEof(); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java index b842d741ac..d3204ca6f7 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java @@ -70,7 +70,7 @@ import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -125,7 +125,7 @@ static Object doNative(@SuppressWarnings("unused") Object type, int wbits, byte[ @Bind Node inliningTarget, @Bind PythonContext context, @Cached ZlibNodes.ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); long zst = zlibSupport.createCompObject(); int err; @@ -262,7 +262,7 @@ static boolean doit(ZlibDecompressorObject self) { boolean doNative(ZlibDecompressorObject self) { synchronized (self) { assert self.isInitialized(); - NFIZlibSupport zlibSupport = PythonContext.get(this).getNativeZlibSupport(); + NativeZlibSupport zlibSupport = PythonContext.get(this).getNativeZlibSupport(); self.setEof(zlibSupport.getEOF(self.getZst()) == 1); return self.isEof(); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java index 98a5e5784e..2b54e6685e 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorObject.java @@ -42,7 +42,7 @@ import com.oracle.graal.python.builtins.objects.bytes.PBytes; import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.truffle.api.object.Shape; public class ZlibDecompressorObject extends PythonBuiltinObject { @@ -58,7 +58,7 @@ public class ZlibDecompressorObject extends PythonBuiltinObject { private boolean needsInput; // native - private ZlibDecompressorObject(Object cls, Shape instanceShape, long zst, NFIZlibSupport zlibSupport) { + private ZlibDecompressorObject(Object cls, Shape instanceShape, long zst, NativeZlibSupport zlibSupport) { super(cls, instanceShape); this.compObject = new NativeZlibCompObject(cls, instanceShape, zst, zlibSupport); this.availInReal = 0; @@ -131,7 +131,7 @@ public boolean isNativeDecompressor() { return compObject instanceof NativeZlibCompObject; } - public static ZlibDecompressorObject createNative(Object cls, Shape instanceShape, long zst, NFIZlibSupport zlibSupport) { + public static ZlibDecompressorObject createNative(Object cls, Shape instanceShape, long zst, NativeZlibSupport zlibSupport) { return new ZlibDecompressorObject(cls, instanceShape, zst, zlibSupport); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java index 942026545f..549dc7a9d5 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java @@ -58,9 +58,9 @@ import static com.oracle.graal.python.nodes.ErrorMessages.WHILE_PREPARING_TO_S_DATA; import static com.oracle.graal.python.nodes.ErrorMessages.WHILE_SETTING_ZDICT; import static com.oracle.graal.python.nodes.ErrorMessages.WHILE_S_DATA; -import static com.oracle.graal.python.runtime.NFIZlibSupport.OUTPUT_OPTION; -import static com.oracle.graal.python.runtime.NFIZlibSupport.UNCONSUMED_TAIL_OPTION; -import static com.oracle.graal.python.runtime.NFIZlibSupport.UNUSED_DATA_OPTION; +import static com.oracle.graal.python.runtime.NativeZlibSupport.OUTPUT_OPTION; +import static com.oracle.graal.python.runtime.NativeZlibSupport.UNCONSUMED_TAIL_OPTION; +import static com.oracle.graal.python.runtime.NativeZlibSupport.UNUSED_DATA_OPTION; import static com.oracle.graal.python.runtime.exception.PythonErrorType.MemoryError; import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError; import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError; @@ -71,7 +71,7 @@ import com.oracle.graal.python.nodes.ErrorMessages; import com.oracle.graal.python.nodes.PNodeWithContext; import com.oracle.graal.python.nodes.PRaiseNode; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.object.PFactory; import com.oracle.graal.python.util.PythonUtils; @@ -101,7 +101,7 @@ public class ZlibNodes { public static final int Z_BUF_ERROR = -5; public static final int Z_VERSION_ERROR = -6; - protected static void deallocateStream(long zst, NFIZlibSupport zlibSupport, boolean deallocate) { + protected static void deallocateStream(long zst, NativeZlibSupport zlibSupport, boolean deallocate) { if (deallocate) { zlibSupport.deallocateStream(zst); } @@ -117,7 +117,7 @@ public abstract static class ZlibNativeCompressObj extends PNodeWithContext { static byte[] nativeCompress(Node inliningTarget, NativeZlibCompObject self, PythonContext context, byte[] bytes, int len, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); self.lastInput = bytes; int err = zlibSupport.compressObj(self.getZst(), self.lastInput, len, DEF_BUF_SIZE); if (err != Z_OK) { @@ -139,7 +139,7 @@ static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int lev @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { PythonContext context = PythonContext.get(inliningTarget); - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] in = bytes; long zst = zlibSupport.createStream(); int err = zlibSupport.deflateOffHeap(zst, in, len, DEF_BUF_SIZE, level, wbits); @@ -162,7 +162,7 @@ public abstract static class ZlibNativeDecompressObj extends PNodeWithContext { static byte[] nativeDecompress(Node inliningTarget, NativeZlibCompObject self, PythonContext context, byte[] bytes, int len, int maxLength, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] in = bytes; int err = zlibSupport.decompressObj(self.getZst(), in, len, DEF_BUF_SIZE, maxLength); if (err != Z_OK) { @@ -183,7 +183,7 @@ public abstract static class ZlibNativeDecompressor extends PNodeWithContext { static byte[] nativeDecompressBuf(Node inliningTarget, ZlibDecompressorObject self, PythonContext context, byte[] bytes, int len, int maxLength, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); byte[] in = bytes; int ret = zlibSupport.decompressor(self.getZst(), in, len, maxLength); if (ret < 0) { @@ -204,7 +204,7 @@ public abstract static class ZlibNativeDecompress extends PNodeWithContext { static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int wbits, int bufsize, PythonContext context, @Cached GetNativeBufferNode getBuffer, @Cached ZlibNativeErrorHandling errorHandling) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); long zst = zlibSupport.createStream(); byte[] in = bytes; int err = zlibSupport.inflateOffHeap(zst, in, len, bufsize, wbits); @@ -222,10 +222,10 @@ static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int wbi @GenerateCached(false) public abstract static class ZlibNativeErrorHandling extends Node { - public abstract void execute(Node inliningTarget, long zst, int err, NFIZlibSupport zlibSupport, boolean deallocate); + public abstract void execute(Node inliningTarget, long zst, int err, NativeZlibSupport zlibSupport, boolean deallocate); @Specialization - static void doError(long zst, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void doError(long zst, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Cached(inline = false) ZlibFunctionNativeErrorHandling errorHandling) { errorHandling.execute(zst, zlibSupport.getErrorFunction(zst), err, zlibSupport, deallocate); } @@ -236,11 +236,11 @@ static void doError(long zst, int err, NFIZlibSupport zlibSupport, boolean deall @GenerateInline(false) public abstract static class ZlibNativeErrorMsg extends Node { - public abstract void execute(long zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate); + public abstract void execute(long zst, int err, TruffleString msg, NativeZlibSupport zlibSupport, boolean deallocate); @SuppressWarnings("unused") @Specialization(guards = "err == Z_VERSION_ERROR") - static void doVersionError(long zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate, + static void doVersionError(long zst, int err, TruffleString msg, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise) { /* @@ -252,7 +252,7 @@ static void doVersionError(long zst, int err, TruffleString msg, NFIZlibSupport } @Specialization(guards = "err != Z_VERSION_ERROR") - static void doError(long zst, int err, TruffleString msg, NFIZlibSupport zlibSupport, boolean deallocate, + static void doError(long zst, int err, TruffleString msg, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise) { TruffleString zmsg = null; @@ -280,15 +280,15 @@ static void doError(long zst, int err, TruffleString msg, NFIZlibSupport zlibSup } } - @ImportStatic({NFIZlibSupport.class, ZLibModuleBuiltins.class}) + @ImportStatic({NativeZlibSupport.class, ZLibModuleBuiltins.class}) @GenerateUncached @GenerateInline(false) public abstract static class ZlibFunctionNativeErrorHandling extends Node { - public abstract void execute(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate); + public abstract void execute(long zst, int function, int err, NativeZlibSupport zlibSupport, boolean deallocate); @Specialization(guards = "function == DEFLATE_INIT_ERROR") - static void deflateInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateInitError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @@ -305,7 +305,7 @@ static void deflateInitError(long zst, @SuppressWarnings("unused") int function, } @Specialization(guards = "function == DEFLATE_OBJ_ERROR") - static void deflateObjInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateObjInitError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @@ -322,7 +322,7 @@ static void deflateObjInitError(long zst, @SuppressWarnings("unused") int functi } @Specialization(guards = "function == DEFLATE_COPY_ERROR") - static void deflateCopyError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateCopyError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @@ -339,7 +339,7 @@ static void deflateCopyError(long zst, @SuppressWarnings("unused") int function, } @Specialization(guards = "function == INFLATE_COPY_ERROR") - static void inflateCopyError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateCopyError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @@ -357,7 +357,7 @@ static void inflateCopyError(long zst, @SuppressWarnings("unused") int function, @SuppressWarnings("unused") @Specialization(guards = "function == DEFLATE_DICT_ERROR") - static void deflateDictError(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateDictError(long zst, int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise) { if (err == Z_STREAM_ERROR) { @@ -369,7 +369,7 @@ static void deflateDictError(long zst, int function, int err, NFIZlibSupport zli } @Specialization(guards = "function == INFLATE_INIT_ERROR") - static void inflateInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateInitError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @@ -383,7 +383,7 @@ static void inflateInitError(long zst, @SuppressWarnings("unused") int function, } @Specialization(guards = "function == INFLATE_OBJ_ERROR") - static void inflateObjInitError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateObjInitError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @@ -400,54 +400,54 @@ static void inflateObjInitError(long zst, @SuppressWarnings("unused") int functi } @Specialization(guards = "function == INFLATE_DICT_ERROR") - static void inflateDictError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateDictError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError) { zlibError.execute(zst, err, WHILE_SETTING_ZDICT, zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_END_ERROR") - static void deflateEndError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateEndError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_FINISHING_S, "compression"), zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_END_ERROR") - static void inflateEndError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateEndError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_FINISHING_S, "decompression"), zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_ERROR") - static void deflateError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_S_DATA, "compressing"), zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_ERROR") - static void inflateError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError, @Shared("format") @Cached SimpleTruffleStringFormatNode formatNode) { zlibError.execute(zst, err, formatNode.format(WHILE_S_DATA, "decompressing"), zlibSupport, deallocate); } @Specialization(guards = "function == DEFLATE_FLUSH_ERROR") - static void deflateFlushError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void deflateFlushError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError) { zlibError.execute(zst, err, WHILE_FLUSHING, zlibSupport, deallocate); } @Specialization(guards = "function == INFLATE_FLUSH_ERROR") - static void inflateFlushError(long zst, @SuppressWarnings("unused") int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void inflateFlushError(long zst, @SuppressWarnings("unused") int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Shared("err") @Cached ZlibNativeErrorMsg zlibError) { zlibError.execute(zst, err, WHILE_FLUSHING, zlibSupport, deallocate); } @SuppressWarnings("unused") @Specialization(guards = "function == MEMORY_ERROR") - static void memError(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate, + static void memError(long zst, int function, int err, NativeZlibSupport zlibSupport, boolean deallocate, @Bind Node inliningTarget, @Shared("r") @Cached PRaiseNode raise) { deallocateStream(zst, zlibSupport, deallocate); @@ -456,7 +456,7 @@ static void memError(long zst, int function, int err, NFIZlibSupport zlibSupport @SuppressWarnings("unused") @Fallback - void fallback(long zst, int function, int err, NFIZlibSupport zlibSupport, boolean deallocate) { + void fallback(long zst, int function, int err, NativeZlibSupport zlibSupport, boolean deallocate) { throw PRaiseNode.raiseStatic(this, SystemError, ErrorMessages.UNHANDLED_ERROR); } } @@ -513,7 +513,7 @@ public byte[] getUnconsumedTailBuffer(Node inliningTarget, long zst, PythonConte @Specialization static byte[] getBuffer(long zst, int option, PythonContext context) { - NFIZlibSupport zlibSupport = context.getNativeZlibSupport(); + NativeZlibSupport zlibSupport = context.getNativeZlibSupport(); int size = zlibSupport.getBufferSize(zst, option); if (size == 0) { return PythonUtils.EMPTY_BYTE_ARRAY; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeBz2Support.java similarity index 94% rename from graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java rename to graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeBz2Support.java index 627410b295..b7230b8d3f 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIBz2Support.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeBz2Support.java @@ -53,9 +53,9 @@ import com.oracle.truffle.api.ThreadLocalAction.Access; import com.oracle.truffle.api.TruffleLogger; -public class NFIBz2Support extends NativeCompressionSupport { +public class NativeBz2Support extends NativeCompressionSupport { - private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFIBz2Support.class); + private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NativeBz2Support.class); private static final String SUPPORTING_NATIVE_LIB_NAME = "bz2support"; abstract static class Bz2NativeFunctions { @@ -105,13 +105,13 @@ static NativeLibrary loadNativeLibrary(PythonContext context) { private final Bz2NativeFunctions nativeFunctions; - private NFIBz2Support(PythonContext context) { + private NativeBz2Support(PythonContext context) { super(context); this.nativeFunctions = isAvailable() ? new Bz2NativeFunctionsGen(context) : null; } - public static NFIBz2Support createNative(PythonContext context, String noNativeAccessHelp) { - return new NFIBz2Support(context); + public static NativeBz2Support createNative(PythonContext context, String noNativeAccessHelp) { + return new NativeBz2Support(context); } static class PointerReleaseCallback implements AsyncHandler.AsyncAction { @@ -129,19 +129,19 @@ public void execute(PythonContext context, Access access) { } try { pointer.doRelease(); - LOGGER.finest("NFIBz2Support pointer has been freed"); + LOGGER.finest("NativeBz2Support pointer has been freed"); } catch (Exception e) { - LOGGER.severe("Error while trying to free NFIBz2Support pointer: " + e.getMessage()); + LOGGER.severe("Error while trying to free NativeBz2Support pointer: " + e.getMessage()); } } } public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference { - private final NFIBz2Support lib; + private final NativeBz2Support lib; private final long pointer; - public Pointer(Object referent, long pointer, NFIBz2Support lib) { + public Pointer(Object referent, long pointer, NativeBz2Support lib) { super(referent, lib.pythonContext.getSharedFinalizer()); this.lib = lib; this.pointer = pointer; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLZMASupport.java similarity index 96% rename from graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java rename to graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLZMASupport.java index 24ba142f57..116d90c42f 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFILZMASupport.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLZMASupport.java @@ -53,9 +53,9 @@ import com.oracle.truffle.api.ThreadLocalAction.Access; import com.oracle.truffle.api.TruffleLogger; -public final class NFILZMASupport extends NativeCompressionSupport { +public final class NativeLZMASupport extends NativeCompressionSupport { - private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFILZMASupport.class); + private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NativeLZMASupport.class); private static final String SUPPORTING_NATIVE_LIB_NAME = "lzmasupport"; public static final int FORMAT_AUTO_INDEX = 0; @@ -197,13 +197,13 @@ static NativeLibrary loadNativeLibrary(PythonContext context) { private final LZMANativeFunctions nativeFunctions; - private NFILZMASupport(PythonContext context) { + private NativeLZMASupport(PythonContext context) { super(context); this.nativeFunctions = isAvailable() ? new LZMANativeFunctionsGen(context) : null; } - public static NFILZMASupport createNative(PythonContext context, String noNativeAccessHelp) { - return new NFILZMASupport(context); + public static NativeLZMASupport createNative(PythonContext context, String noNativeAccessHelp) { + return new NativeLZMASupport(context); } static class PointerReleaseCallback implements AsyncHandler.AsyncAction { @@ -221,19 +221,19 @@ public void execute(PythonContext context, Access access) { } try { pointer.doRelease(); - LOGGER.finest("NFILZMASupport pointer has been freed"); + LOGGER.finest("NativeLZMASupport pointer has been freed"); } catch (Exception e) { - LOGGER.severe("Error while trying to free NFILZMASupport pointer: " + e.getMessage()); + LOGGER.severe("Error while trying to free NativeLZMASupport pointer: " + e.getMessage()); } } } public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference { - private final NFILZMASupport lib; + private final NativeLZMASupport lib; private final long pointer; - public Pointer(Object referent, long pointer, NFILZMASupport lib) { + public Pointer(Object referent, long pointer, NativeLZMASupport lib) { super(referent, lib.pythonContext.getSharedFinalizer()); this.lib = lib; this.pointer = pointer; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeZlibSupport.java similarity index 96% rename from graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java rename to graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeZlibSupport.java index 863de1ffef..99eaf06566 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIZlibSupport.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeZlibSupport.java @@ -55,9 +55,9 @@ import com.oracle.truffle.api.TruffleLogger; import com.oracle.truffle.api.strings.TruffleString; -public class NFIZlibSupport extends NativeCompressionSupport { +public class NativeZlibSupport extends NativeCompressionSupport { - private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFIZlibSupport.class); + private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NativeZlibSupport.class); private static final String SUPPORTING_NATIVE_LIB_NAME = "zsupport"; public static final int NO_ERROR = 0; @@ -177,13 +177,13 @@ static NativeLibrary loadNativeLibrary(PythonContext context) { private final ZlibNativeFunctions nativeFunctions; - private NFIZlibSupport(PythonContext context) { + private NativeZlibSupport(PythonContext context) { super(context); this.nativeFunctions = isAvailable() ? new ZlibNativeFunctionsGen(context) : null; } - public static NFIZlibSupport createNative(PythonContext context, String noNativeAccessHelp) { - return new NFIZlibSupport(context); + public static NativeZlibSupport createNative(PythonContext context, String noNativeAccessHelp) { + return new NativeZlibSupport(context); } static class PointerReleaseCallback implements AsyncHandler.AsyncAction { @@ -201,19 +201,19 @@ public void execute(PythonContext context, Access access) { } try { pointer.doRelease(); - LOGGER.finest("NFIZlibSupport pointer has been freed"); + LOGGER.finest("NativeZlibSupport pointer has been freed"); } catch (Exception e) { - LOGGER.severe("Error while trying to free NFIZlibSupport pointer: " + e.getMessage()); + LOGGER.severe("Error while trying to free NativeZlibSupport pointer: " + e.getMessage()); } } } public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference { - private final NFIZlibSupport lib; + private final NativeZlibSupport lib; private final long pointer; - public Pointer(Object referent, long pointer, NFIZlibSupport lib) { + public Pointer(Object referent, long pointer, NativeZlibSupport lib) { super(referent, lib.pythonContext.getSharedFinalizer()); this.lib = lib; this.pointer = pointer; diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java index 4f5fc41bc5..e09fed6aaa 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java @@ -777,9 +777,9 @@ static PythonThreadState getThreadState(Node n) { @CompilationFinal(dimensions = 1) private byte[] hashSecret = new byte[24]; @CompilationFinal private PosixSupport posixSupport; - @CompilationFinal private NFIZlibSupport nativeZlib; - @CompilationFinal private NFIBz2Support nativeBz2lib; - @CompilationFinal private NFILZMASupport nativeLZMA; + @CompilationFinal private NativeZlibSupport nativeZlib; + @CompilationFinal private NativeBz2Support nativeBz2lib; + @CompilationFinal private NativeLZMASupport nativeLZMA; // if set to 0 the VM will set it to whatever it likes private final AtomicLong pythonThreadStackSize = new AtomicLong(0); @@ -1423,15 +1423,15 @@ public boolean isNativeAccessAllowed() { return nativeAccessAllowed; } - public NFIZlibSupport getNFIZlibSupport() { + public NativeZlibSupport getNativeZlibSupport() { return nativeZlib; } - public NFIBz2Support getNFIBz2Support() { + public NativeBz2Support getNativeBz2Support() { return nativeBz2lib; } - public NFILZMASupport getNFILZMASupport() { + public NativeLZMASupport getNativeLZMASupport() { return nativeLZMA; } @@ -1749,9 +1749,9 @@ private void setupRuntimeInformation(boolean isPatching) { initializeLocale(); setIntMaxStrDigits(getOption(PythonOptions.IntMaxStrDigits)); if (!PythonImageBuildOptions.WITHOUT_COMPRESSION_LIBRARIES) { - nativeZlib = NFIZlibSupport.createNative(this, ""); - nativeBz2lib = NFIBz2Support.createNative(this, ""); - nativeLZMA = NFILZMASupport.createNative(this, ""); + nativeZlib = NativeZlibSupport.createNative(this, ""); + nativeBz2lib = NativeBz2Support.createNative(this, ""); + nativeLZMA = NativeLZMASupport.createNative(this, ""); } mainModule = PFactory.createPythonModule(T___MAIN__); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java index 3951fa8d2b..7f6ae44422 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PFactory.java @@ -237,7 +237,7 @@ import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode; import com.oracle.graal.python.nodes.bytecode_dsl.BytecodeDSLCodeUnit; import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNode; -import com.oracle.graal.python.runtime.NFIZlibSupport; +import com.oracle.graal.python.runtime.NativeZlibSupport; import com.oracle.graal.python.runtime.PythonContext; import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage; import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage; @@ -1194,15 +1194,15 @@ public static JavaDecompress createJavaZLibCompObjectDecompress(PythonLanguage l return new JavaDecompress(PythonBuiltinClassType.ZlibDecompress, PythonBuiltinClassType.ZlibDecompress.getInstanceShape(language), wbits, zdict); } - public static ZLibCompObject createNativeZLibCompObjectCompress(PythonLanguage language, long zst, NFIZlibSupport zlibSupport) { + public static ZLibCompObject createNativeZLibCompObjectCompress(PythonLanguage language, long zst, NativeZlibSupport zlibSupport) { return createNativeZLibCompObject(PythonBuiltinClassType.ZlibCompress, PythonBuiltinClassType.ZlibCompress.getInstanceShape(language), zst, zlibSupport); } - public static ZLibCompObject createNativeZLibCompObjectDecompress(PythonLanguage language, long zst, NFIZlibSupport zlibSupport) { + public static ZLibCompObject createNativeZLibCompObjectDecompress(PythonLanguage language, long zst, NativeZlibSupport zlibSupport) { return createNativeZLibCompObject(PythonBuiltinClassType.ZlibDecompress, PythonBuiltinClassType.ZlibDecompress.getInstanceShape(language), zst, zlibSupport); } - public static ZLibCompObject createNativeZLibCompObject(Object cls, Shape shape, long zst, NFIZlibSupport zlibSupport) { + public static ZLibCompObject createNativeZLibCompObject(Object cls, Shape shape, long zst, NativeZlibSupport zlibSupport) { return new NativeZlibCompObject(cls, shape, zst, zlibSupport); } @@ -1210,7 +1210,7 @@ public static ZlibDecompressorObject createJavaZlibDecompressorObject(PythonLang return ZlibDecompressorObject.createJava(PythonBuiltinClassType.ZlibDecompressor, PythonBuiltinClassType.ZlibDecompressor.getInstanceShape(language), wbits, zdict); } - public static ZlibDecompressorObject createNativeZlibDecompressorObject(PythonLanguage language, long zst, NFIZlibSupport zlibSupport) { + public static ZlibDecompressorObject createNativeZlibDecompressorObject(PythonLanguage language, long zst, NativeZlibSupport zlibSupport) { return ZlibDecompressorObject.createNative(PythonBuiltinClassType.ZlibDecompressor, PythonBuiltinClassType.ZlibDecompressor.getInstanceShape(language), zst, zlibSupport); } diff --git a/graalpython/python-libbz2/src/bz2.c b/graalpython/python-libbz2/src/bz2.c index f608a6f183..2404d6027f 100644 --- a/graalpython/python-libbz2/src/bz2.c +++ b/graalpython/python-libbz2/src/bz2.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 diff --git a/graalpython/python-liblzma/src/lzma.c b/graalpython/python-liblzma/src/lzma.c index 26a5a88323..1d886c7104 100644 --- a/graalpython/python-liblzma/src/lzma.c +++ b/graalpython/python-liblzma/src/lzma.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 diff --git a/graalpython/python-libzsupport/src/zlib.c b/graalpython/python-libzsupport/src/zlib.c index 26d3338d72..6a09f9c9c0 100644 --- a/graalpython/python-libzsupport/src/zlib.c +++ b/graalpython/python-libzsupport/src/zlib.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 From e7a8daa4d5f2195050f4e02539b8fbf955297bd0 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Fri, 29 May 2026 15:30:32 +0200 Subject: [PATCH 6/8] Remove unused NFI generator --- scripts/nfi_gen.py | 526 --------------------------------------------- 1 file changed, 526 deletions(-) delete mode 100644 scripts/nfi_gen.py diff --git a/scripts/nfi_gen.py b/scripts/nfi_gen.py deleted file mode 100644 index 070c713552..0000000000 --- a/scripts/nfi_gen.py +++ /dev/null @@ -1,526 +0,0 @@ -# Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. -# -# The Universal Permissive License (UPL), Version 1.0 -# -# Subject to the condition set forth below, permission is hereby granted to any -# person obtaining a copy of this software, associated documentation and/or -# data (collectively the "Software"), free of charge and under any and all -# copyright rights in the Software, and any and all patent rights owned or -# freely licensable by each licensor hereunder covering either (i) the -# unmodified Software as contributed to or provided by such licensor, or (ii) -# the Larger Works (as defined below), to deal in both -# -# (a) the Software, and -# -# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if -# one is included with the Software each a "Larger Work" to which the Software -# is contributed by such licensors), -# -# without restriction, including without limitation the rights to copy, create -# derivative works of, display, perform, and distribute the Software and make, -# use, sell, offer for sale, import, export, have made, and have sold the -# Software and the Larger Work(s), and to sublicense the foregoing rights on -# either these or other terms. -# -# This license is subject to the following condition: -# -# The above copyright notice and either this complete permission notice or at a -# minimum a reference to the UPL must be included in all copies or substantial -# portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -import re -import os - -LICENSE = """/* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * The Universal Permissive License (UPL), Version 1.0 - * - * Subject to the condition set forth below, permission is hereby granted to any - * person obtaining a copy of this software, associated documentation and/or - * data (collectively the "Software"), free of charge and under any and all - * copyright rights in the Software, and any and all patent rights owned or - * freely licensable by each licensor hereunder covering either (i) the - * unmodified Software as contributed to or provided by such licensor, or (ii) - * the Larger Works (as defined below), to deal in both - * - * (a) the Software, and - * - * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if - * one is included with the Software each a "Larger Work" to which the Software - * is contributed by such licensors), - * - * without restriction, including without limitation the rights to copy, create - * derivative works of, display, perform, and distribute the Software and make, - * use, sell, offer for sale, import, export, have made, and have sold the - * Software and the Larger Work(s), and to sublicense the foregoing rights on - * either these or other terms. - * - * This license is subject to the following condition: - * - * The above copyright notice and either this complete permission notice or at a - * minimum a reference to the UPL must be included in all copies or substantial - * portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */""" - -class_template = LICENSE + """ -package com.oracle.graal.python.runtime; - -import com.oracle.graal.python.PythonLanguage; -import com.oracle.truffle.api.CompilerAsserts; -import com.oracle.truffle.api.CompilerDirectives; -import com.oracle.truffle.api.TruffleLogger; - -/*- - * Generated using: -{cmd} - */ -public class NFI{lib_name}Support {{ - - private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFI{lib_name}Support.class); - -{var_defs}{enum_def} -{class_init} -{static_functions} -{functions} -}} -""" -var_def_template = " public static final int {name} = {value};" - -class_init_template = """ private static final String SUPPORTING_NATIVE_LIB_NAME = "{sys_lib}"; - - private final PythonContext pythonContext; - private final NativeLibrary.TypedNativeLibrary<{lib_name}NativeFunctions> typedNativeLib; - - @CompilerDirectives.CompilationFinal private boolean available; - - private NFI{lib_name}Support(PythonContext context, NativeLibrary.NFIBackend backend, String noNativeAccessHelp) {{ - if (context.isNativeAccessAllowed()) {{ - this.pythonContext = context; - this.typedNativeLib = NativeLibrary.create(SUPPORTING_NATIVE_LIB_NAME, {lib_name}NativeFunctions.values(), - backend, noNativeAccessHelp, true); - this.available = true; - }} else {{ - this.pythonContext = null; - this.typedNativeLib = null; - this.available = false; - }} - }} - - public static NFI{lib_name}Support createNative(PythonContext context, String noNativeAccessHelp) {{ - return new NFI{lib_name}Support(context, NativeLibrary.NFIBackend.NATIVE, noNativeAccessHelp); - }} - - public static NFI{lib_name}Support createLLVM(PythonContext context, String noNativeAccessHelp) {{ - return new NFI{lib_name}Support(context, NativeLibrary.NFIBackend.LLVM, noNativeAccessHelp); - }} -{finalizer_helpers} - public void notAvailable() {{ - if (available) {{ - CompilerAsserts.neverPartOfCompilation("Checking NFI{lib_name}Support availability should only be done during initialization."); - available = false; - }} - }} - - public boolean isAvailable() {{ - return available; - }} - - public PythonContext getContext() {{ - return pythonContext; - }} -""" - -gc_finalizer_template = """ - static class PointerReleaseCallback implements AsyncHandler.AsyncAction {{ - private final Pointer pointer; - - public PointerReleaseCallback(Pointer pointer) {{ - this.pointer = pointer; - }} - - @Override - public void execute(PythonContext context) {{ - synchronized (pointer) {{ - if (pointer.isReleased()) {{ - return; - }} - try {{ - pointer.doRelease(); - pointer.markReleased(); - LOGGER.finest("NFI{lib_name}Support pointer has been freed"); - }} catch (Exception e) {{ - LOGGER.severe("Error while trying to free NFI{lib_name}Support pointer: " + e.getMessage()); - }} - }} - }} - }} - - public static class Pointer extends AsyncHandler.SharedFinalizer.FinalizableReference {{ - - private final NFI{lib_name}Support lib; - - public Pointer(Object referent, Object ptr, NFI{lib_name}Support lib) {{ - super(referent, ptr, lib.pythonContext.getSharedFinalizer()); - this.lib = lib; - }} - - protected void doRelease() {{ - lib.{release_function}(getReference()); - }} - - @Override - public AsyncHandler.AsyncAction release() {{ - if (!isReleased()) {{ - return new PointerReleaseCallback(this); - }} - return null; - }} - }} -""" - -enum_def_template = """ enum {lib_name}NativeFunctions implements NativeLibrary.NativeFunction {{ -{enums}; - - private final String signature; - - {lib_name}NativeFunctions(String signature) {{ - this.signature = signature; - }} - - @Override - public String signature() {{ - return signature; - }} - }} -""" -enum_value_template = """ - /*- - {comments} - */ - {native_func_name}("{native_func_sign}"),""" - -doc_star = " *" -doc_java_param_template = doc_star + " @param {param_name} {param_desc}" -doc_java_return_template = doc_star + " @return %s" -java_function_return = " return " -java_function_no_return = " " -java_function = """ /** - * -{params_doc} -{return_doc} - */ - public {return_type} {function_name}({params_list} - NativeLibrary.InvokeNativeFunction invokeNode) {{ -{do_return}invokeNode.{call_type}(typedNativeLib, {lib_name}NativeFunctions.{native_func_name}{args_list}); - }} -""" - -java_static_function = """ /** - * -{params_doc} -{return_doc} - */ - public Object {function_name}({params_list}) {{ - return typedNativeLib.callUncached(pythonContext, {lib_name}NativeFunctions.{native_func_name}{args_list}); - }} -""" - -nfi_type_dict = { - 'int' : 'SINT32', - 'uInt' : 'UINT32', - 'int*' : '[SINT32]', - 'uint32_t' : 'UINT32', - 'uint32_t*' : '[UINT32]', - - 'long' : 'SINT64', - 'int64_t' : 'SINT64', - 'ssize_t' : 'SINT64', - 'long*' : '[SINT64]', - 'int64_t*' : '[SINT64]', - 'ssize_t*' : '[SINT64]', - 'uLong' : 'UINT64', - 'uint64_t' : 'UINT64', - 'size_t' : 'UINT64', - 'uint64_t*' : '[UINT64]', - 'size_t*' : '[UINT64]', - - 'double' : 'DOUBLE', - 'double*' : '[DOUBLE]', - - 'Byte' : 'UINT8', - 'Byte*' : '[UINT8]', - - 'char*' : 'STRING', - - 'void' : 'VOID', - 'void*': 'POINTER' -} - -java_type_dict = { - 'int' : 'int', - 'uInt' : 'int', - - 'uint32_t' : 'long', - - 'long' : 'long', - 'uLong' : 'long', - 'int64_t' : 'long', - 'uint64_t' : 'long', - 'ssize_t' : 'long', - 'size_t' : 'long', - - 'double' : 'double', - - 'Byte' : 'byte', - - 'char*' : 'String', - - 'void' : 'void', -} - -java_ret_type_dict = { - 'int' : ('int', 'callInt'), - 'uInt' : ('int', 'callInt'), - 'size_t' : ('long', 'callLong'), - 'ssize_t' : ('long', 'callLong'), - 'long' : ('long', 'callLong'), - 'uLong' : ('long', 'callLong'), - 'char*' : ('String', 'callString'), - 'void' : ('void', 'call'), -} - -def find_nfi_type(t, local_dict, obj='OBJECT', ref='POINTER'): - t = t.replace(' ','') - if t in local_dict: - return local_dict[t] - elif t in nfi_type_dict: - return nfi_type_dict[t] - else: - return ref if '*' in t else obj - -def find_java_type(t, obj='Object'): - t = t.replace(' ','') - if t in java_type_dict: - return java_type_dict[t] - else: - return obj - -ignore_list = ['{', 'static ', 'const ', 'struct '] -annotation_regex_name = "(name\(\'(.*?)\'\))" -annotation_regex_map = "(map\(\'(.*?)\', \'(.*?)\'\))" -annotation_regex_static = "(static\(true\))" -funcdef_regex = '([\w\s\*]+) ([\s\*\w]+)' -var_regex = '#define\s+(\w+)\s+([0-9]+)' - -annotation_regex_gc = "(release\(true\))" - -def generate_nfi_support_class(cmd, lib_name, c_source_path, sys_lib, to_path): - text = None - with open(c_source_path, 'r') as fp: - text = fp.read() - text_s = text.split('\n') - class nfi_function: - def __init__(self, java_name, name, ret_type, orig_ret_type, arg_orig_list, arg_name_list, arg_type_list, arg_java_list, comments, is_static, is_release): - self.java_name = java_name - self.name = name - self.orig_ret_type = orig_ret_type - self.ret_type = ret_type - self.arg_orig_list = arg_orig_list - self.arg_name_list = arg_name_list - self.arg_type_list = arg_type_list - self.arg_java_list = arg_java_list - self.comments = comments - self.is_static = is_static - self.is_release = is_release - - nfi_vars = [] - nfi_functions = [] - for i, l in enumerate(text_s): - if 'nfi_var' in l: - var_def = re.findall(var_regex, l) - nfi_vars.append(var_def[0]) - pass - elif 'nfi_function:' in l: - java_name = re.findall(annotation_regex_name, l) - java_name = java_name[0][1] - mapping = re.findall(annotation_regex_map, l) - local_dict = {} - for n, k, v in mapping: - local_dict[k] = v - - is_static = True if re.findall(annotation_regex_static, l) else False - is_release = True if re.findall(annotation_regex_gc, l) else False - - func_def = text_s[i+1] - j = 2 - while '{' not in func_def: - func_def += text_s[i+j].strip() - j += 1 - for ignore in ignore_list: - func_def = func_def.replace(ignore, '') - - sign_list = re.findall(funcdef_regex, func_def) - name = sign_list[0][1] - ret_type = sign_list[0][0] - if '*' in name: - ret_type += '*' - name = name.replace('*', '') - orig_ret_type = ret_type - ret_type = find_nfi_type(ret_type, local_dict) - arg_orig_list = [] - arg_name_list = [] - arg_type_list = [] - arg_java_list = [] - for t, v in sign_list[1:]: - arg_orig_list.append(t + ' ' + v) - if '*' in v: - t += '*' - arg_name_list.append(v.replace('*', '')) - arg_java_list.append(find_java_type(t)) - arg_type_list.append(find_nfi_type(t, local_dict)) - comments = [l.strip().replace('// ', ''), func_def.strip()] - nfi_functions.append(nfi_function(java_name, name, ret_type, orig_ret_type, arg_orig_list, arg_name_list, arg_type_list, arg_java_list, comments, is_static, is_release)) - - enum_vals = [] - java_funcs = [] - java_static_funcs = [] - release_func_name = None - for f in nfi_functions: - comments = '\n '.join(f.comments) - function_name = f.java_name - native_func_name = f.name - native_func_sign = '(' + ', '.join(f.arg_type_list) + '): ' + f.ret_type - enum_val = enum_value_template.format( - comments=comments, - native_func_name=native_func_name, - native_func_sign=native_func_sign - ) - enum_vals.append(enum_val) - - args_list = ', '.join(f.arg_name_list) - if args_list: - args_list = ', ' + args_list - def clean_str(s): - return ' '.join(s.strip().split()) if s else s - params_doc = '\n'.join([doc_java_param_template.format(param_name=clean_str(name), param_desc=clean_str(desc)) for name, desc in zip(f.arg_name_list, f.arg_orig_list)]) - if not params_doc: - params_doc = doc_star - return_type, call_type = java_ret_type_dict.get(f.orig_ret_type, ('Object', 'call')) - return_doc = doc_star - do_return = java_function_no_return - if return_type != "void": - return_doc = doc_java_return_template % clean_str(f.orig_ret_type) - do_return = java_function_return - params_list = ', '.join([' '.join(map(str, i)) for i in zip(f.arg_java_list, f.arg_name_list)]) - if f.is_static or f.is_release: - java_static_func = java_static_function.format( - params_doc=params_doc, - return_doc=return_doc, - params_list=params_list, - args_list=args_list, - lib_name=lib_name, - native_func_name=native_func_name, - function_name=function_name - ) - java_static_funcs.append(java_static_func) - if f.is_release: - release_func_name = function_name - else: - if params_list: - params_list += ',' - java_func = java_function.format( - params_doc=params_doc, - return_doc=return_doc, - do_return=do_return, - return_type=return_type, - call_type=call_type, - params_list=params_list, - args_list=args_list, - lib_name=lib_name, - native_func_name=native_func_name, - function_name=function_name - ) - java_funcs.append(java_func) - - var_defs = '' - if nfi_vars: - var_defs = '\n'.join([var_def_template.format(name=name, value=value) for name, value in nfi_vars]) - var_defs += '\n\n' - enum_vals_str = '\n'.join(enum_vals) - enum_def = enum_def_template.format( - lib_name=lib_name, - enums=enum_vals_str[:-1] - ) - - java_lang_imports = '' - other_imports = '' - finalizer_helpers = '' - if release_func_name: - finalizer_helpers = gc_finalizer_template.format( - lib_name=lib_name, - release_function=release_func_name - ) - - class_init = class_init_template.format( - sys_lib=sys_lib, - lib_name=lib_name, - finalizer_helpers=finalizer_helpers - ) - java_func_str = '\n'.join(java_funcs) - java_static_func_str = '\n'.join(java_static_funcs) - cmd_str = ' * %s' % ' '.join(cmd) - - class_str = class_template.format( - cmd=cmd_str, - lib_name=lib_name, - c_source_path=c_source_path, - sys_lib=sys_lib, - var_defs=var_defs, - enum_def=enum_def, - class_init=class_init, - static_functions=java_static_func_str, - functions=java_func_str - ) - if not to_path: - print(class_str) - else: - with open(to_path + os.path.sep + ("NFI%sSupport.java" % lib_name), 'w') as fp: - fp.write(class_str) - -if __name__ == "__main__": - import sys - from argparse import ArgumentParser - default_rt_path = [ - '..', - 'graalpython', - 'com.oracle.graal.python', - 'src', 'com', 'oracle', 'graal', 'python', - 'runtime'] - to_path = os.path.abspath(os.path.join(os.path.dirname(__file__), *default_rt_path)) - parser = ArgumentParser() - parser.add_argument('-name', required=True, help="Name of the generated NFISupport class") - parser.add_argument('-cpath', required=True, help="Path to the c file that contains nfi tags") - parser.add_argument('-lib', required=True, help="The name of the native library .so") - parser.add_argument('-to', default=to_path, help="Path of the generated class /NFISupport.java") - parsed_args = parser.parse_args(sys.argv[1:]) - - generate_nfi_support_class(sys.argv, parsed_args.name, parsed_args.cpath, parsed_args.lib, parsed_args.to) From 1d81616ed1afed95d4b185f1644edee934b67e60 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Mon, 1 Jun 2026 18:11:06 +0200 Subject: [PATCH 7/8] Remove any LLVM reference --- .../graal/python/test/advanced/LeakTest.java | 1 - .../oracle/graal/python/PythonLanguage.java | 2 +- .../graal/python/builtins/Python3Core.java | 2 +- .../objects/cext/capi/CApiContext.java | 4 --- .../capi/transitions/CApiTransitions.java | 18 +---------- .../graal/python/runtime/NativeLibrary.java | 31 +++++-------------- .../graal/python/runtime/PythonContext.java | 16 +++------- 7 files changed, 15 insertions(+), 59 deletions(-) diff --git a/graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advanced/LeakTest.java b/graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advanced/LeakTest.java index ce8438d096..f6e9bbfb81 100644 --- a/graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advanced/LeakTest.java +++ b/graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advanced/LeakTest.java @@ -189,7 +189,6 @@ private boolean checkCApiResidue(Heap heap) { addResidue(residues, inst, "referencesToBeFreed", this::collectionSize); addResidue(residues, inst, "nativeLookup", this::collectionSize); addResidue(residues, inst, "nativeWeakRef", this::collectionSize); - addResidue(residues, inst, "managedNativeLookup", this::collectionSize); addResidue(residues, inst, "nativeTypeLookup", this::objectArraySize); addResidue(residues, inst, "nativeStubLookup", this::objectArraySize); addResidue(residues, inst, "nativeStorageReferences", this::collectionSize); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java index a88ca8eb6a..5598dbb9da 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java @@ -157,7 +157,7 @@ version = PythonLanguage.VERSION, // characterMimeTypes = {PythonLanguage.MIME_TYPE}, // defaultMimeType = PythonLanguage.MIME_TYPE, // - dependentLanguages = {"nfi", "llvm"}, // + dependentLanguages = "nfi", // interactive = true, internal = false, // contextPolicy = TruffleLanguage.ContextPolicy.SHARED, // fileTypeDetectors = PythonFileDetector.class, // diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java index 4c2c2b9d57..77f0395e74 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java @@ -1070,7 +1070,7 @@ public final void postInitialize(Env env) { * Special case for _bz2: If native access is not allowed, we cannot use the built-in * implementation that would call libbz2 via NativeAccess. Therefore, we remove it from the * built-in modules map (and also from sys.modules if already loaded). This will cause a - * fallback to another _bz2 implementation (e.g. LLVM or maybe some Java lib). This + * fallback to another _bz2 implementation (e.g. some Java lib). This * needs to be done here and cannot be done in 'initializeBuiltins' because then we * would never include the intrinsified _bz2 module in the native image since native * access is never allowed during context pre-initialization. diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java index e1976c748d..0cbfa2dd9b 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java @@ -1427,8 +1427,4 @@ public long getOrAllocateNativePyMethodDef(PyMethodDefHelper pyMethodDef) { public PyCFunctionWrapper getOrCreatePyCFunctionWrapper(RootCallTarget ct, Function cons) { return pyCFunctionWrappers.computeIfAbsent(ct, cons); } - - public static boolean isPointerObject(Object object) { - return object.getClass() == NativePointer.class || object.getClass().getSimpleName().contains("NFIPointer") || object.getClass().getSimpleName().contains("LLVMPointer"); - } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java index c9eedea206..b699b73d1f 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java @@ -69,7 +69,6 @@ import java.util.Iterator; import java.util.Map.Entry; import java.util.Set; -import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -234,7 +233,6 @@ public HandleContext(boolean useShadowTable) { public final ArrayList referencesToBeFreed = new ArrayList<>(); public final HashMap> nativeLookup = new HashMap<>(); public final ConcurrentHashMap nativeWeakRef = new ConcurrentHashMap<>(); - public final WeakHashMap> managedNativeLookup = new WeakHashMap<>(); public IdReference[] nativeTypeLookup; @@ -1659,20 +1657,6 @@ public static int createPythonNativeClassReference(PythonNativeClass clazz) { } } - /** - * Creates a weak reference to {@code delegate} and connects that to the given {@code pointer} - * object such that the {@code pointer} can be resolved to the {@code delegate}. - *

- * This is used in LLVM managed mode where we will not have real native pointers (i.e. addresses - * pointing into off-heap memory) but managed pointers to objects emulating the native memory. - * We still need to be able to resolve those managed pointers to our objects. - *

- */ - public static void createManagedReference(Object delegate, Object pointer) { - assert PythonContext.get(null).ownsGil(); - getContext().managedNativeLookup.put(pointer, new WeakReference<>(delegate)); - } - // logging private static void log(Object... args) { @@ -2735,7 +2719,7 @@ private static PythonNativeClass recreatePythonNativeClass(HandleContext handleC @TruffleBoundary public static boolean isBackendPointerObject(Object obj) { - return obj != null && (obj.getClass().toString().contains("LLVMPointerImpl") || obj.getClass().toString().contains("NFIPointer") || obj.getClass().toString().contains("NativePointer")); + return obj != null && obj.getClass().toString().contains("NativePointer"); } /** diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java index 63468877f7..fab47674e1 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java @@ -102,17 +102,6 @@ interface NativeFunction { int ordinal(); } - enum NFIBackend { - NATIVE(""), - LLVM("with llvm "); - - private final String withClause; - - NFIBackend(String withClause) { - this.withClause = withClause; - } - } - /** * This is a helper exception that will be thrown in case a library is {@link #optional} and not * available. @@ -134,7 +123,6 @@ public Throwable fillInStackTrace() { private final int functionsCount; private final String name; - private final NFIBackend nfiBackend; /** * If given functionality has a fully managed variant that can be configured, this help message @@ -148,10 +136,9 @@ public Throwable fillInStackTrace() { private volatile Object cachedLibrary; private volatile InteropLibrary cachedLibraryInterop; - public NativeLibrary(String name, int functionsCount, NFIBackend nfiBackend, String noNativeAccessHelp, boolean optional) { + public NativeLibrary(String name, int functionsCount, String noNativeAccessHelp, boolean optional) { this.functionsCount = functionsCount; this.name = name; - this.nfiBackend = nfiBackend; this.noNativeAccessHelp = noNativeAccessHelp; this.optional = optional; } @@ -208,7 +195,7 @@ private FunctionWithSignature getFunction(Node location, PythonContext context, } private Object parseSignature(Node location, PythonContext context, String signature) { - Source sigSource = Source.newBuilder(J_NFI_LANGUAGE, nfiBackend.withClause + signature, "python-nfi-signature").build(); + Source sigSource = Source.newBuilder(J_NFI_LANGUAGE, signature, "python-nfi-signature").build(); return callCallTarget(context.getEnv().parseInternal(sigSource), location); } @@ -227,9 +214,9 @@ private Object loadLibrary(Node location, PythonContext context) { CompilerAsserts.neverPartOfCompilation(); if (context.isNativeAccessAllowed()) { String path = getLibPath(context, name); - String src = String.format("%sload (RTLD_LOCAL) \"%s\"", nfiBackend.withClause, path); + String src = String.format("load (RTLD_LOCAL) \"%s\"", path); if (LOGGER.isLoggable(Level.FINE)) { - LOGGER.fine(String.format("Loading native library %s from path %s %s", name, path, nfiBackend.withClause)); + LOGGER.fine(String.format("Loading native library %s from path %s", name, path)); } Source loadSrc = Source.newBuilder(J_NFI_LANGUAGE, src, "load:" + name).internal(true).build(); try { @@ -283,16 +270,12 @@ protected Object callUncached(PythonContext context, NativeFunction f, Object... } public static & NativeFunction> TypedNativeLibrary create(String name, T[] functions, String noNativeAccessHelp, boolean canIgnore) { - return create(name, functions, NFIBackend.NATIVE, noNativeAccessHelp, canIgnore); - } - - public static & NativeFunction> TypedNativeLibrary create(String name, T[] functions, NFIBackend nfiBackendName, String noNativeAccessHelp, boolean canIgnore) { - return new TypedNativeLibrary<>(name, functions.length, nfiBackendName, noNativeAccessHelp, canIgnore); + return new TypedNativeLibrary<>(name, functions.length, noNativeAccessHelp, canIgnore); } public static final class TypedNativeLibrary & NativeFunction> extends NativeLibrary { - public TypedNativeLibrary(String name, int functionsCount, NFIBackend nfiBackendName, String noNativeAccessHelp, boolean canIgnore) { - super(name, functionsCount, nfiBackendName, noNativeAccessHelp, canIgnore); + public TypedNativeLibrary(String name, int functionsCount, String noNativeAccessHelp, boolean canIgnore) { + super(name, functionsCount, noNativeAccessHelp, canIgnore); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java index e09fed6aaa..182a913bd0 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java @@ -542,11 +542,7 @@ public void setContextVarsContext(PContextVarsContext contextVarsContext) { this.contextVarsContext = contextVarsContext; } - public void dispose(boolean canRunGuestCode, boolean clearNativeThreadLocalVarPointer) { - dispose(canRunGuestCode, clearNativeThreadLocalVarPointer, true); - } - - public void dispose(boolean canRunGuestCode, boolean clearNativeThreadLocalVarPointer, boolean markShuttingDown) { + public void dispose(boolean clearNativeThreadLocalVarPointer, boolean markShuttingDown) { // This method may be called twice on the same object. /* @@ -577,11 +573,9 @@ public void dispose(boolean canRunGuestCode, boolean clearNativeThreadLocalVarPo /* * Write 'NULL' to the native thread-local variable used to store the PyThreadState - * struct such that it cannot accidentally be reused. Since this is done as a - * precaution, we just skip this if we cannot run guest code, because it may invoke - * LLVM. + * struct such that it cannot accidentally be reused. */ - if (nativeThreadLocalVarPointer != NULLPTR && canRunGuestCode && clearNativeThreadLocalVarPointer) { + if (nativeThreadLocalVarPointer != NULLPTR && clearNativeThreadLocalVarPointer) { NativeMemory.writePtr(nativeThreadLocalVarPointer, NULLPTR); } nativeThreadLocalVarPointer = NULLPTR; @@ -2277,7 +2271,7 @@ public void runShutdownHooks() { /** * Release all resources held by the thread states. This function needs to run as long as the - * context is still valid because it may call into LLVM to release handles. + * context is still valid because it may release handles. */ @TruffleBoundary private void disposeThreadStates() { @@ -2830,7 +2824,7 @@ public void disposeThread(Thread thread, boolean canRunGuestCode, boolean markSh ts.shutdown(); } threadStateMapping.remove(thread); - ts.dispose(canRunGuestCode, thread == Thread.currentThread(), markShuttingDown); + ts.dispose(thread == Thread.currentThread(), markShuttingDown); releaseSentinelLock(ts.sentinelLock); getSharedMultiprocessingData().removeChildContextThread(PThread.getThreadId(thread)); } From 0db4584c3e6fabdf01bbe24420bd555b935b6920 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Tue, 2 Jun 2026 11:09:07 +0200 Subject: [PATCH 8/8] Remove unused InvokeNativeFunction node --- .../graal/python/runtime/NativeLibrary.java | 146 ------------------ 1 file changed, 146 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java index fab47674e1..491d0584f8 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NativeLibrary.java @@ -41,36 +41,22 @@ package com.oracle.graal.python.runtime; import static com.oracle.graal.python.nodes.StringLiterals.J_NFI_LANGUAGE; -import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; import static com.oracle.graal.python.util.PythonUtils.callCallTarget; import java.lang.invoke.VarHandle; -import java.util.Objects; import java.util.logging.Level; import com.oracle.graal.python.PythonLanguage; -import com.oracle.graal.python.nodes.PNodeWithContext; -import com.oracle.graal.python.runtime.NativeLibraryFactory.InvokeNativeFunctionNodeGen; import com.oracle.graal.python.util.FunctionWithSignature; import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.CompilerDirectives; -import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.TruffleFile; import com.oracle.truffle.api.TruffleLogger; -import com.oracle.truffle.api.dsl.Bind; -import com.oracle.truffle.api.dsl.Cached; -import com.oracle.truffle.api.dsl.NeverDefault; -import com.oracle.truffle.api.dsl.Specialization; -import com.oracle.truffle.api.interop.ArityException; import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.UnknownIdentifierException; import com.oracle.truffle.api.interop.UnsupportedMessageException; -import com.oracle.truffle.api.interop.UnsupportedTypeException; -import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.Node; -import com.oracle.truffle.api.profiles.InlinedExactClassProfile; import com.oracle.truffle.api.source.Source; -import com.oracle.truffle.api.strings.TruffleString; import com.oracle.truffle.nfi.api.SignatureLibrary; /** @@ -82,11 +68,6 @@ * Because of Truffle DSL restrictions this class cannot be generic, but users should work with * generic subclass {@link TypedNativeLibrary}, which can be created with one of the {@code create} * factory methods. - *

- * For now, until there is no need to access the library and function objects directly, this object - * is opaque to the outside code and the only entrypoint is {@link InvokeNativeFunction}, which - * lazily loads the library, the requested function and invokes it. This node takes care of - * efficient caching of the loaded NFI objects. */ public class NativeLibrary { private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NativeLibrary.class); @@ -278,131 +259,4 @@ public TypedNativeLibrary(String name, int functionsCount, String noNativeAccess super(name, functionsCount, noNativeAccessHelp, canIgnore); } } - - public abstract static class InvokeNativeFunction extends PNodeWithContext { - private static final InvokeNativeFunction UNCACHED = InvokeNativeFunctionNodeGen.create(InteropLibrary.getUncached()); - @Child private InteropLibrary resultInterop; - @Child private TruffleString.SwitchEncodingNode switchEncodingNode; - - public InvokeNativeFunction(InteropLibrary resultInterop) { - this.resultInterop = resultInterop; - } - - @NeverDefault - public static InvokeNativeFunction create() { - return InvokeNativeFunctionNodeGen.create(null); - } - - public static InvokeNativeFunction getUncached() { - return UNCACHED; - } - - public & NativeFunction> Object call(TypedNativeLibrary lib, T function, Object... args) { - return execute(lib, function, args); - } - - public & NativeFunction> long callLong(TypedNativeLibrary lib, T function, Object... args) { - try { - return ensureResultInterop().asLong(call(lib, function, args)); - } catch (UnsupportedMessageException e) { - throw CompilerDirectives.shouldNotReachHere(function.name(), e); - } - } - - public & NativeFunction> int callInt(TypedNativeLibrary lib, T function, Object... args) { - try { - return ensureResultInterop().asInt(call(lib, function, args)); - } catch (UnsupportedMessageException e) { - throw CompilerDirectives.shouldNotReachHere(function.name(), e); - } - } - - public & NativeFunction> TruffleString callString(TypedNativeLibrary lib, T function, Object... args) { - try { - return ensureSwitchEncoding().execute(ensureResultInterop().asTruffleString(call(lib, function, args)), TS_ENCODING); - } catch (UnsupportedMessageException e) { - throw CompilerDirectives.shouldNotReachHere(function.name(), e); - } - } - - protected abstract Object execute(NativeLibrary lib, NativeFunction function, Object[] args); - - @Specialization(guards = {"isSingleContext()", "function == cachedFunction", "lib == cachedLib"}, limit = "3") - static Object doSingleContext(@SuppressWarnings("unused") NativeLibrary lib, @SuppressWarnings("unused") NativeFunction function, Object[] args, - @SuppressWarnings("unused") @Cached(value = "lib", weak = true) NativeLibrary cachedLib, - @Cached("function") NativeFunction cachedFunction, - @Cached(value = "getFunction($node, lib, function)", weak = true) FunctionWithSignature funObj, - @CachedLibrary("funObj.signature()") SignatureLibrary funInterop) { - return invoke(cachedFunction, args, funObj, funInterop); - } - - @Specialization(replaces = "doSingleContext") - static Object doMultiContext(NativeLibrary lib, NativeFunction functionIn, Object[] args, - @Bind Node inliningTarget, - @Cached InlinedExactClassProfile functionClassProfile, - @CachedLibrary(limit = "1") SignatureLibrary funInterop) { - NativeFunction function = functionClassProfile.profile(inliningTarget, functionIn); - FunctionWithSignature funObj = lib.getCachedFunction(inliningTarget, PythonContext.get(funInterop), function); - return invoke(function, args, funObj, funInterop); - } - - private static Object invoke(NativeFunction function, Object[] args, FunctionWithSignature funObj, SignatureLibrary funInterop) { - try { - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest(buildLogMessage(function, args)); - } - Object result = funInterop.call(funObj.signature(), funObj.function(), args); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest(buildReturnLogMessage(function, result)); - } - return result; - } catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) { - throw CompilerDirectives.shouldNotReachHere(function.name(), e); - } - } - - protected FunctionWithSignature getFunction(Node location, NativeLibrary lib, NativeFunction fun) { - return lib.getFunction(location, PythonContext.get(this), fun); - } - - @TruffleBoundary - private static String buildLogMessage(NativeFunction function, Object[] args) { - StringBuilder sb = new StringBuilder("Executing native function "); - sb.append(function.name()).append(" with arguments: "); - for (Object arg : args) { - sb.append(safeToString(arg)).append(','); - } - return sb.toString(); - } - - @TruffleBoundary - private static String buildReturnLogMessage(NativeFunction function, Object result) { - return "Finished executing native function " + function.name() + " with result: " + safeToString(result); - } - - private static String safeToString(Object value) { - try { - return Objects.toString(value); - } catch (Exception ex) { - return String.format("%s (toString threw %s),", value.getClass().getSimpleName(), ex.getClass().getSimpleName()); - } - } - - public InteropLibrary ensureResultInterop() { - if (resultInterop == null) { - CompilerDirectives.transferToInterpreterAndInvalidate(); - resultInterop = insert(InteropLibrary.getFactory().createDispatched(3)); - } - return resultInterop; - } - - public TruffleString.SwitchEncodingNode ensureSwitchEncoding() { - if (switchEncodingNode == null) { - CompilerDirectives.transferToInterpreterAndInvalidate(); - switchEncodingNode = insert(TruffleString.SwitchEncodingNode.create()); - } - return switchEncodingNode; - } - - } }