guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: about `smob' generated by swig


From: Marco Maggi
Subject: Re: about `smob' generated by swig
Date: Wed, 28 Mar 2007 06:57:18 +0200

"William Xu" wrote:
> Swig could generate pointer types  in C to smob. But users
> still  have to define  smob related  operations(like mark,
> free, etc) by hand?

Mh, no.   I have never used SWIG  but if I read  [1] and [2]
and   by   inspecting   "Examples/guile"   in   the   source
distribution  of SWIG  1.3.31,  it seems  to  me that  every
unknown data type used  as parameter to functions is treated
like a pointer.

[1] <http://www.swig.org/Doc1.1/HTML/SWIG.html#n3>
[2] "swig-1.3.31/Doc/Manual/Guile.html"

  That is: SWIG generates a SMOB holding two values:

1 - the pointer to the data instance;
2 - a string holding the name of the unknown type;

and uses  that as  reference to the  unknown type;  the data
instance  itself is  just a  block  of memory,  it is  *not*
wrapped in  an automatically  defined SMOB.  Let's  call the
SWIG generated SMOB: 'reference'  SMOB.  A reference SMOB is
of  a type  defined by  SWIG, so  we have  to do  nothing to
handle its  memory: by  looking in a  "*_wrap.c" file  I see
that  there  are SMOB  type  definitions  whose drivers  are
referenced     by    variables    like     'swig_tag'    and
'swig_collectable_tag',  and   SWIG  implements  the  driver
functions for them.

  When SWIG  processes a function  that has some  C language
type  as  argument  or   return  value,  it  just  uses  the
corresponding SMOB type as defined by Guile to handle it, so
we have to do nothing.

  When SWIG  processes a function that has  unknown types as
argument  or return value,  it wraps  the function  with one
that uses reference SMOBs.

  The reference  SMOB drivers have no  mark function. AFAICS
there is  no way to tell  SWIG to invoke a  mark function to
mark  fields in  a  custom  data structure;  that  is: if  a
function builds and returns a structure:

    struct my_type_t {
      int a;
      SCM b;
    };

there is  no way to  mark 'b'. We  have to register  it with
'scm_gc_protect_object()'.

  It is my  understanding that it is possible  to register a
destroy function to be  invoke when garbage collection runs,
to free the  data referenced by a reference  SMOB; but I was
not able to find how to do it.

  Anyway  a   destructor  for  custom   data  referenced  by
reference SMOBs has  to be written, and then  you can use it
in a guardian and with 'after-gc-hook'.

  By the way: to write a SMOB driver is not difficult if
you know some C language. Are you sure that you need
SWIG?

--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"





reply via email to

[Prev in Thread] Current Thread [Next in Thread]