Caching pool is one sample implementation of pool factory where the factory can reuse memory to create a pool. Application defines what the maximum memory the factory can hold, and when a pool is released the factory decides whether to destroy the pool or to keep it for future use. If the total amount of memory in the internal cache is still within the limit, the factory will keep the pool in the internal cache, otherwise the pool will be destroyed, thus releasing the memory back to the system.




Number of unique sizes, to be used as index to the free list. Each pool in the free list is organized by it’s size.


void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)

Initialize caching pool.

  • ch_pool – The caching pool factory to be initialized.

  • policy – Pool factory policy.

  • max_capacity – The total capacity to be retained in the cache. When the pool is returned to the cache, it will be kept in recycling list if the total capacity of pools in this list plus the capacity of the pool is still below this value.

void pj_caching_pool_destroy(pj_caching_pool *ch_pool)

Destroy caching pool, and release all the pools in the recycling list.


ch_pool – The caching pool.

struct pj_caching_pool
#include <pool.h>

Declaration for caching pool. Application doesn’t normally need to care about the contents of this struct, it is only provided here because application need to define an instance of this struct (we can not allocate the struct from a pool since there is no pool factory yet!).

Forward declaration for caching pool, a pool factory implementation.

Public Members

pj_pool_factory factory

Pool factory interface, must be declared first.

pj_size_t capacity

Current factory’s capacity, i.e. number of bytes that are allocated and available for application in this factory. The factory’s capacity represents the size of all pools kept by this factory in it’s free list, which will be returned to application when it requests to create a new pool.

pj_size_t max_capacity

Maximum size that can be held by this factory. Once the capacity has exceeded max_capacity, further pj_pool_release() will flush the pool. If the capacity is still below the max_capacity, pj_pool_release() will save the pool to the factory’s free list.

pj_size_t used_count

Number of pools currently held by applications. This number gets incremented everytime pj_pool_create() is called, and gets decremented when pj_pool_release() is called.

pj_size_t used_size

Total size of memory currently used by application.

This field is deprecated.

pj_size_t peak_used_size

The maximum size of memory used by application throughout the life of the caching pool.

This field is deprecated.

pj_list free_list[16]

Lists of pools in the cache, indexed by pool size.

pj_list used_list

List of pools currently allocated by applications.

char pool_buf[256 * (sizeof(size_t) / 4)]

Internal pool.

pj_lock_t *lock