Merge commit '5b7632331e7551ac202bbaba37c572b96a791c6e'
[bpt/guile.git] / test-suite / standalone / test-smob-mark.c
CommitLineData
0c1f2b0e 1/* Copyright (C) 2013, 2014 Free Software Foundation, Inc.
d2df3950
MG
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
19#if HAVE_CONFIG_H
20#include <config.h>
21#endif
22
0c1f2b0e
LC
23#undef NDEBUG
24
d2df3950
MG
25#include <assert.h>
26#include <libguile.h>
27#include <stdio.h>
28#include <stdlib.h>
29
30#define SMOBS_COUNT (10000)
31
32struct x_tag
33{
34 SCM scm_value;
35 int c_value;
36};
37
38typedef struct x_tag x_t;
39
40unsigned int mark_call_count = 0;
41
42static scm_t_bits x_tag;
43static SCM make_x (void);
44static SCM mark_x (SCM x);
45static int print_x (SCM x, SCM port, scm_print_state * pstate);
46static size_t free_x (SCM x);
47static void init_smob_type (void);
48static void test_scm_smob_mark (void);
49
50static SCM
51make_x ()
52{
53 static int i = 0;
54 SCM s_x;
55 x_t *c_x;
56
57 i++;
58 c_x = (x_t *) scm_gc_malloc (sizeof (x_t), "x");
59 c_x->scm_value = scm_from_int (i);
60 c_x->c_value = i;
61 SCM_NEWSMOB (s_x, x_tag, c_x);
62 return s_x;
63}
64
65static SCM
66mark_x (SCM x)
67{
68 x_t *c_x;
69 c_x = (x_t *) SCM_SMOB_DATA (x);
70 scm_gc_mark (c_x->scm_value);
71 mark_call_count++;
72 return SCM_BOOL_F;
73}
74
75static size_t
76free_x (SCM x)
77{
78 x_t *c_x;
79 c_x = (x_t *) SCM_SMOB_DATA (x);
80 scm_gc_free (c_x, sizeof (x_t), "x");
81 c_x = NULL;
82 return 0;
83}
84
85static int
86print_x (SCM x, SCM port, scm_print_state * pstate SCM_UNUSED)
87{
88 x_t *c_x = (x_t *) SCM_SMOB_DATA (x);
89 scm_puts ("#<x ", port);
90 if (c_x == (x_t *) NULL)
91 scm_puts ("(freed)", port);
92 else
93 scm_write (c_x->scm_value, port);
94 scm_puts (">", port);
95
96 return 1;
97}
98
99static void
100test_scm_smob_mark ()
101{
102 int i;
103 mark_call_count = 0;
104 for (i = 0; i < SMOBS_COUNT; i++)
105 make_x ();
106 scm_gc ();
107 if (mark_call_count < SMOBS_COUNT)
108 {
109 fprintf (stderr, "FAIL: SMOB mark function called for each SMOB\n");
110 exit (EXIT_FAILURE);
111 }
112}
113
114static void
115init_smob_type ()
116{
117 x_tag = scm_make_smob_type ("x", sizeof (x_t));
118 scm_set_smob_free (x_tag, free_x);
119 scm_set_smob_print (x_tag, print_x);
120 scm_set_smob_mark (x_tag, mark_x);
121}
122
123static void
124tests (void *data, int argc, char **argv)
125{
126 init_smob_type ();
127 test_scm_smob_mark ();
128}
129
130int
131main (int argc, char *argv[])
132{
133 scm_boot_guile (argc, argv, tests, NULL);
134 return 0;
135}