Commit | Line | Data |
---|---|---|
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 | ||
32 | struct x_tag | |
33 | { | |
34 | SCM scm_value; | |
35 | int c_value; | |
36 | }; | |
37 | ||
38 | typedef struct x_tag x_t; | |
39 | ||
40 | unsigned int mark_call_count = 0; | |
41 | ||
42 | static scm_t_bits x_tag; | |
43 | static SCM make_x (void); | |
44 | static SCM mark_x (SCM x); | |
45 | static int print_x (SCM x, SCM port, scm_print_state * pstate); | |
46 | static size_t free_x (SCM x); | |
47 | static void init_smob_type (void); | |
48 | static void test_scm_smob_mark (void); | |
49 | ||
50 | static SCM | |
51 | make_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 | ||
65 | static SCM | |
66 | mark_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 | ||
75 | static size_t | |
76 | free_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 | ||
85 | static int | |
86 | print_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 | ||
99 | static void | |
100 | test_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 | ||
114 | static void | |
115 | init_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 | ||
123 | static void | |
124 | tests (void *data, int argc, char **argv) | |
125 | { | |
126 | init_smob_type (); | |
127 | test_scm_smob_mark (); | |
128 | } | |
129 | ||
130 | int | |
131 | main (int argc, char *argv[]) | |
132 | { | |
133 | scm_boot_guile (argc, argv, tests, NULL); | |
134 | return 0; | |
135 | } |