From ad64f34f43a1d37fcc9a3c87be89d90d64dcc073 Mon Sep 17 00:00:00 2001 From: Alexey Pismenskiy Date: Thu, 1 Feb 2024 09:37:23 -0700 Subject: [PATCH] Addresses https://github.com/gliwka/hyperscan-java/issues/220 --- .../com/gliwka/hyperscan/wrapper/Scanner.java | 6 +++-- .../gliwka/hyperscan/wrapper/ScannerTest.java | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/gliwka/hyperscan/wrapper/ScannerTest.java diff --git a/src/main/java/com/gliwka/hyperscan/wrapper/Scanner.java b/src/main/java/com/gliwka/hyperscan/wrapper/Scanner.java index c560149..d2877be 100644 --- a/src/main/java/com/gliwka/hyperscan/wrapper/Scanner.java +++ b/src/main/java/com/gliwka/hyperscan/wrapper/Scanner.java @@ -47,8 +47,10 @@ public Scanner() { private static class NativeScratch extends hs_scratch_t { void registerDeallocator() { - hs_scratch_t p = new hs_scratch_t(this); - deallocator(() -> hs_free_scratch(p)); + if (deallocator() != null) { + hs_scratch_t p = new hs_scratch_t(this); + deallocator(() -> hs_free_scratch(p)); + } } } diff --git a/src/test/java/com/gliwka/hyperscan/wrapper/ScannerTest.java b/src/test/java/com/gliwka/hyperscan/wrapper/ScannerTest.java new file mode 100644 index 0000000..a340d71 --- /dev/null +++ b/src/test/java/com/gliwka/hyperscan/wrapper/ScannerTest.java @@ -0,0 +1,27 @@ +package com.gliwka.hyperscan.wrapper; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +public class ScannerTest { + + @Test + void oneScannerWithMultipleDatabases() throws Exception { + try ( + Database dbA = Database.compile(new Expression("a")); + Database dbB = Database.compile(new Expression("b")); + Scanner scanner = new Scanner() + ) { + scanner.allocScratch(dbA); + List matchesA = scanner.scan(dbA, "a"); + assertFalse(matchesA.isEmpty()); + + scanner.allocScratch(dbB); + List matchesB = scanner.scan(dbB, "b"); + assertFalse(matchesB.isEmpty()); + } + } +}