Back to index

glibc  2.9
tst-mtrace.c
Go to the documentation of this file.
00001 /* Test program for mtrace.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <mcheck.h>
00021 #include <paths.h>
00022 #include <search.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 
00028 static void print (const void *node, VISIT value, int level);
00029 
00030 /* Used for several purposes.  */
00031 static FILE *fp;
00032 
00033 
00034 int
00035 main (void)
00036 {
00037   void *root = NULL;
00038   size_t linelen = 0;
00039   char *line = NULL;
00040 
00041   /* Enable memory usage tracing.  */
00042   mtrace ();
00043 
00044   /* Perform some operations which definitely will allocate some
00045      memory.  */
00046   fp = fopen (__FILE__, "r");
00047   if (fp == NULL)
00048     /* Shouldn't happen since this program is executed in the source
00049        directory.  */
00050     abort ();
00051 
00052   while (!feof (fp))
00053     {
00054       char **p;
00055       char *copy;
00056       ssize_t n = getline (&line, &linelen, fp);
00057 
00058       if (n < 0)
00059        break;
00060 
00061       if (n == 0)
00062        continue;
00063 
00064       copy = strdup (line);
00065       if (copy == NULL)
00066        abort ();
00067 
00068       p = (char **) tsearch (copy, &root,
00069                           (int (*) (const void *, const void *)) strcmp);
00070       if (*p != copy)
00071        /* This line wasn't added.  */
00072        free (copy);
00073     }
00074 
00075   fclose (fp);
00076 
00077   fp = fopen (_PATH_DEVNULL, "w");
00078   if (fp != NULL)
00079     {
00080       /* Write something through stdout.  */
00081       twalk (root, print);
00082 
00083       fclose (fp);
00084     }
00085 
00086   /* Free everything.  */
00087   tdestroy (root, free);
00088 
00089   /* Also the line buffer.  */
00090   free (line);
00091 
00092   /* That's it.  */
00093   return 0;
00094 }
00095 
00096 
00097 static void
00098 print (const void *node, VISIT value, int level)
00099 {
00100   static int cnt;
00101   if (value == postorder || value == leaf)
00102     fprintf (fp, "%3d: %s", ++cnt, *(const char **) node);
00103 }