-/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
- *
+/* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2006,
+ * 2009, 2010, 2011, 2012, 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
#endif
#include <stdio.h>
+#include <stdlib.h>
#include <errno.h>
#include "libguile/_scm.h"
#include "libguile/async.h"
-#include "libguile/objects.h"
#include "libguile/goops.h"
-#include "libguile/ports.h"
-
-#ifdef HAVE_MALLOC_H
-#include <malloc.h>
-#endif
+#include "libguile/instructions.h"
+#include "libguile/objcodes.h"
+#include "libguile/programs.h"
#include "libguile/smob.h"
-#include "libguile/boehm-gc.h"
+#include "libguile/bdw-gc.h"
#include <gc/gc_mark.h>
long scm_numsmob;
scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
-/* Lower 16 bit of data must be zero.
-*/
-void
-scm_i_set_smob_flags (SCM x, scm_t_bits data)
-{
- SCM_SET_CELL_WORD_0 (x, (SCM_CELL_WORD_0 (x) & 0xFFFF) | data);
-}
-
void
scm_assert_smob_type (scm_t_bits tag, SCM val)
{
return SCM_CELL_OBJECT_1 (ptr);
}
+\f
/* {Free}
*/
return 0;
}
-size_t
-scm_smob_free (SCM obj)
-{
- long n = SCM_SMOBNUM (obj);
- if (scm_smobs[n].size > 0)
- scm_gc_free ((void *) SCM_CELL_WORD_1 (obj),
- scm_smobs[n].size, SCM_SMOBNAME (n));
- return 0;
-}
-
+\f
/* {Print}
*/
return 1;
}
+\f
/* {Apply}
*/
-#define SCM_SMOB_APPLY0(SMOB) \
- SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
-#define SCM_SMOB_APPLY1(SMOB, A1) \
- SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
-#define SCM_SMOB_APPLY2(SMOB, A1, A2) \
- SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
-#define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
- SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
+static SCM scm_smob_trampolines[16];
+
+/* (nargs * nargs) + nopt + rest * (nargs + 1) */
+#define SCM_SMOB_TRAMPOLINE(nreq,nopt,rest) \
+ scm_smob_trampolines[(nreq + nopt + rest) * (nreq + nopt + rest) \
+ + nopt + rest * (nreq + nopt + rest + 1)]
static SCM
-scm_smob_apply_0_010 (SCM smob)
+apply_0 (SCM smob)
{
- return SCM_SMOB_APPLY1 (smob, SCM_UNDEFINED);
+ SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
+ return subr (smob);
}
static SCM
-scm_smob_apply_0_020 (SCM smob)
+apply_1 (SCM smob, SCM a)
{
- return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_UNDEFINED);
+ SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
+ return subr (smob, a);
}
static SCM
-scm_smob_apply_0_030 (SCM smob)
+apply_2 (SCM smob, SCM a, SCM b)
{
- return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_UNDEFINED);
+ SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
+ return subr (smob, a, b);
}
static SCM
-scm_smob_apply_0_001 (SCM smob)
+apply_3 (SCM smob, SCM a, SCM b, SCM c)
{
- return SCM_SMOB_APPLY1 (smob, SCM_EOL);
+ SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
+ return subr (smob, a, b, c);
}
static SCM
-scm_smob_apply_0_011 (SCM smob)
+scm_smob_trampoline (unsigned int nreq, unsigned int nopt,
+ unsigned int rest)
{
- return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_EOL);
-}
+ SCM trampoline;
-static SCM
-scm_smob_apply_0_021 (SCM smob)
-{
- return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_EOL);
-}
+ if (SCM_UNLIKELY (rest > 1 || nreq + nopt + rest > 3))
+ scm_out_of_range ("make-smob", scm_from_uint (nreq + nopt + rest));
+
+ trampoline = SCM_SMOB_TRAMPOLINE (nreq, nopt, rest);
-static SCM
-scm_smob_apply_0_error (SCM smob)
-{
- scm_wrong_num_args (smob);
-}
-
-static SCM
-scm_smob_apply_1_020 (SCM smob, SCM a1)
-{
- return SCM_SMOB_APPLY2 (smob, a1, SCM_UNDEFINED);
-}
-
-static SCM
-scm_smob_apply_1_030 (SCM smob, SCM a1)
-{
- return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_UNDEFINED);
-}
+ if (SCM_LIKELY (SCM_UNPACK (trampoline)))
+ return trampoline;
-static SCM
-scm_smob_apply_1_001 (SCM smob, SCM a1)
-{
- return SCM_SMOB_APPLY1 (smob, scm_list_1 (a1));
-}
-
-static SCM
-scm_smob_apply_1_011 (SCM smob, SCM a1)
-{
- return SCM_SMOB_APPLY2 (smob, a1, SCM_EOL);
-}
-
-static SCM
-scm_smob_apply_1_021 (SCM smob, SCM a1)
-{
- return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_EOL);
-}
-
-static SCM
-scm_smob_apply_1_error (SCM smob, SCM a1 SCM_UNUSED)
-{
- scm_wrong_num_args (smob);
-}
-
-static SCM
-scm_smob_apply_2_030 (SCM smob, SCM a1, SCM a2)
-{
- return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_UNDEFINED);
-}
-
-static SCM
-scm_smob_apply_2_001 (SCM smob, SCM a1, SCM a2)
-{
- return SCM_SMOB_APPLY1 (smob, scm_list_2 (a1, a2));
-}
-
-static SCM
-scm_smob_apply_2_011 (SCM smob, SCM a1, SCM a2)
-{
- return SCM_SMOB_APPLY2 (smob, a1, scm_list_1 (a2));
-}
-
-static SCM
-scm_smob_apply_2_021 (SCM smob, SCM a1, SCM a2)
-{
- return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_EOL);
-}
-
-static SCM
-scm_smob_apply_2_error (SCM smob, SCM a1 SCM_UNUSED, SCM a2 SCM_UNUSED)
-{
- scm_wrong_num_args (smob);
-}
-
-static SCM
-scm_smob_apply_3_030 (SCM smob, SCM a1, SCM a2, SCM rst)
-{
- if (!scm_is_null (SCM_CDR (rst)))
- scm_wrong_num_args (smob);
- return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_CAR (rst));
-}
-
-static SCM
-scm_smob_apply_3_001 (SCM smob, SCM a1, SCM a2, SCM rst)
-{
- return SCM_SMOB_APPLY1 (smob, scm_cons2 (a1, a2, rst));
-}
-
-static SCM
-scm_smob_apply_3_011 (SCM smob, SCM a1, SCM a2, SCM rst)
-{
- return SCM_SMOB_APPLY2 (smob, a1, scm_cons (a2, rst));
-}
+ switch (nreq + nopt + rest)
+ {
+ /* The + 1 is for the smob itself. */
+ case 0:
+ trampoline = scm_c_make_gsubr ("apply-smob/0", nreq + 1, nopt, rest,
+ apply_0);
+ break;
+ case 1:
+ trampoline = scm_c_make_gsubr ("apply-smob/1", nreq + 1, nopt, rest,
+ apply_1);
+ break;
+ case 2:
+ trampoline = scm_c_make_gsubr ("apply-smob/2", nreq + 1, nopt, rest,
+ apply_2);
+ break;
+ case 3:
+ trampoline = scm_c_make_gsubr ("apply-smob/3", nreq + 1, nopt, rest,
+ apply_3);
+ break;
+ default:
+ abort ();
+ }
-static SCM
-scm_smob_apply_3_021 (SCM smob, SCM a1, SCM a2, SCM rst)
-{
- return SCM_SMOB_APPLY3 (smob, a1, a2, rst);
-}
+ SCM_SMOB_TRAMPOLINE (nreq, nopt, rest) = trampoline;
-static SCM
-scm_smob_apply_3_error (SCM smob,
- SCM a1 SCM_UNUSED,
- SCM a2 SCM_UNUSED,
- SCM rst SCM_UNUSED)
-{
- scm_wrong_num_args (smob);
+ return trampoline;
}
\f
scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
scm_smobs[new_smob].name = name;
- if (size != 0)
- {
- scm_smobs[new_smob].size = size;
- scm_smobs[new_smob].free = scm_smob_free;
- }
+ scm_smobs[new_smob].size = size;
/* Make a class object if Goops is present. */
if (SCM_UNPACK (scm_smob_class[0]) != 0)
scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
unsigned int req, unsigned int opt, unsigned int rst)
{
- SCM (*apply_0) (SCM);
- SCM (*apply_1) (SCM, SCM);
- SCM (*apply_2) (SCM, SCM, SCM);
- SCM (*apply_3) (SCM, SCM, SCM, SCM);
- int type = SCM_GSUBR_MAKTYPE (req, opt, rst);
-
- if (rst > 1 || req + opt + rst > 3)
- {
- puts ("Unsupported smob application type");
- abort ();
- }
-
- switch (type)
- {
- case SCM_GSUBR_MAKTYPE (0, 0, 0):
- apply_0 = apply; break;
- case SCM_GSUBR_MAKTYPE (0, 1, 0):
- apply_0 = scm_smob_apply_0_010; break;
- case SCM_GSUBR_MAKTYPE (0, 2, 0):
- apply_0 = scm_smob_apply_0_020; break;
- case SCM_GSUBR_MAKTYPE (0, 3, 0):
- apply_0 = scm_smob_apply_0_030; break;
- case SCM_GSUBR_MAKTYPE (0, 0, 1):
- apply_0 = scm_smob_apply_0_001; break;
- case SCM_GSUBR_MAKTYPE (0, 1, 1):
- apply_0 = scm_smob_apply_0_011; break;
- case SCM_GSUBR_MAKTYPE (0, 2, 1):
- apply_0 = scm_smob_apply_0_021; break;
- default:
- apply_0 = scm_smob_apply_0_error; break;
- }
+ SCM trampoline = scm_smob_trampoline (req, opt, rst);
- switch (type)
- {
- case SCM_GSUBR_MAKTYPE (1, 0, 0):
- case SCM_GSUBR_MAKTYPE (0, 1, 0):
- apply_1 = apply; break;
- case SCM_GSUBR_MAKTYPE (1, 1, 0):
- case SCM_GSUBR_MAKTYPE (0, 2, 0):
- apply_1 = scm_smob_apply_1_020; break;
- case SCM_GSUBR_MAKTYPE (1, 2, 0):
- case SCM_GSUBR_MAKTYPE (0, 3, 0):
- apply_1 = scm_smob_apply_1_030; break;
- case SCM_GSUBR_MAKTYPE (0, 0, 1):
- apply_1 = scm_smob_apply_1_001; break;
- case SCM_GSUBR_MAKTYPE (1, 0, 1):
- case SCM_GSUBR_MAKTYPE (0, 1, 1):
- apply_1 = scm_smob_apply_1_011; break;
- case SCM_GSUBR_MAKTYPE (1, 1, 1):
- case SCM_GSUBR_MAKTYPE (0, 2, 1):
- apply_1 = scm_smob_apply_1_021; break;
- default:
- apply_1 = scm_smob_apply_1_error; break;
- }
-
- switch (type)
- {
- case SCM_GSUBR_MAKTYPE (2, 0, 0):
- case SCM_GSUBR_MAKTYPE (1, 1, 0):
- case SCM_GSUBR_MAKTYPE (0, 2, 0):
- apply_2 = apply; break;
- case SCM_GSUBR_MAKTYPE (2, 1, 0):
- case SCM_GSUBR_MAKTYPE (1, 2, 0):
- case SCM_GSUBR_MAKTYPE (0, 3, 0):
- apply_2 = scm_smob_apply_2_030; break;
- case SCM_GSUBR_MAKTYPE (0, 0, 1):
- apply_2 = scm_smob_apply_2_001; break;
- case SCM_GSUBR_MAKTYPE (1, 0, 1):
- case SCM_GSUBR_MAKTYPE (0, 1, 1):
- apply_2 = scm_smob_apply_2_011; break;
- case SCM_GSUBR_MAKTYPE (2, 0, 1):
- case SCM_GSUBR_MAKTYPE (1, 1, 1):
- case SCM_GSUBR_MAKTYPE (0, 2, 1):
- apply_2 = scm_smob_apply_2_021; break;
- default:
- apply_2 = scm_smob_apply_2_error; break;
- }
-
- switch (type)
- {
- case SCM_GSUBR_MAKTYPE (3, 0, 0):
- case SCM_GSUBR_MAKTYPE (2, 1, 0):
- case SCM_GSUBR_MAKTYPE (1, 2, 0):
- case SCM_GSUBR_MAKTYPE (0, 3, 0):
- apply_3 = scm_smob_apply_3_030; break;
- case SCM_GSUBR_MAKTYPE (0, 0, 1):
- apply_3 = scm_smob_apply_3_001; break;
- case SCM_GSUBR_MAKTYPE (1, 0, 1):
- case SCM_GSUBR_MAKTYPE (0, 1, 1):
- apply_3 = scm_smob_apply_3_011; break;
- case SCM_GSUBR_MAKTYPE (2, 0, 1):
- case SCM_GSUBR_MAKTYPE (1, 1, 1):
- case SCM_GSUBR_MAKTYPE (0, 2, 1):
- apply_3 = scm_smob_apply_3_021; break;
- default:
- apply_3 = scm_smob_apply_3_error; break;
- }
-
- scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
- scm_smobs[SCM_TC2SMOBNUM (tc)].apply_0 = apply_0;
- scm_smobs[SCM_TC2SMOBNUM (tc)].apply_1 = apply_1;
- scm_smobs[SCM_TC2SMOBNUM (tc)].apply_2 = apply_2;
- scm_smobs[SCM_TC2SMOBNUM (tc)].apply_3 = apply_3;
- scm_smobs[SCM_TC2SMOBNUM (tc)].gsubr_type = type;
+ scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
+ /* In 2.2 this field is renamed to "apply_trampoline". */
+ scm_smobs[SCM_TC2SMOBNUM (tc)].apply_trampoline_objcode = trampoline;
if (SCM_UNPACK (scm_smob_class[0]) != 0)
scm_i_inherit_applicable (scm_smob_class[SCM_TC2SMOBNUM (tc)]);
/* The GC kind used for SMOB types that provide a custom mark procedure. */
static int smob_gc_kind;
+/* Mark stack pointer and limit, used by `scm_gc_mark'. */
+static scm_i_pthread_key_t current_mark_stack_pointer;
+static scm_i_pthread_key_t current_mark_stack_limit;
+
-/* The generic SMOB mark procedure that gets called for SMOBs allocated with
- `scm_i_new_smob_with_mark_proc ()'. */
+/* The generic SMOB mark procedure that gets called for SMOBs allocated
+ with smob_gc_kind. */
static struct GC_ms_entry *
smob_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
struct GC_ms_entry *mark_stack_limit, GC_word env)
{
SCM obj;
- SCM_I_CURRENT_THREAD->current_mark_stack_ptr = mark_stack_ptr;
- SCM_I_CURRENT_THREAD->current_mark_stack_limit = mark_stack_limit;
+ scm_i_pthread_setspecific (current_mark_stack_pointer, mark_stack_ptr);
+ scm_i_pthread_setspecific (current_mark_stack_limit, mark_stack_limit);
/* Invoke the SMOB's mark procedure, which will in turn invoke
- `scm_gc_mark ()', which may modify `current_mark_stack_ptr'. */
+ `scm_gc_mark', which may modify `current_mark_stack_pointer'. */
obj = scm_smobs[smobnum].mark (cell);
- mark_stack_ptr = SCM_I_CURRENT_THREAD->current_mark_stack_ptr;
+ mark_stack_ptr = scm_i_pthread_getspecific (current_mark_stack_pointer);
if (SCM_NIMP (obj))
/* Mark the returned object. */
mark_stack_ptr,
mark_stack_limit, NULL);
- SCM_I_CURRENT_THREAD->current_mark_stack_limit = NULL;
- SCM_I_CURRENT_THREAD->current_mark_stack_ptr = NULL;
+ scm_i_pthread_setspecific (current_mark_stack_pointer, NULL);
+ scm_i_pthread_setspecific (current_mark_stack_limit, NULL);
}
return mark_stack_ptr;
}
-/* Mark object O. We assume that this function is only called during the
- mark phase, i.e., from within `smob_mark ()' or one of its
- descendents. */
+/* Mark object O. We assume that this function is only called during the mark
+ phase, i.e., from within `smob_mark' or one of its descendants. */
void
scm_gc_mark (SCM o)
{
-#define CURRENT_MARK_PTR \
- ((struct GC_ms_entry *)(SCM_I_CURRENT_THREAD->current_mark_stack_ptr))
-#define CURRENT_MARK_LIMIT \
- ((struct GC_ms_entry *)(SCM_I_CURRENT_THREAD->current_mark_stack_limit))
-
if (SCM_NIMP (o))
{
- /* At this point, the `current_mark_*' fields of the current thread
- must be defined (they are set in `smob_mark ()'). */
- register struct GC_ms_entry *mark_stack_ptr;
+ void *mark_stack_ptr, *mark_stack_limit;
+
+ mark_stack_ptr = scm_i_pthread_getspecific (current_mark_stack_pointer);
+ mark_stack_limit = scm_i_pthread_getspecific (current_mark_stack_limit);
- if (!CURRENT_MARK_PTR)
+ if (mark_stack_ptr == NULL)
/* The function was not called from a mark procedure. */
abort ();
mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (o),
- CURRENT_MARK_PTR, CURRENT_MARK_LIMIT,
+ mark_stack_ptr, mark_stack_limit,
NULL);
- SCM_I_CURRENT_THREAD->current_mark_stack_ptr = mark_stack_ptr;
+ scm_i_pthread_setspecific (current_mark_stack_pointer, mark_stack_ptr);
}
-#undef CURRENT_MARK_PTR
-#undef CURRENT_MARK_LIMIT
-}
-
-/* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
- provide a custom mark procedure and it will be honored. */
-SCM
-scm_i_new_smob_with_mark_proc (scm_t_bits tc, scm_t_bits data1,
- scm_t_bits data2, scm_t_bits data3)
-{
- /* Return a double cell. */
- SCM cell = SCM_PACK (GC_generic_malloc (2 * sizeof (scm_t_cell),
- smob_gc_kind));
-
- SCM_SET_CELL_WORD_3 (cell, data3);
- SCM_SET_CELL_WORD_2 (cell, data2);
- SCM_SET_CELL_WORD_1 (cell, data1);
- SCM_SET_CELL_WORD_0 (cell, tc);
-
- return cell;
}
\f
/* Finalize SMOB by calling its SMOB type's free function, if any. */
-void
-scm_i_finalize_smob (GC_PTR ptr, GC_PTR data)
+static void
+finalize_smob (void *ptr, void *data)
{
SCM smob;
size_t (* free_smob) (SCM);
free_smob (smob);
}
+/* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
+ provide a custom mark procedure and it will be honored. */
+SCM
+scm_i_new_smob (scm_t_bits tc, scm_t_bits data)
+{
+ scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
+ SCM ret;
+
+ /* Use the smob_gc_kind if needed to allow the mark procedure to
+ run. Since the marker only deals with double cells, that case
+ allocates a double cell. We leave words 2 and 3 to there initial
+ values, which is 0. */
+ if (scm_smobs [smobnum].mark)
+ ret = PTR2SCM (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
+ else
+ ret = PTR2SCM (GC_MALLOC (sizeof (scm_t_cell)));
+
+ SCM_SET_CELL_WORD_1 (ret, data);
+ SCM_SET_CELL_WORD_0 (ret, tc);
+
+ if (scm_smobs[smobnum].free)
+ scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
+
+ return ret;
+}
+
+/* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
+ provide a custom mark procedure and it will be honored. */
+SCM
+scm_i_new_double_smob (scm_t_bits tc, scm_t_bits data1,
+ scm_t_bits data2, scm_t_bits data3)
+{
+ scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
+ SCM ret;
+
+ /* Use the smob_gc_kind if needed to allow the mark procedure to
+ run. */
+ if (scm_smobs [smobnum].mark)
+ ret = PTR2SCM (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
+ else
+ ret = PTR2SCM (GC_MALLOC (2 * sizeof (scm_t_cell)));
+
+ SCM_SET_CELL_WORD_3 (ret, data3);
+ SCM_SET_CELL_WORD_2 (ret, data2);
+ SCM_SET_CELL_WORD_1 (ret, data1);
+ SCM_SET_CELL_WORD_0 (ret, tc);
+
+ if (scm_smobs[smobnum].free)
+ scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
+
+ return ret;
+}
+
+
+\f
+
+/* These two are internal details of the previous implementation of
+ SCM_NEWSMOB and are no longer used. They are still here to preserve
+ ABI stability in the 2.0 series. */
+void
+scm_i_finalize_smob (void *ptr, void *data)
+{
+ finalize_smob (ptr, data);
+}
+
+SCM
+scm_i_new_smob_with_mark_proc (scm_t_bits tc, scm_t_bits word1,
+ scm_t_bits word2, scm_t_bits word3)
+{
+ return scm_new_double_smob (tc, word1, word2, word3);
+}
+
+
\f
void
scm_smob_prehistory ()
{
long i;
+ scm_i_pthread_key_create (¤t_mark_stack_pointer, NULL);
+ scm_i_pthread_key_create (¤t_mark_stack_limit, NULL);
+
smob_gc_kind = GC_new_kind (GC_new_free_list (),
GC_MAKE_PROC (GC_new_proc (smob_mark), 0),
0,
scm_smobs[i].print = scm_smob_print;
scm_smobs[i].equalp = 0;
scm_smobs[i].apply = 0;
- scm_smobs[i].apply_0 = 0;
- scm_smobs[i].apply_1 = 0;
- scm_smobs[i].apply_2 = 0;
- scm_smobs[i].apply_3 = 0;
- scm_smobs[i].gsubr_type = 0;
+ scm_smobs[i].apply_trampoline_objcode = SCM_BOOL_F;
}
}