jabberd2  2.2.16
util/pool.h
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Pool-based memory management routines.
00004  *
00005  * Copyright (c) 2002-2004 Jeremie Miller, Thomas Muldowney,
00006  *                         Ryan Eatmon, Robert Norris
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00021  */
00022 
00023 #ifndef INCL_UTIL_POOL_H
00024 #define INCL_UTIL_POOL_H 1
00025 
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029 
00030 /* jabberd2 Windows DLL */
00031 #ifndef JABBERD2_API
00032 # ifdef _WIN32
00033 #  ifdef JABBERD2_EXPORTS
00034 #   define JABBERD2_API  __declspec(dllexport)
00035 #  else /* JABBERD2_EXPORTS */
00036 #   define JABBERD2_API  __declspec(dllimport)
00037 #  endif /* JABBERD2_EXPORTS */
00038 # else /* _WIN32 */
00039 #  define JABBERD2_API extern
00040 # endif /* _WIN32 */
00041 #endif /* JABBERD2_API */
00042 
00043 #ifdef POOL_DEBUG
00044 /* prime number for top # of pools debugging */
00045 #define POOL_NUM 40009
00046 #endif
00047 
00053 typedef void (*pool_cleanup_t)(void *arg);
00054 
00058 struct pheap
00059 {
00060     void *block;
00061     int size, used;
00062 };
00063 
00068 struct pfree
00069 {
00070     pool_cleanup_t f;
00071     void *arg;
00072     struct pheap *heap;
00073     struct pfree *next;
00074 };
00075 
00080 typedef struct pool_struct
00081 {
00082     int size;
00083     struct pfree *cleanup;
00084     struct pfree *cleanup_tail;
00085     struct pheap *heap;
00086 #ifdef POOL_DEBUG
00087     char name[8], zone[32];
00088     int lsize;
00089 #endif
00090 } _pool, *pool_t;
00091 
00092 #ifdef POOL_DEBUG
00093 # define pool_new() _pool_new(__FILE__,__LINE__) 
00094 # define pool_heap(i) _pool_new_heap(i,__FILE__,__LINE__) 
00095 #else
00096 # define pool_heap(i) _pool_new_heap(i,NULL,0) 
00097 # define pool_new() _pool_new(NULL,0)
00098 #endif
00099 
00100 JABBERD2_API pool_t _pool_new(char *file, int line); /* new pool :) */
00101 JABBERD2_API pool_t _pool_new_heap(int size, char *file, int line); /* creates a new memory pool with an initial heap size */
00102 JABBERD2_API void *pmalloc(pool_t, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */
00103 JABBERD2_API void *pmalloc_x(pool_t p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */
00104 JABBERD2_API void *pmalloco(pool_t p, int size); /* YAPW for zeroing the block */
00105 JABBERD2_API char *pstrdup(pool_t p, const char *src); /* wrapper around strdup, gains mem from pool */
00106 JABBERD2_API char *pstrdupx(pool_t p, const char *src, int len); /* use given len */
00107 JABBERD2_API void pool_stat(int full); /* print to stderr the changed pools and reset */
00108 JABBERD2_API void pool_cleanup(pool_t p, pool_cleanup_t fn, void *arg); /* calls f(arg) before the pool is freed during cleanup */
00109 JABBERD2_API void pool_free(pool_t p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */
00110 JABBERD2_API int pool_size(pool_t p); /* returns total bytes allocated in this pool */
00111 
00112 
00113 #endif