tst-mtrace.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Test program for mtrace.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <mcheck.h>
  16. #include <paths.h>
  17. #include <search.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. static void print (const void *node, VISIT value, int level);
  22. /* Used for several purposes. */
  23. static FILE *fp;
  24. static int
  25. do_test (void)
  26. {
  27. void *root = NULL;
  28. size_t linelen = 0;
  29. char *line = NULL;
  30. /* Enable memory usage tracing. */
  31. mtrace ();
  32. /* Perform some operations which definitely will allocate some
  33. memory. */
  34. fp = fopen (__FILE__, "r");
  35. if (fp == NULL)
  36. /* Shouldn't happen since this program is executed in the source
  37. directory. */
  38. abort ();
  39. while (!feof (fp))
  40. {
  41. char **p;
  42. char *copy;
  43. ssize_t n = getline (&line, &linelen, fp);
  44. if (n < 0)
  45. break;
  46. if (n == 0)
  47. continue;
  48. copy = strdup (line);
  49. if (copy == NULL)
  50. abort ();
  51. p = (char **) tsearch (copy, &root,
  52. (int (*)(const void *, const void *))strcmp);
  53. if (*p != copy)
  54. /* This line wasn't added. */
  55. free (copy);
  56. }
  57. fclose (fp);
  58. fp = fopen (_PATH_DEVNULL, "w");
  59. if (fp != NULL)
  60. {
  61. /* Write something through stdout. */
  62. twalk (root, print);
  63. fclose (fp);
  64. }
  65. /* Free everything. */
  66. tdestroy (root, free);
  67. /* Also the line buffer. */
  68. free (line);
  69. /* That's it. */
  70. return 0;
  71. }
  72. static void
  73. print (const void *node, VISIT value, int level)
  74. {
  75. static int cnt;
  76. if (value == postorder || value == leaf)
  77. fprintf (fp, "%3d: %s", ++cnt, *(const char **) node);
  78. }
  79. #define TEST_FUNCTION do_test ()
  80. #include "../test-skeleton.c"