java.lang.Object
net.i2p.data.i2np.GarlicClovePool

public class GarlicClovePoolextends Object
Pool for reusing GarlicClove instances across message parsing. Reduces per-clove allocation in the hot garlic message loop.

Cloves are acquired, populated via setters, used, then released back to the pool for reuse. The pool expands dynamically when empty.

Thread-safe for single-producer/single-consumer use (typical garlic message parsing pattern). Not safe for concurrent acquire/release.

  • Field Details

    • pool

      private final GarlicClove[] pool
      The clove objects held for reuse.
    • available

      private int available
      How many clove objects are ready.
    • context

      private final I2PAppContext context
      Supplies the pool's configuration.
  • Constructor Details

    • GarlicClovePool

      public GarlicClovePool(I2PAppContext context, int initialCapacity)
      Create a pool with the specified initial capacity.
      Parameters:
      context - the router/app context for creating new cloves
      initialCapacity - the number of cloves to pre-allocate
    • GarlicClovePool

      public GarlicClovePool(I2PAppContext context)
      Create a pool with default capacity (4).
      Parameters:
      context - the router/app context
  • Method Details

    • acquire

      public GarlicClove acquire()
      Acquire a clove from the pool. If the pool is empty, a new clove is allocated. The returned clove is in its initial state (cloveId = -1, no data, no certificate) ready for population.
      Returns:
      a GarlicClove instance, never null
    • release

      public void release(GarlicClove clove)
      Release a clove back to the pool for reuse. The clove is not cleared — callers should ensure they no longer hold references to the released clove.
      Parameters:
      clove - the clove to release
    • available

      public int available()
      Current number of available cloves in the pool.
      Returns:
      the number of cloves the pool can currently hand out
    • capacity

      public int capacity()
      Maximum capacity of the pool.
      Returns:
      the most cloves the pool can hold at once