]> git.ipfire.org Git - thirdparty/glibc.git/blame - malloc/tst-mtrace.c
Implement allocation buffers for internal use
[thirdparty/glibc.git] / malloc / tst-mtrace.c
CommitLineData
3eea1bc9 1/* Test program for mtrace.
bfff8b1b 2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3eea1bc9
UD
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
41bdb6e2
AJ
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.
3eea1bc9
UD
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
41bdb6e2 13 Lesser General Public License for more details.
3eea1bc9 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
3eea1bc9
UD
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
27static void print (const void *node, VISIT value, int level);
28
29/* Used for several purposes. */
30static FILE *fp;
31
32
29955b5d
AS
33static int
34do_test (void)
3eea1bc9
UD
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)
6c8dbf00 58 break;
3eea1bc9
UD
59
60 if (n == 0)
6c8dbf00 61 continue;
3eea1bc9
UD
62
63 copy = strdup (line);
64 if (copy == NULL)
6c8dbf00 65 abort ();
3eea1bc9
UD
66
67 p = (char **) tsearch (copy, &root,
6c8dbf00 68 (int (*)(const void *, const void *))strcmp);
3eea1bc9 69 if (*p != copy)
6c8dbf00
OB
70 /* This line wasn't added. */
71 free (copy);
3eea1bc9
UD
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
96static void
97print (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}
29955b5d
AS
103
104#define TEST_FUNCTION do_test ()
105#include "../test-skeleton.c"