jabberd2  2.3.1
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(const char *file, int line); /* new pool :) */
101 JABBERD2_API pool_t _pool_new_heap(int size, const 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
void pool_free(pool_t p)
Definition: pool.c:226
void * block
Definition: pool.h:60
void * pmalloc(pool_t p, int size)
Definition: pool.c:141
int size
Definition: pool.h:61
pool_t _pool_new(const char *zone, int line)
make an empty pool
Definition: pool.c:45
pool_cleanup_t f
Definition: pool.h:70
pfree - a linked list node which stores an allocation chunk, plus a callback
Definition: pool.h:68
struct pool_struct _pool
pool - base node for a pool.
void pool_cleanup(pool_t p, pool_cleanup_t f, void *arg)
public cleanup utils, insert in a way that they are run FIFO, before mem frees
Definition: pool.c:251
int pool_size(pool_t p)
Definition: pool.c:219
#define JABBERD2_API
Definition: pool.h:39
void * pmalloco(pool_t p, int size)
easy safety utility (for creating blank mem for structs, etc)
Definition: pool.c:183
char * pstrdupx(pool_t p, const char *src, int len)
use given size
Definition: pool.c:205
void pool_stat(int full)
Definition: pool.c:285
struct pool_struct * pool_t
struct pfree * cleanup
Definition: pool.h:83
int size
Definition: pool.h:82
struct pfree * cleanup_tail
Definition: pool.h:84
struct pheap * heap
Definition: pool.h:85
struct pheap * heap
Definition: pool.h:72
struct pfree * next
Definition: pool.h:73
int used
Definition: pool.h:61
pheap - singular allocation of memory
Definition: pool.h:58
char * pstrdup(pool_t p, const char *src)
XXX efficient: move this to const char * and then loop throug the existing heaps to see if src is wit...
Definition: pool.c:191
pool_t _pool_new_heap(int size, const char *zone, int line)
Definition: pool.c:133
void * arg
Definition: pool.h:71
void * pmalloc_x(pool_t p, int size, char c)
Definition: pool.c:174
pool - base node for a pool.
Definition: pool.h:80
void(* pool_cleanup_t)(void *arg)
pool_cleanup_t - callback type which is associated with a pool entry; invoked when the pool entry is ...
Definition: pool.h:53