Skip to content

Commit

Permalink
Make one addition to RecyclerPool.WithPool to ensure "release" meth…
Browse files Browse the repository at this point in the history
…od implemented (#1126)
  • Loading branch information
cowtowncoder committed Oct 15, 2023
1 parent d2854f7 commit 5a971d2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/jackson/core/io/IOContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ private IllegalArgumentException wrongBuf() {
@Override
public void close() {
if (!_closed) {
_bufferRecycler.release();
_bufferRecycler.releaseToPool();
_closed = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ protected int charBufferLength(int ix) {
protected byte[] balloc(int size) { return new byte[size]; }
protected char[] calloc(int size) { return new char[size]; }

/*
/**********************************************************
/* WithPool implementation
/**********************************************************
*/

/**
* Method called by owner of this recycler instance, to provide reference to
* {@link RecyclerPool} into which instance is to be released (if any)
Expand All @@ -205,7 +211,7 @@ protected int charBufferLength(int ix) {
*/
@Override
public BufferRecycler withPool(RecyclerPool<BufferRecycler> pool) {
if (this._pool != null) {
if (_pool != null) {
throw new IllegalStateException("BufferRecycler already linked to pool: "+pool);
}
// assign to pool to which this BufferRecycler belongs in order to release it
Expand All @@ -220,7 +226,8 @@ public BufferRecycler withPool(RecyclerPool<BufferRecycler> pool) {
*
* @since 2.16
*/
public void release() {
@Override
public void releaseToPool() {
if (_pool != null) {
RecyclerPool<BufferRecycler> tmpPool = _pool;
// nullify the reference to the pool in order to avoid the risk of releasing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class JsonRecyclerPools
public static RecyclerPool<BufferRecycler> defaultPool() {
return threadLocalPool();
}

/**
* Accessor for getting the shared/global {@link ThreadLocalPool} instance
* (due to design only one instance ever needed)
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/util/RecyclerPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@ public interface RecyclerPool<P extends RecyclerPool.WithPool<P>> extends Serial
* @param <P> Self type
*/
public interface WithPool<P extends WithPool<P>> {
/**
* Method to call to add link from pooled item back to pool
* that handles it
*
* @param pool Pool that "owns" pooled item
*
* @return This item (for call chaining)
*/
P withPool(RecyclerPool<P> pool);

/**
* Method called when this item is to be released back to the
* pool that owns it (if any)
*/
void releaseToPool();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void checkBufferRecyclerPoolImpl(RecyclerPool<BufferRecycler> pool,
try {
assertSame(usedBufferRecycler, pooledBufferRecycler);
} finally {
pooledBufferRecycler.release();
pooledBufferRecycler.releaseToPool();
}
}
}
Expand Down

0 comments on commit 5a971d2

Please sign in to comment.