Datasets:

License:
File size: 2,644 Bytes
3dcad1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* test-foreign-object-c.c - exercise C foreign object interface */

/* Copyright 2014,2018
     Free Software Foundation, Inc.

   This file is part of Guile.

   Guile 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.

   Guile 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 Guile.  If not, see
   <https://www.gnu.org/licenses/>.  */

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

#include <libguile.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

enum
  {
    CSTR_SLOT_ADDR,
    CSTR_SLOT_LEN,
    CSTR_SLOT_COUNT
  };

static void
finalizer (SCM obj)
{
  free (scm_foreign_object_ref (obj, CSTR_SLOT_ADDR));
}

static SCM
make_cstr_from_static (SCM type, const char *str)
{
  char *ours = strdup (str);

  if (!ours)
    abort ();

  return scm_make_foreign_object_2 (type, ours, (void *) strlen (ours));
}

static int
cstr_equals_static_p (SCM cstr, const char *str)
{
  const char *addr;
  size_t len;

  addr = scm_foreign_object_ref (cstr, CSTR_SLOT_ADDR);
  len = scm_foreign_object_unsigned_ref (cstr, CSTR_SLOT_LEN);

  if (strlen (str) != len)
    return 0;

  return strncmp (addr, str, len) == 0;
}

static void
test_scm_foreign_object (void)
{
  SCM type_name, slot_names, type, cstr;

  type_name = scm_from_utf8_symbol ("<cstr>");
  slot_names = scm_list_2 (scm_from_utf8_symbol ("addr"),
                           scm_from_utf8_symbol ("len"));
  type = scm_make_foreign_object_type (type_name, slot_names, finalizer);

  cstr = make_cstr_from_static (type, "Hello, world!");
  scm_assert_foreign_object_type (type, cstr);

  if (!cstr_equals_static_p (cstr, "Hello, world!"))
    {
      fprintf (stderr, "fail: test-foreign-object 1\n");
      exit (EXIT_FAILURE);
    }

  {
    int i;
    for (i = 0; i < 5000; i++)
      cstr = make_cstr_from_static (type, "Hello, world!");
    cstr = SCM_BOOL_F;
  }

  scm_gc ();
  scm_gc ();
  scm_gc ();

  /* Allow time for the finalizer thread to run.  */
  scm_usleep (scm_from_uint (50 * 1000));
}

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

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