2.9
[glibc/nacl-glibc.git] / malloc / tst-mtrace.c
blob78be74494c119c049bb0130e348bb3c0f5cb177c
1 /* Test program for mtrace.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <mcheck.h>
21 #include <paths.h>
22 #include <search.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
28 static void print (const void *node, VISIT value, int level);
30 /* Used for several purposes. */
31 static FILE *fp;
34 int
35 main (void)
37 void *root = NULL;
38 size_t linelen = 0;
39 char *line = NULL;
41 /* Enable memory usage tracing. */
42 mtrace ();
44 /* Perform some operations which definitely will allocate some
45 memory. */
46 fp = fopen (__FILE__, "r");
47 if (fp == NULL)
48 /* Shouldn't happen since this program is executed in the source
49 directory. */
50 abort ();
52 while (!feof (fp))
54 char **p;
55 char *copy;
56 ssize_t n = getline (&line, &linelen, fp);
58 if (n < 0)
59 break;
61 if (n == 0)
62 continue;
64 copy = strdup (line);
65 if (copy == NULL)
66 abort ();
68 p = (char **) tsearch (copy, &root,
69 (int (*) (const void *, const void *)) strcmp);
70 if (*p != copy)
71 /* This line wasn't added. */
72 free (copy);
75 fclose (fp);
77 fp = fopen (_PATH_DEVNULL, "w");
78 if (fp != NULL)
80 /* Write something through stdout. */
81 twalk (root, print);
83 fclose (fp);
86 /* Free everything. */
87 tdestroy (root, free);
89 /* Also the line buffer. */
90 free (line);
92 /* That's it. */
93 return 0;
97 static void
98 print (const void *node, VISIT value, int level)
100 static int cnt;
101 if (value == postorder || value == leaf)
102 fprintf (fp, "%3d: %s", ++cnt, *(const char **) node);