This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Problem with new datatype


Ralf Schmelter <ralfs@heineken.chemie.uni-dortmund.de> writes:

> Now this works well, but if I move the definition "SCM scm_result;"
> after the while loop the program crashed with a segmentation fault.
> Can anoyone explain this behavior?

After a short perusal of your code, I don't find anything obviously
wrong.  But that's probably because I use a structured approach to
adding smobs, which you might find useful.  Also, you're not type
checking the first argument to your primitive.

I always write little functions to do type testing, boxing, and
unboxing for each source file that defines a new smob.  This makes the
remainder of the code in the source file much easier to read.  I hope
this helps.

static struct {
    long execution_type_tag;
} g;

/*
 * boxing, unboxing, type testing functions
 */
static LamNormTrade *
exec_unbox(SCM obj)
{
    return((LamNormTrade*)SCM_CDR(obj));
}

static SCM
exec_box(LamNormTrade *lnt)
{
    SCM z;
    GC_PRINT(fprintf(stderr, "making exec with addr 0x%x\n", (void*)lnt));
    SCM_DEFER_INTS;
    SCM_NEWCELL(z);
    SCM_SETCAR(z, g.execution_type_tag);
    SCM_SETCDR(z, (SCM)lnt);
    SCM_ALLOW_INTS;
    return z;
}

static int
exec_p(SCM obj)
{
    return(SCM_NIMP(obj) && SCM_CAR(obj) == g.execution_type_tag);
}


And here is a primitive using these functions:

SCM_PROC(s_tb_get_round_rule, "tb:get-round-rule", 1, 0, 0, scm_tb_get_round_rule);
static SCM
scm_tb_get_round_rule(SCM exec)
{
    SCM_ASSERT(exec_p(exec), exec, SCM_ARG1, s_tb_get_round_rule);
    return SCM_MAKINUM(exec_unbox(exec)->getRoundRule());
}


--
"People who fight may lose. People who do not fight have already lost." 
             -- Bertold Brecht