summaryrefslogtreecommitdiff
path: root/test-suite/standalone/test-smob-mark.c
blob: d9db9a651d6a8a93b467e8ee5080a55de0651760 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (C) 2013 Free Software Foundation, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <libguile.h>
#include <stdio.h>
#include <stdlib.h>

#define SMOBS_COUNT (10000)

struct x_tag
{
  SCM scm_value;
  int c_value;
};

typedef struct x_tag x_t;

unsigned int mark_call_count = 0;

static scm_t_bits x_tag;
static SCM make_x (void);
static SCM mark_x (SCM x);
static int print_x (SCM x, SCM port, scm_print_state * pstate);
static size_t free_x (SCM x);
static void init_smob_type (void);
static void test_scm_smob_mark (void);

static SCM
make_x ()
{
  static int i = 0;
  SCM s_x;
  x_t *c_x;

  i++;
  c_x = (x_t *) scm_gc_malloc (sizeof (x_t), "x");
  c_x->scm_value = scm_from_int (i);
  c_x->c_value = i;
  SCM_NEWSMOB (s_x, x_tag, c_x);
  return s_x;
}

static SCM
mark_x (SCM x)
{
  x_t *c_x;
  c_x = (x_t *) SCM_SMOB_DATA (x);
  scm_gc_mark (c_x->scm_value);
  mark_call_count++;
  return SCM_BOOL_F;
}

static size_t
free_x (SCM x)
{
  x_t *c_x;
  c_x = (x_t *) SCM_SMOB_DATA (x);
  scm_gc_free (c_x, sizeof (x_t), "x");
  c_x = NULL;
  return 0;
}

static int
print_x (SCM x, SCM port, scm_print_state * pstate SCM_UNUSED)
{
  x_t *c_x = (x_t *) SCM_SMOB_DATA (x);
  scm_puts ("#<x ", port);
  if (c_x == (x_t *) NULL)
    scm_puts ("(freed)", port);
  else
    scm_write (c_x->scm_value, port);
  scm_puts (">", port);

  return 1;
}

static void
test_scm_smob_mark ()
{
  int i;
  mark_call_count = 0;
  for (i = 0; i < SMOBS_COUNT; i++)
    make_x ();
  scm_gc ();
  if (mark_call_count < SMOBS_COUNT)
    {
      fprintf (stderr, "FAIL: SMOB mark function called for each SMOB\n");
      exit (EXIT_FAILURE);
    }
}

static void
init_smob_type ()
{
  x_tag = scm_make_smob_type ("x", sizeof (x_t));
  scm_set_smob_free (x_tag, free_x);
  scm_set_smob_print (x_tag, print_x);
  scm_set_smob_mark (x_tag, mark_x);
}

static void
tests (void *data, int argc, char **argv)
{
  init_smob_type ();
  test_scm_smob_mark ();
}

int
main (int argc, char *argv[])
{
  scm_boot_guile (argc, argv, tests, NULL);
  return 0;
}