Home --> Documentations --> PJLIB Reference

Data Structures | Macros | Typedefs | Functions
Memory Pool Object

The memory pool is an opaque object created by pool factory. Application uses this object to request a memory chunk, by calling pj_pool_alloc(), pj_pool_calloc(), or pj_pool_zalloc(). When the application has finished using the pool, it must call pj_pool_release() to free all the chunks previously allocated and release the pool back to the factory. More...

Data Structures

struct  pj_pool_block
 
struct  pj_pool_t
 

Macros

#define PJ_POOL_SIZE   (sizeof(struct pj_pool_t))
 
#define PJ_POOL_ALIGNMENT   4
 
#define PJ_POOL_ALLOC_T(pool, type)   ((type*)pj_pool_alloc(pool, sizeof(type)))
 
#define PJ_POOL_ZALLOC_T(pool, type)   ((type*)pj_pool_zalloc(pool, sizeof(type)))
 

Typedefs

typedef void pj_pool_callback (pj_pool_t *pool, pj_size_t size)
 

Functions

pj_pool_tpj_pool_create (pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
 
void pj_pool_release (pj_pool_t *pool)
 
void pj_pool_safe_release (pj_pool_t **ppool)
 
const char * pj_pool_getobjname (const pj_pool_t *pool)
 
void pj_pool_reset (pj_pool_t *pool)
 
pj_size_t pj_pool_get_capacity (pj_pool_t *pool)
 
pj_size_t pj_pool_get_used_size (pj_pool_t *pool)
 
void * pj_pool_alloc (pj_pool_t *pool, pj_size_t size)
 
void * pj_pool_calloc (pj_pool_t *pool, pj_size_t count, pj_size_t elem)
 
void * pj_pool_zalloc (pj_pool_t *pool, pj_size_t size)
 
void * pj_pool_alloc_from_block (pj_pool_block *block, pj_size_t size)
 
void * pj_pool_allocate_find (pj_pool_t *pool, pj_size_t size)
 

Detailed Description

The memory pool is an opaque object created by pool factory. Application uses this object to request a memory chunk, by calling pj_pool_alloc(), pj_pool_calloc(), or pj_pool_zalloc(). When the application has finished using the pool, it must call pj_pool_release() to free all the chunks previously allocated and release the pool back to the factory.

A memory pool is initialized with an initial amount of memory, which is called a block. Pool can be configured to dynamically allocate more memory blocks when it runs out of memory.

The pool doesn't keep track of individual memory allocations by user, and the user doesn't have to free these indidual allocations. This makes memory allocation simple and very fast. All the memory allocated from the pool will be destroyed when the pool itself is destroyed.

More on Threading Policies

Examples

For some sample codes on how to use the pool, please see:

Macro Definition Documentation

#define PJ_POOL_ALIGNMENT   4

Pool memory alignment (must be power of 2).

#define PJ_POOL_ALLOC_T (   pool,
  type 
)    ((type*)pj_pool_alloc(pool, sizeof(type)))

This macro allocates memory from the pool and returns the instance of the specified type. It provides a stricker type safety than pj_pool_alloc() since the return value of this macro will be type-casted to the specified type.

Parameters
poolThe pool
typeThe type of object to be allocated
Returns
Memory buffer of the specified type.
#define PJ_POOL_SIZE   (sizeof(struct pj_pool_t))

Guidance on how much memory required for initial pool administrative data.

#define PJ_POOL_ZALLOC_T (   pool,
  type 
)    ((type*)pj_pool_zalloc(pool, sizeof(type)))

This macro allocates memory from the pool, zeroes the buffer, and returns the instance of the specified type. It provides a stricker type safety than pj_pool_zalloc() since the return value of this macro will be type-casted to the specified type.

Parameters
poolThe pool
typeThe type of object to be allocated
Returns
Memory buffer of the specified type.

Typedef Documentation

typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size)

The type for function to receive callback from the pool when it is unable to allocate memory. The elegant way to handle this condition is to throw exception, and this is what is expected by most of this library components.

Function Documentation

void* pj_pool_alloc ( pj_pool_t pool,
pj_size_t  size 
)

Allocate storage with the specified size from the pool. If there's no storage available in the pool, then the pool can allocate more blocks if the increment size is larger than the requested size.

Parameters
poolthe pool.
sizethe requested size.
Returns
pointer to the allocated memory.
See also
PJ_POOL_ALLOC_T
void* pj_pool_calloc ( pj_pool_t pool,
pj_size_t  count,
pj_size_t  elem 
)

Allocate storage from the pool, and initialize it to zero. This function behaves like pj_pool_alloc(), except that the storage will be initialized to zero.

Parameters
poolthe pool.
countthe number of elements in the array.
elemthe size of individual element.
Returns
pointer to the allocated memory.

Referenced by pj_pool_zalloc().

pj_pool_t* pj_pool_create ( pj_pool_factory factory,
const char *  name,
pj_size_t  initial_size,
pj_size_t  increment_size,
pj_pool_callback callback 
)

Create a new pool from the pool factory. This wrapper will call create_pool member of the pool factory.

Parameters
factoryThe pool factory.
nameThe name to be assigned to the pool. The name should not be longer than PJ_MAX_OBJ_NAME (32 chars), or otherwise it will be truncated.
initial_sizeThe size of initial memory blocks taken by the pool. Note that the pool will take 68+20 bytes for administrative area from this block.
increment_sizethe size of each additional blocks to be allocated when the pool is running out of memory. If user requests memory which is larger than this size, then an error occurs. Note that each time a pool allocates additional block, it needs PJ_POOL_SIZE more to store some administrative info.
callbackCallback to be called when error occurs in the pool. If this value is NULL, then the callback from pool factory policy will be used. Note that when an error occurs during pool creation, the callback itself is not called. Instead, NULL will be returned.
Returns
The memory pool, or NULL.
pj_size_t pj_pool_get_capacity ( pj_pool_t pool)

Get the pool capacity, that is, the system storage that have been allocated by the pool, and have been used/will be used to allocate user requests. There's no guarantee that the returned value represent a single contiguous block, because the capacity may be spread in several blocks.

Parameters
poolthe pool.
Returns
the capacity.
pj_size_t pj_pool_get_used_size ( pj_pool_t pool)

Get the total size of user allocation request.

Parameters
poolthe pool.
Returns
the total size.
const char* pj_pool_getobjname ( const pj_pool_t pool)

Get pool object name.

Parameters
poolthe pool.
Returns
pool name as NULL terminated string.
void pj_pool_release ( pj_pool_t pool)

Release the pool back to pool factory.

Parameters
poolMemory pool.
void pj_pool_reset ( pj_pool_t pool)

Reset the pool to its state when it was initialized. This means that if additional blocks have been allocated during runtime, then they will be freed. Only the original block allocated during initialization is retained. This function will also reset the internal counters, such as pool capacity and used size.

Parameters
poolthe pool.
void pj_pool_safe_release ( pj_pool_t **  ppool)

Release the pool back to pool factory and set the pool pointer to zero.

Parameters
ppoolPointer to memory pool.
void* pj_pool_zalloc ( pj_pool_t pool,
pj_size_t  size 
)

Allocate storage from the pool and initialize it to zero.

Parameters
poolThe pool.
sizeThe size to be allocated.
Returns
Pointer to the allocated memory.
See also
PJ_POOL_ZALLOC_T

References pj_pool_calloc().

 


PJLIB Open Source, high performance, small footprint, and very very portable framework
Copyright (C) 2006-2009 Teluu Inc.