pool.cc File Reference

Handling of memory pools. More...

#include <jabberdlib.h>

Defines

#define MAX_MALLOC_TRIES   10
#define _pool__malloc   malloc
#define _pool__free   free

Functions

void * _retried__malloc (size_t size)
pool _pool_new (char *zone, int line)
void _pool_heap_free (void *arg)
void _pool_cleanup_append (pool p, struct pfree *pf)
struct pfree_pool_free (pool p, pool_cleaner f, void *arg)
struct pheap_pool_heap (pool p, int size)
pool _pool_new_heap (int size, char *zone, int line)
void * pmalloc (pool p, int size)
void * pmalloc_x (pool p, int size, char c)
void * pmalloco (pool p, int size)
char * pstrdup (pool p, const char *src)
char * pstrdupx (pool p, const char *src)
int pool_size (pool p)
void pool_free (pool p)
void pool_cleanup (pool p, pool_cleaner f, void *arg)
void pool_stat (int full)

Detailed Description

Handling of memory pools.

Jabberd handles its memory allocations in pools. You create a pool, can allocate memory from it and all allocations will be freed if you free the pool. Therefore you don't have to care that each malloc is freed, you only have to take care that the pool is freed.

The normal call-flow for pools is:

pool p = pool_new(); struct mystruct *allocation1 = pmalloc(sizeof(struct mystruct)); struct myotherstruct *allocation2 = pmalloc(sizeof(struct myotherstruct)); ... pool_free(p);


Define Documentation

#define _pool__free   free

_pool__free updates pool__total counter if POOL_DEBUG is defined

Referenced by _pool_heap_free(), pmalloc(), and pool_free().

#define _pool__malloc   malloc

_pool__malloc updates pool__total counter if POOL_DEBUG is defined

Referenced by _retried__malloc().

#define MAX_MALLOC_TRIES   10

how many seconds we try to allocate memory

Referenced by _retried__malloc().


Function Documentation

void _pool_cleanup_append ( pool  p,
struct pfree pf 
)

append a pool_cleaner function (callback) to a pool

mem should always be freed last

All appended pool_cleaner functions will be called if a pool is freed. This might be used to clean logically subpools.

Parameters:
p to which pool the pool_cleaner should be added
pf structure containing the reference to the pool_cleaner and links for the list

References pool_struct::cleanup, and pfree::next.

Referenced by _pool_heap(), and pmalloc().

struct pfree* _pool_free ( pool  p,
pool_cleaner  f,
void *  arg 
) [read]

create a cleanup tracker

this function is used to create a pfree structure that can be passed to _pool_cleanup_append()

Parameters:
p the pool to which the pool_cleaner should be added
f the function that should be called if the pool is freed
arg the parameter that should be passed to the pool_cleaner function
Returns:
pointer to the new pfree structure

References _retried__malloc(), pfree::arg, pfree::f, and pfree::next.

Referenced by _pool_heap(), pmalloc(), and pool_cleanup().

struct pheap* _pool_heap ( pool  p,
int  size 
) [read]

create a heap and make sure it get's cleaned up

pheaps are used by memory pools internally to handle the memory allocations

Note:
the macro pool_heap calls _pool_new_heap and NOT _pool_heap
Parameters:
p for which pool the heap should be created
size how big the pool should be
Returns:
pointer to the new pheap

References _pool_cleanup_append(), _pool_free(), _pool_heap_free(), _retried__malloc(), pheap::block, pfree::heap, pool_struct::size, pheap::size, and pheap::used.

Referenced by _pool_new_heap(), and pmalloc().

void _pool_heap_free ( void *  arg  ) 

free a memory heap (struct pheap)

Parameters:
arg which heep should be freed

References _pool__free, and pheap::block.

Referenced by _pool_heap().

pool _pool_new ( char *  zone,
int  line 
)

make an empty pool

Use the macro pool_new() instead of a direct call to this function. The macro will create the parameters for you.

Parameters:
zone the file in which the pool_new macro is called
line the line in the file in which the pool_new macro is called
Returns:
the new allocated memory pool

References _retried__malloc(), pool_struct::cleanup, pool_struct::heap, pool_struct::size, xhash_new(), and xhash_put().

Referenced by _pool_new_heap().

pool _pool_new_heap ( int  size,
char *  zone,
int  line 
)

create a new memory pool and set the initial heap size

Note:
you should not call this function but use the macro pool_heap instead which fills zone and line automatically
Parameters:
size the initial size of the memory pool
zone the file where this function is called (for debugging)
line the line in the file where this function is called
Returns:
the new memory pool

References _pool_heap(), _pool_new(), and pool_struct::heap.

void* _retried__malloc ( size_t  size  )  [inline]

try to allocate memory

If allocation fails, it will be retries for MAX_MALLOC_TRIES seconds. If it still fails, we exit the process

Parameters:
size how many bytes of memory we allocate
Returns:
pointer to the allocated memory

References _pool__malloc, and MAX_MALLOC_TRIES.

Referenced by _pool_free(), _pool_heap(), _pool_new(), and pmalloc().

void* pmalloc ( pool  p,
int  size 
)

allocate memory from a memory pool

Parameters:
p the pool to use
size how much memory to allocate
Returns:
pointer to the allocated memory

References _pool__free, _pool_cleanup_append(), _pool_free(), _pool_heap(), _retried__malloc(), pheap::block, pool_struct::heap, pool_struct::size, pheap::size, and pheap::used.

Referenced by jlimit_new(), jpacket_new(), pmalloc_x(), pmalloco(), pstrdup(), strescape(), strunescape(), and xmlnode_insert_cdata().

void* pmalloc_x ( pool  p,
int  size,
char  c 
)

allocate memory and initialize the memory with the given char c

Deprecated:
jabberd does use pmalloco instead, this function will be removed
Parameters:
p which pool to use
size the size of the allocation
c the initialization character
Returns:
pointer to the allocated memory

References pmalloc().

void* pmalloco ( pool  p,
int  size 
)
void pool_cleanup ( pool  p,
pool_cleaner  f,
void *  arg 
)
void pool_free ( pool  p  ) 
int pool_size ( pool  p  ) 

get the size of a memory pool

Parameters:
p the pool
Returns:
the size

References pool_struct::size.

Referenced by xstream_eat().

void pool_stat ( int  full  ) 

dummy implementation: print memory pool statistics (for debugging purposes, real implementation if POOL_DEBUG is defined)

Parameters:
full make a full report? (0 = no, 1 = yes)

Referenced by main().

char* pstrdup ( pool  p,
const char *  src 
)
char* pstrdupx ( pool  p,
const char *  src 
)

when pstrdup() is moved to "const char*", this one would actually return a new block

References pstrdup().


Generated on Thu Mar 11 06:00:05 2010 for jabberd14 by  doxygen 1.6.1