mempool-allocfree.h revision 66251e6ab31e5cc153fe5cae608e416dacafe9cd
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen#ifndef __MEMPOOL_H
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#define __MEMPOOL_H
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#include "macros.h"
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen
9de5eb9e1ac3a07c4197a60fdefd412d6cc78eb2Timo Sirainen/* #define POOL_CHECK_LEAKS */
db3b95d5a33ddce552d41136ae68d7331f8bf5feTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen/* Memory allocated and reallocated (the new data in it) in pools is always
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen zeroed, it will cost only a few CPU cycles and may well save some debug
7a60e1dc9e93ef3f7c7fe1af6385a0bfa1e31bc3Timo Sirainen time. */
fe8af34153615d9007f2238fca87df11ff32d614Timo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainentypedef struct Pool *Pool;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo SirainenPool pool_create(const char *name, unsigned int size);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid pool_ref(Pool pool);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid pool_unref(Pool pool);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
db3b95d5a33ddce552d41136ae68d7331f8bf5feTimo Sirainen#define p_new(pool, type, count) \
db3b95d5a33ddce552d41136ae68d7331f8bf5feTimo Sirainen ((type *) p_malloc(pool, (unsigned) sizeof(type) * (count)))
db3b95d5a33ddce552d41136ae68d7331f8bf5feTimo Sirainenvoid *p_malloc(Pool pool, unsigned int size);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid p_free(Pool pool, void *mem);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#define p_free_and_null(pool, rec) \
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen STMT_START { \
db3b95d5a33ddce552d41136ae68d7331f8bf5feTimo Sirainen p_free(pool, rec); \
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen (rec) = NULL; \
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen } STMT_END
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
db3b95d5a33ddce552d41136ae68d7331f8bf5feTimo Sirainen/* p_free_clean() should be used when pool is being destroyed, so freeing
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen memory isn't needed for anything else than detecting memory leaks. */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#ifdef POOL_CHECK_LEAKS
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# define p_free_clean(pool, mem) p_free(pool, mem)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#else
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen# define p_free_clean(pool, mem)
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen#endif
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen/* reallocate the `mem' to be exactly `size' */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid *p_realloc(Pool pool, void *mem, unsigned int size);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen/* reallocate the `mem' to be at least `size' if it wasn't previously */
152db3f90f298b7fb2dbbd4276f0fc30a9bc30f6Timo Sirainenvoid *p_realloc_min(Pool pool, void *mem, unsigned int size);
0dc72981f5286d60ca9233f6ac7c444d393d24fbTimo Sirainen
0dc72981f5286d60ca9233f6ac7c444d393d24fbTimo Sirainen/* Clear the pool. Memory allocated from pool before this call must not be
009217abb57a24a4076092e8e4e165545747839eStephan Bosch used after. */
009217abb57a24a4076092e8e4e165545747839eStephan Boschvoid p_clear(Pool pool);
0dc72981f5286d60ca9233f6ac7c444d393d24fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#endif
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen