malloc: Add posix_memalign test.
[platform/upstream/glibc.git] / malloc / tst-mtrace.c
1 /* Test program for mtrace.
2    Copyright (C) 2000-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <mcheck.h>
20 #include <paths.h>
21 #include <search.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26
27 static void print (const void *node, VISIT value, int level);
28
29 /* Used for several purposes.  */
30 static FILE *fp;
31
32
33 int
34 main (void)
35 {
36   void *root = NULL;
37   size_t linelen = 0;
38   char *line = NULL;
39
40   /* Enable memory usage tracing.  */
41   mtrace ();
42
43   /* Perform some operations which definitely will allocate some
44      memory.  */
45   fp = fopen (__FILE__, "r");
46   if (fp == NULL)
47     /* Shouldn't happen since this program is executed in the source
48        directory.  */
49     abort ();
50
51   while (!feof (fp))
52     {
53       char **p;
54       char *copy;
55       ssize_t n = getline (&line, &linelen, fp);
56
57       if (n < 0)
58         break;
59
60       if (n == 0)
61         continue;
62
63       copy = strdup (line);
64       if (copy == NULL)
65         abort ();
66
67       p = (char **) tsearch (copy, &root,
68                              (int (*) (const void *, const void *)) strcmp);
69       if (*p != copy)
70         /* This line wasn't added.  */
71         free (copy);
72     }
73
74   fclose (fp);
75
76   fp = fopen (_PATH_DEVNULL, "w");
77   if (fp != NULL)
78     {
79       /* Write something through stdout.  */
80       twalk (root, print);
81
82       fclose (fp);
83     }
84
85   /* Free everything.  */
86   tdestroy (root, free);
87
88   /* Also the line buffer.  */
89   free (line);
90
91   /* That's it.  */
92   return 0;
93 }
94
95
96 static void
97 print (const void *node, VISIT value, int level)
98 {
99   static int cnt;
100   if (value == postorder || value == leaf)
101     fprintf (fp, "%3d: %s", ++cnt, *(const char **) node);
102 }