comparison src/protocols/jabber/pool.c @ 2086:424a40f12a6c

[gaim-migrate @ 2096] moving protocols from plugins/ to src/protocols. making it so that you can select which protocols are compiled statically. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Tue, 31 Jul 2001 01:00:39 +0000
parents
children 4e7cefc55971
comparison
equal deleted inserted replaced
2085:7ebb4322f89b 2086:424a40f12a6c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Jabber
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
18 *
19 * 2/27/00:3am, random plans by jer
20 *
21 * ok based on gprof, we really need some innovation here... my thoughs are this:
22 *
23 * most things are strings, so have a string-based true-blue garbage collector
24 * one big global hash containing all the strings created by any pstrdup, returning const char *
25 * a refcount on each string block
26 * when a pool is freed, it moves down the refcount
27 * garbage collector collects pools on the free stack, and runs through the hash for unused strings
28 * j_strcmp can check for == (if they are both from a pstrdup)
29 *
30 * let's see... this would change:
31 * pstrdup: do a hash lookup, success=return, fail=pmalloc & hash put
32 * pool_free:
33 *
34 *
35 *
36 *
37 *
38 */
39
40 #include "libxode.h"
41 #include "config.h"
42
43
44 #ifdef POOL_DEBUG
45 int pool__total = 0;
46 int pool__ltotal = 0;
47 HASHTABLE pool__disturbed = NULL;
48 void *_pool__malloc(size_t size)
49 {
50 pool__total++;
51 return malloc(size);
52 }
53 void _pool__free(void *block)
54 {
55 pool__total--;
56 free(block);
57 }
58 #else
59 #define _pool__malloc malloc
60 #define _pool__free free
61 #endif
62
63
64 /* make an empty pool */
65 pool _pool_new(char *zone)
66 {
67 pool p;
68 while((p = _pool__malloc(sizeof(_pool))) == NULL) sleep(1);
69 p->cleanup = NULL;
70 p->heap = NULL;
71 p->size = 0;
72
73 #ifdef POOL_DEBUG
74 p->lsize = -1;
75 p->zone[0] = '\0';
76 strcat(p->zone,zone);
77 sprintf(p->name,"%X",p);
78
79 if(pool__disturbed == NULL)
80 pool__disturbed = ghash_create(POOL_DEBUG,(KEYHASHFUNC)str_hash_code,(KEYCOMPAREFUNC)j_strcmp);
81 ghash_put(pool__disturbed,p->name,p);
82 #endif
83
84 return p;
85 }
86
87 /* free a heap */
88 void _pool_heap_free(void *arg)
89 {
90 struct pheap *h = (struct pheap *)arg;
91
92 _pool__free(h->block);
93 _pool__free(h);
94 }
95
96 /* mem should always be freed last */
97 void _pool_cleanup_append(pool p, struct pfree *pf)
98 {
99 struct pfree *cur;
100
101 if(p->cleanup == NULL)
102 {
103 p->cleanup = pf;
104 return;
105 }
106
107 /* fast forward to end of list */
108 for(cur = p->cleanup; cur->next != NULL; cur = cur->next);
109
110 cur->next = pf;
111 }
112
113 /* create a cleanup tracker */
114 struct pfree *_pool_free(pool p, pool_cleaner f, void *arg)
115 {
116 struct pfree *ret;
117
118 /* make the storage for the tracker */
119 while((ret = _pool__malloc(sizeof(struct pfree))) == NULL) sleep(1);
120 ret->f = f;
121 ret->arg = arg;
122 ret->next = NULL;
123
124 return ret;
125 }
126
127 /* create a heap and make sure it get's cleaned up */
128 struct pheap *_pool_heap(pool p, int size)
129 {
130 struct pheap *ret;
131 struct pfree *clean;
132
133 /* make the return heap */
134 while((ret = _pool__malloc(sizeof(struct pheap))) == NULL) sleep(1);
135 while((ret->block = _pool__malloc(size)) == NULL) sleep(1);
136 ret->size = size;
137 p->size += size;
138 ret->used = 0;
139
140 /* append to the cleanup list */
141 clean = _pool_free(p, _pool_heap_free, (void *)ret);
142 clean->heap = ret; /* for future use in finding used mem for pstrdup */
143 _pool_cleanup_append(p, clean);
144
145 return ret;
146 }
147
148 pool _pool_new_heap(int size, char *zone)
149 {
150 pool p;
151 p = _pool_new(zone);
152 p->heap = _pool_heap(p,size);
153 return p;
154 }
155
156 void *pmalloc(pool p, int size)
157 {
158 void *block;
159
160 if(p == NULL)
161 {
162 fprintf(stderr,"Memory Leak! [pmalloc received NULL pool, unable to track allocation, exiting]\n");
163 abort();
164 }
165
166 /* if there is no heap for this pool or it's a big request, just raw, I like how we clean this :) */
167 if(p->heap == NULL || size > (p->heap->size / 2))
168 {
169 while((block = _pool__malloc(size)) == NULL) sleep(1);
170 p->size += size;
171 _pool_cleanup_append(p, _pool_free(p, _pool__free, block));
172 return block;
173 }
174
175 /* we have to preserve boundaries, long story :) */
176 if(size >= 4)
177 while(p->heap->used&7) p->heap->used++;
178
179 /* if we don't fit in the old heap, replace it */
180 if(size > (p->heap->size - p->heap->used))
181 p->heap = _pool_heap(p, p->heap->size);
182
183 /* the current heap has room */
184 block = (char *)p->heap->block + p->heap->used;
185 p->heap->used += size;
186 return block;
187 }
188
189 void *pmalloc_x(pool p, int size, char c)
190 {
191 void* result = pmalloc(p, size);
192 if (result != NULL)
193 memset(result, c, size);
194 return result;
195 }
196
197 /* easy safety utility (for creating blank mem for structs, etc) */
198 void *pmalloco(pool p, int size)
199 {
200 void *block = pmalloc(p, size);
201 memset(block, 0, size);
202 return block;
203 }
204
205 /* XXX efficient: move this to const char * and then loop throug the existing heaps to see if src is within a block in this pool */
206 char *pstrdup(pool p, const char *src)
207 {
208 char *ret;
209
210 if(src == NULL)
211 return NULL;
212
213 ret = pmalloc(p,strlen(src) + 1);
214 strcpy(ret,src);
215
216 return ret;
217 }
218
219 /* when move above, this one would actually return a new block */
220 char *pstrdupx(pool p, const char *src)
221 {
222 return pstrdup(p, src);
223 }
224
225 int pool_size(pool p)
226 {
227 if(p == NULL) return 0;
228
229 return p->size;
230 }
231
232 void pool_free(pool p)
233 {
234 struct pfree *cur, *stub;
235
236 if(p == NULL) return;
237
238 cur = p->cleanup;
239 while(cur != NULL)
240 {
241 (*cur->f)(cur->arg);
242 stub = cur->next;
243 _pool__free(cur);
244 cur = stub;
245 }
246
247 #ifdef POOL_DEBUG
248 ghash_remove(pool__disturbed,p->name);
249 #endif
250
251 _pool__free(p);
252
253 }
254
255 /* public cleanup utils, insert in a way that they are run FIFO, before mem frees */
256 void pool_cleanup(pool p, pool_cleaner f, void *arg)
257 {
258 struct pfree *clean;
259
260 clean = _pool_free(p, f, arg);
261 clean->next = p->cleanup;
262 p->cleanup = clean;
263 }
264
265 #ifdef POOL_DEBUG
266 void debug_log(char *zone, const char *msgfmt, ...);
267 int _pool_stat(void *arg, const void *key, void *data)
268 {
269 pool p = (pool)data;
270
271 if(p->lsize == -1)
272 debug_log("leak","%s: %X is a new pool",p->zone,p->name);
273 else if(p->size > p->lsize)
274 debug_log("leak","%s: %X grew %d",p->zone,p->name, p->size - p->lsize);
275 else if((int)arg)
276 debug_log("leak","%s: %X exists %d",p->zone,p->name, p->size);
277 p->lsize = p->size;
278 return 1;
279 }
280
281 void pool_stat(int full)
282 {
283 ghash_walk(pool__disturbed,_pool_stat,(void *)full);
284 if(pool__total != pool__ltotal)
285 debug_log("leak","%d\ttotal missed mallocs",pool__total);
286 pool__ltotal = pool__total;
287 return;
288 }
289 #else
290 void pool_stat(int full)
291 {
292 return;
293 }
294 #endif