jabberd2  2.2.17
pool.h
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Pool-based memory management routines.
4  *
5  * Copyright (c) 2002-2004 Jeremie Miller, Thomas Muldowney,
6  * Ryan Eatmon, Robert Norris
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
21  */
22 
23 #ifndef INCL_UTIL_POOL_H
24 #define INCL_UTIL_POOL_H 1
25 
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 
30 /* jabberd2 Windows DLL */
31 #ifndef JABBERD2_API
32 # ifdef _WIN32
33 # ifdef JABBERD2_EXPORTS
34 # define JABBERD2_API __declspec(dllexport)
35 # else /* JABBERD2_EXPORTS */
36 # define JABBERD2_API __declspec(dllimport)
37 # endif /* JABBERD2_EXPORTS */
38 # else /* _WIN32 */
39 # define JABBERD2_API extern
40 # endif /* _WIN32 */
41 #endif /* JABBERD2_API */
42 
43 #ifdef POOL_DEBUG
44 /* prime number for top # of pools debugging */
45 #define POOL_NUM 40009
46 #endif
47 
53 typedef void (*pool_cleanup_t)(void *arg);
54 
58 struct pheap
59 {
60  void *block;
61  int size, used;
62 };
63 
68 struct pfree
69 {
71  void *arg;
72  struct pheap *heap;
73  struct pfree *next;
74 };
75 
80 typedef struct pool_struct
81 {
82  int size;
83  struct pfree *cleanup;
85  struct pheap *heap;
86 #ifdef POOL_DEBUG
87  char name[8], zone[32];
88  int lsize;
89 #endif
90 } _pool, *pool_t;
91 
92 #ifdef POOL_DEBUG
93 # define pool_new() _pool_new(__FILE__,__LINE__)
94 # define pool_heap(i) _pool_new_heap(i,__FILE__,__LINE__)
95 #else
96 # define pool_heap(i) _pool_new_heap(i,NULL,0)
97 # define pool_new() _pool_new(NULL,0)
98 #endif
99 
100 JABBERD2_API pool_t _pool_new(char *file, int line); /* new pool :) */
101 JABBERD2_API pool_t _pool_new_heap(int size, char *file, int line); /* creates a new memory pool with an initial heap size */
102 JABBERD2_API void *pmalloc(pool_t, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */
103 JABBERD2_API void *pmalloc_x(pool_t p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */
104 JABBERD2_API void *pmalloco(pool_t p, int size); /* YAPW for zeroing the block */
105 JABBERD2_API char *pstrdup(pool_t p, const char *src); /* wrapper around strdup, gains mem from pool */
106 JABBERD2_API char *pstrdupx(pool_t p, const char *src, int len); /* use given len */
107 JABBERD2_API void pool_stat(int full); /* print to stderr the changed pools and reset */
108 JABBERD2_API void pool_cleanup(pool_t p, pool_cleanup_t fn, void *arg); /* calls f(arg) before the pool is freed during cleanup */
109 JABBERD2_API void pool_free(pool_t p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */
110 JABBERD2_API int pool_size(pool_t p); /* returns total bytes allocated in this pool */
111 
112 
113 #endif