Skip to content

Commit

Permalink
Allocate seqprod state during registration, so that each compression …
Browse files Browse the repository at this point in the history
…context gets its own state allocation
  • Loading branch information
Coder-256 authored and luben committed Nov 30, 2023
1 parent 220e990 commit 53e2087
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/main/java/com/github/luben/zstd/SequenceProducer.java
@@ -1,21 +1,30 @@
package com.github.luben.zstd;

/**
* Interface for an extenal sequence producer. The class implementing
* SequenceProducer must be provided by the user. To use, SequenceProducer should
* Interface for an extenal sequence producer. The class implementing
* SequenceProducer must be provided by the user. To use, SequenceProducer should
* be passed to `ZstdCompressCtx.registerSequenceProducer()`.
*/
public interface SequenceProducer {
/**
* Returns a function pointer of type `ZSTD_sequenceProducer_F` that will be
* passed to zstd as the `sequenceProducer` function. The returned pointer
* must be valid for the duration of this SeqeunceProducers's lifetime.
* Returns a pointer to the sequence producer function. This method is called
* once in {@link ZstdCompressCtx registerSequenceProducer}.
* @return A function pointer of type `ZSTD_sequenceProducer_F *`
*/
public long getProducerFunction();
public long getFunctionPointer();

/**
* Returns a `void *` pointer to the `sequenceProducerState`. The returned
* pointer must be valid for the duration of this SeqeunceProducers's lifetime.
* Allocate the sequence producer state. The returned pointer will be passed
* to the sequence producer function. This method is called once in
* {@link ZstdCompressCtx#registerSequenceProducer(SequenceProducer)}.
* @return A `void *` pointer to the sequence producer state
*/
public long getStatePointer();
public long createState();

/**
* Free the sequence producer state. This method is called when closing the
* {@link ZstdCompressCtx} or registering a different sequence producer.
* @param statePointer the state pointer to be freed
*/
public void freeState(long statePointer);
}
18 changes: 15 additions & 3 deletions src/main/java/com/github/luben/zstd/ZstdCompressCtx.java
Expand Up @@ -16,7 +16,9 @@ public class ZstdCompressCtx extends AutoCloseBase {

private ZstdDictCompress compression_dict = null;

private SequenceProducer sequence_producer = null;
private SequenceProducer seqprod = null;

private long seqprod_state = 0;

private static native long init();

Expand All @@ -38,6 +40,10 @@ void doClose() {
if (nativePtr != 0) {
free(nativePtr);
nativePtr = 0;
if (seqprod != null) {
seqprod.freeState(seqprod_state);
seqprod = null;
}
}
}

Expand Down Expand Up @@ -277,12 +283,18 @@ public ZstdCompressCtx setLong(int windowLog) {
public void registerSequenceProducer(SequenceProducer producer) {
ensureOpen();
acquireSharedLock();
if (this.seqprod != null) {
this.seqprod.freeState(seqprod_state);
}

if (producer == null) {
seqprod_state = 0;
Zstd.registerSequenceProducer(nativePtr, 0, 0);
} else {
Zstd.registerSequenceProducer(nativePtr, producer.getStatePointer(), producer.getProducerFunction());
seqprod_state = producer.createState();
Zstd.registerSequenceProducer(nativePtr, seqprod_state, producer.getFunctionPointer());
}
this.sequence_producer = producer;
this.seqprod = producer;
releaseSharedLock();
}

Expand Down

0 comments on commit 53e2087

Please sign in to comment.