1 /* Test pools, subpools, memory allocation and so on.
2 * By Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_pool.c,v 1.5 2002/12/07 15:18:26 rich Exp $
28 /* Implement a simple malloc debugger -- which counts allocations and
29 * frees and ensures that all memory allocated by the pool code is
33 static void trace_init (void);
34 static void trace_finish (void);
35 static void *trace_malloc (size_t n, const char *filename, int line);
36 static void *trace_realloc (void *p, size_t n, const char *filename, int line);
37 static void trace_free (void *p);
39 #define malloc(n) trace_malloc ((n), __FILE__, __LINE__);
40 #define realloc(p,n) trace_realloc ((p), (n), __FILE__, __LINE__);
41 #define free(p) trace_free ((p));
43 #define NO_GLOBAL_POOL 1
48 static void test (void);
59 /* Perform the tests. */
68 for (i = 0; i < 1000; ++i)
72 prealloc (p, pmalloc (p, 200), 300);
74 prealloc (p, pmalloc (p, 1000), 1001);
75 prealloc (p, pmalloc (p, 900), 901);
87 simple_subpool_test (pool parent, int level)
92 if (level >= 8) return;
96 /* The following two statements must remain together. */
97 p = new_subpool (parent);
98 prealloc (p, prealloc (p, pmalloc (p, 16), 100),
100 /* End of realloc test. */
104 prealloc (p, pmalloc (p, 4), 8);
106 prealloc (parent, pmalloc (parent, 8), 8);
107 prealloc (parent, pmalloc (parent, 8), 16);
112 prealloc (p, pmalloc (p, 4), 8);
117 for (i = 0; i < 3; ++i)
119 simple_subpool_test (p, level+1);
121 if (parent) pmalloc (parent, 1);
124 if (parent == 0 || level == 4)
128 /* This is a regression test: we had reason to believe this didn't work
132 create_delete_test ()
140 static int cleanup_called = 0;
154 pool_register_cleanup_fn (p, cleanup_fn, 0);
156 assert (cleanup_called == 1);
159 sp1 = new_subpool (p);
160 sp2 = new_subpool (p);
161 pool_register_cleanup_fn (p, cleanup_fn, 0);
162 pool_register_cleanup_fn (sp1, cleanup_fn, 0);
163 pool_register_cleanup_fn (sp2, cleanup_fn, 0);
165 assert (cleanup_called == 2);
167 assert (cleanup_called == 4);
177 fd = open ("/dev/null", O_RDONLY);
178 pool_register_fd (p, fd);
181 assert (close (fd) == -1 && errno == EBADF);
185 cleanup_malloc_test ()
192 pool_register_malloc (p, m);
197 lots_of_pmalloc_test ()
203 /* This test kind of simulates what the chat server does, in an
204 * attempt to find the putative memory leak.
207 subpools = pcalloc (p, sizeof (struct pool), 100);
209 for (i = 0; i < 10000; ++i)
211 if (subpools[np] != 0)
212 delete_pool (subpools[np]);
213 subpools[np] = new_subpool (p);
215 for (j = 0; j < 100; ++j)
217 void *ptr = pmalloc (subpools[np], 100);
218 prealloc (subpools[np], ptr, 200);
222 if (np == 100) np = 0;
231 simple_alloc_test ();
232 simple_subpool_test (0, 0);
233 create_delete_test ();
236 cleanup_malloc_test ();
237 lots_of_pmalloc_test ();
240 /* The tracing code. */
245 static int nr_allocations = 0;
255 if (nr_allocations > 0)
257 fprintf (stderr, "*** error: %d allocation(s) leaked.\n",
264 trace_malloc (size_t n, const char *filename, int line)
271 trace_realloc (void *p, size_t n, const char *filename, int line)
273 return realloc (p, n);