test-smob-mark.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2013, 2014 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #if HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #undef NDEBUG
  22. #include <assert.h>
  23. #include <libguile.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #define SMOBS_COUNT (10000)
  27. struct x_tag
  28. {
  29. SCM scm_value;
  30. int c_value;
  31. };
  32. typedef struct x_tag x_t;
  33. unsigned int mark_call_count = 0;
  34. static scm_t_bits x_tag;
  35. static SCM make_x (void);
  36. static SCM mark_x (SCM x);
  37. static int print_x (SCM x, SCM port, scm_print_state * pstate);
  38. static size_t free_x (SCM x);
  39. static void init_smob_type (void);
  40. static void test_scm_smob_mark (void);
  41. static SCM
  42. make_x ()
  43. {
  44. static int i = 0;
  45. SCM s_x;
  46. x_t *c_x;
  47. i++;
  48. c_x = (x_t *) scm_gc_malloc (sizeof (x_t), "x");
  49. c_x->scm_value = scm_from_int (i);
  50. c_x->c_value = i;
  51. SCM_NEWSMOB (s_x, x_tag, c_x);
  52. return s_x;
  53. }
  54. static SCM
  55. mark_x (SCM x)
  56. {
  57. x_t *c_x;
  58. c_x = (x_t *) SCM_SMOB_DATA (x);
  59. scm_gc_mark (c_x->scm_value);
  60. mark_call_count++;
  61. return SCM_BOOL_F;
  62. }
  63. static size_t
  64. free_x (SCM x)
  65. {
  66. x_t *c_x;
  67. c_x = (x_t *) SCM_SMOB_DATA (x);
  68. scm_gc_free (c_x, sizeof (x_t), "x");
  69. c_x = NULL;
  70. return 0;
  71. }
  72. static int
  73. print_x (SCM x, SCM port, scm_print_state * pstate SCM_UNUSED)
  74. {
  75. x_t *c_x = (x_t *) SCM_SMOB_DATA (x);
  76. scm_puts ("#<x ", port);
  77. if (c_x == (x_t *) NULL)
  78. scm_puts ("(freed)", port);
  79. else
  80. scm_write (c_x->scm_value, port);
  81. scm_puts (">", port);
  82. return 1;
  83. }
  84. static void
  85. test_scm_smob_mark ()
  86. {
  87. int i;
  88. mark_call_count = 0;
  89. for (i = 0; i < SMOBS_COUNT; i++)
  90. make_x ();
  91. scm_gc ();
  92. if (mark_call_count < SMOBS_COUNT)
  93. {
  94. fprintf (stderr, "FAIL: SMOB mark function called for each SMOB\n");
  95. exit (EXIT_FAILURE);
  96. }
  97. }
  98. static void
  99. init_smob_type ()
  100. {
  101. x_tag = scm_make_smob_type ("x", sizeof (x_t));
  102. scm_set_smob_free (x_tag, free_x);
  103. scm_set_smob_print (x_tag, print_x);
  104. scm_set_smob_mark (x_tag, mark_x);
  105. }
  106. static void
  107. tests (void *data, int argc, char **argv)
  108. {
  109. init_smob_type ();
  110. test_scm_smob_mark ();
  111. }
  112. int
  113. main (int argc, char *argv[])
  114. {
  115. scm_boot_guile (argc, argv, tests, NULL);
  116. return 0;
  117. }