From e85c28ce148e0740697c76e9751b0d9cacbe67d1 Mon Sep 17 00:00:00 2001 From: blacksirius Date: Sun, 10 Jun 2012 16:04:24 +0000 Subject: - added some missing copyrights - merged (bs-coreoptimize->trunk) generic athena style configuration parser (raconf) - merged (bs-coreoptimize->trunk) threadsafe memory pool (mempool) [i need it for the new 'socket' system] - set svn:eol-style property on newer files were it was missing git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@16263 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/mempool.h | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/common/mempool.h (limited to 'src/common/mempool.h') diff --git a/src/common/mempool.h b/src/common/mempool.h new file mode 100644 index 000000000..aeaebe7fe --- /dev/null +++ b/src/common/mempool.h @@ -0,0 +1,100 @@ +#ifndef _rA_MEMPOOL_H_ +#define _rA_MEMPOOL_H_ + +#include "../common/cbasetypes.h" + +typedef struct mempool *mempool; + +typedef void (*memPoolOnNodeAllocationProc)(void *ptr); +typedef void (*memPoolOnNodeDeallocationProc)(void *ptr); + +typedef struct mempool_stats{ + int64 num_nodes_total; + int64 num_nodes_free; + int64 num_nodes_used; + + int64 num_segments; + int64 num_realloc_events; + + int64 peak_nodes_used; + + int64 num_bytes_total; +} mempool_stats; + + +// +void mempool_init(); +void mempool_final(); + + +/** + * Creates a new Mempool + * + * @param name - Name of the pool (used for debug / error messages) + * @param elem_size - size of each element + * @param initial_count - preallocation count + * @param realloc_count - #no of nodes being allocated when pool is running empty. + * @param onNodeAlloc - Node Allocation callback (see @note!) + * @param onNodeDealloc - Node Deallocation callback (see @note!) + * + * @note: + * The onNode(De)alloc callbacks are only called once during segment allocation + * (pool initialization / rallocation ) + * you can use this callbacks for example to initlaize a mutex or somethingelse + * you definitly need during runtime + * + * @return not NULL + */ +mempool mempool_create(const char *name, + uint64 elem_size, + uint64 initial_count, + uint64 realloc_count, + + memPoolOnNodeAllocationProc onNodeAlloc, + memPoolOnNodeDeallocationProc onNodeDealloc); + + +/** + * Destroys a Mempool + * + * @param pool - the mempool to destroy + * + * @note: + * Everything gets deallocated, regardless if everything was freed properly! + * So you have to ensure that all references are cleared properly! + */ +void mempool_destroy(mempool pool); + + +/** + * Gets a new / empty node from the given mempool. + * + * @param pool - the pool to get an empty node from. + * + * @return Address of empty Node + */ +void *mempool_node_get(mempool pool); + + +/** + * Returns the given node to the given mempool + * + * @param pool - the pool to put the node, to + * @param node - the node to return + */ +void mempool_node_put(mempool pool, void *node); + + +/** + * Returns Statistics for the given mempool + * + * @param pool - the pool to get thats for + * + * @note: i dont like pushing masses of values over the stack, too - but its lazy and okay for stats. (blacksirius) + * + * @return stats struct + */ +mempool_stats mempool_get_stats(mempool pool); + + +#endif -- cgit v1.2.3-60-g2f50