Mercurial > ~mikael > mcabber > hg
annotate mcabber/libjabber/pool.c @ 414:ec86d759ed54
Trailing whitespace cleanup
No real change.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Tue, 30 Aug 2005 22:26:19 +0200 |
parents | bf3d6e241714 |
children | c3ae9251c197 |
rev | line source |
---|---|
25 | 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/ | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
18 * |
25 | 19 * 2/27/00:3am, random plans by jer |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
20 * |
25 | 21 * ok based on gprof, we really need some innovation here... my thoughs are this: |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
22 * |
25 | 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) | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
29 * |
25 | 30 * let's see... this would change: |
31 * pstrdup: do a hash lookup, success=return, fail=pmalloc & hash put | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
32 * pool_free: |
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
33 * |
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
34 * |
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
35 * |
25 | 36 */ |
37 | |
38 #include "libxode.h" | |
39 | |
40 #ifdef POOL_DEBUG | |
41 int pool__total = 0; | |
42 int pool__ltotal = 0; | |
43 HASHTABLE pool__disturbed = NULL; | |
44 void *_pool__malloc(size_t size) | |
45 { | |
46 pool__total++; | |
47 return malloc(size); | |
48 } | |
49 void _pool__free(void *block) | |
50 { | |
51 pool__total--; | |
52 free(block); | |
53 } | |
54 #else | |
55 #define _pool__malloc malloc | |
56 #define _pool__free free | |
57 #endif | |
58 | |
59 | |
60 /* make an empty pool */ | |
61 pool _pool_new(char *zone) | |
62 { | |
63 pool p; | |
64 while((p = _pool__malloc(sizeof(_pool))) == NULL) sleep(1); | |
65 p->cleanup = NULL; | |
66 p->heap = NULL; | |
67 p->size = 0; | |
68 | |
69 #ifdef POOL_DEBUG | |
70 p->lsize = -1; | |
71 p->zone[0] = '\0'; | |
72 strcat(p->zone,zone); | |
73 sprintf(p->name,"%X",p); | |
74 | |
75 if(pool__disturbed == NULL) | |
76 pool__disturbed = ghash_create(POOL_DEBUG,(KEYHASHFUNC)str_hash_code,(KEYCOMPAREFUNC)j_strcmp); | |
77 ghash_put(pool__disturbed,p->name,p); | |
78 #endif | |
79 | |
80 return p; | |
81 } | |
82 | |
83 /* free a heap */ | |
84 void _pool_heap_free(void *arg) | |
85 { | |
86 struct pheap *h = (struct pheap *)arg; | |
87 | |
88 _pool__free(h->block); | |
89 _pool__free(h); | |
90 } | |
91 | |
92 /* mem should always be freed last */ | |
93 void _pool_cleanup_append(pool p, struct pfree *pf) | |
94 { | |
95 struct pfree *cur; | |
96 | |
97 if(p->cleanup == NULL) | |
98 { | |
99 p->cleanup = pf; | |
100 return; | |
101 } | |
102 | |
103 /* fast forward to end of list */ | |
104 for(cur = p->cleanup; cur->next != NULL; cur = cur->next); | |
105 | |
106 cur->next = pf; | |
107 } | |
108 | |
109 /* create a cleanup tracker */ | |
110 struct pfree *_pool_free(pool p, pool_cleaner f, void *arg) | |
111 { | |
112 struct pfree *ret; | |
113 | |
114 /* make the storage for the tracker */ | |
115 while((ret = _pool__malloc(sizeof(struct pfree))) == NULL) sleep(1); | |
116 ret->f = f; | |
117 ret->arg = arg; | |
118 ret->next = NULL; | |
119 | |
120 return ret; | |
121 } | |
122 | |
123 /* create a heap and make sure it get's cleaned up */ | |
124 struct pheap *_pool_heap(pool p, int size) | |
125 { | |
126 struct pheap *ret; | |
127 struct pfree *clean; | |
128 | |
129 /* make the return heap */ | |
130 while((ret = _pool__malloc(sizeof(struct pheap))) == NULL) sleep(1); | |
131 while((ret->block = _pool__malloc(size)) == NULL) sleep(1); | |
132 ret->size = size; | |
133 p->size += size; | |
134 ret->used = 0; | |
135 | |
136 /* append to the cleanup list */ | |
137 clean = _pool_free(p, _pool_heap_free, (void *)ret); | |
138 clean->heap = ret; /* for future use in finding used mem for pstrdup */ | |
139 _pool_cleanup_append(p, clean); | |
140 | |
141 return ret; | |
142 } | |
143 | |
144 pool _pool_new_heap(int size, char *zone) | |
145 { | |
146 pool p; | |
147 p = _pool_new(zone); | |
148 p->heap = _pool_heap(p,size); | |
149 return p; | |
150 } | |
151 | |
152 void *pmalloc(pool p, int size) | |
153 { | |
154 void *block; | |
155 | |
156 if(p == NULL) | |
157 { | |
158 fprintf(stderr,"Memory Leak! [pmalloc received NULL pool, unable to track allocation, exiting]\n"); | |
159 abort(); | |
160 } | |
161 | |
162 /* if there is no heap for this pool or it's a big request, just raw, I like how we clean this :) */ | |
163 if(p->heap == NULL || size > (p->heap->size / 2)) | |
164 { | |
165 while((block = _pool__malloc(size)) == NULL) sleep(1); | |
166 p->size += size; | |
167 _pool_cleanup_append(p, _pool_free(p, _pool__free, block)); | |
168 return block; | |
169 } | |
170 | |
171 /* we have to preserve boundaries, long story :) */ | |
172 if(size >= 4) | |
173 while(p->heap->used&7) p->heap->used++; | |
174 | |
175 /* if we don't fit in the old heap, replace it */ | |
176 if(size > (p->heap->size - p->heap->used)) | |
177 p->heap = _pool_heap(p, p->heap->size); | |
178 | |
179 /* the current heap has room */ | |
180 block = (char *)p->heap->block + p->heap->used; | |
181 p->heap->used += size; | |
182 return block; | |
183 } | |
184 | |
185 void *pmalloc_x(pool p, int size, char c) | |
186 { | |
187 void* result = pmalloc(p, size); | |
188 if (result != NULL) | |
189 memset(result, c, size); | |
190 return result; | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
191 } |
25 | 192 |
193 /* easy safety utility (for creating blank mem for structs, etc) */ | |
194 void *pmalloco(pool p, int size) | |
195 { | |
196 void *block = pmalloc(p, size); | |
197 memset(block, 0, size); | |
198 return block; | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
199 } |
25 | 200 |
201 /* 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 */ | |
202 char *pstrdup(pool p, const char *src) | |
203 { | |
204 char *ret; | |
205 | |
206 if(src == NULL) | |
207 return NULL; | |
208 | |
209 ret = pmalloc(p,strlen(src) + 1); | |
210 strcpy(ret,src); | |
211 | |
212 return ret; | |
213 } | |
214 | |
215 /* when move above, this one would actually return a new block */ | |
216 char *pstrdupx(pool p, const char *src) | |
217 { | |
218 return pstrdup(p, src); | |
219 } | |
220 | |
221 int pool_size(pool p) | |
222 { | |
223 if(p == NULL) return 0; | |
224 | |
225 return p->size; | |
226 } | |
227 | |
228 void pool_free(pool p) | |
229 { | |
230 struct pfree *cur, *stub; | |
231 | |
232 if(p == NULL) return; | |
233 | |
234 cur = p->cleanup; | |
235 while(cur != NULL) | |
236 { | |
237 (*cur->f)(cur->arg); | |
238 stub = cur->next; | |
239 _pool__free(cur); | |
240 cur = stub; | |
241 } | |
242 | |
243 #ifdef POOL_DEBUG | |
244 ghash_remove(pool__disturbed,p->name); | |
245 #endif | |
246 | |
247 _pool__free(p); | |
248 | |
249 } | |
250 | |
251 /* public cleanup utils, insert in a way that they are run FIFO, before mem frees */ | |
252 void pool_cleanup(pool p, pool_cleaner f, void *arg) | |
253 { | |
254 struct pfree *clean; | |
255 | |
256 clean = _pool_free(p, f, arg); | |
257 clean->next = p->cleanup; | |
258 p->cleanup = clean; | |
259 } | |
260 | |
261 #ifdef POOL_DEBUG | |
262 void debug_log(char *zone, const char *msgfmt, ...); | |
263 int _pool_stat(void *arg, const void *key, void *data) | |
264 { | |
265 pool p = (pool)data; | |
266 | |
267 if(p->lsize == -1) | |
268 debug_log("leak","%s: %X is a new pool",p->zone,p->name); | |
269 else if(p->size > p->lsize) | |
270 debug_log("leak","%s: %X grew %d",p->zone,p->name, p->size - p->lsize); | |
271 else if((int)arg) | |
272 debug_log("leak","%s: %X exists %d",p->zone,p->name, p->size); | |
273 p->lsize = p->size; | |
274 return 1; | |
275 } | |
276 | |
277 void pool_stat(int full) | |
278 { | |
279 ghash_walk(pool__disturbed,_pool_stat,(void *)full); | |
280 if(pool__total != pool__ltotal) | |
281 debug_log("leak","%d\ttotal missed mallocs",pool__total); | |
282 pool__ltotal = pool__total; | |
283 return; | |
284 } | |
285 #else | |
286 void pool_stat(int full) | |
287 { | |
288 return; | |
289 } | |
290 #endif |