caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Again C-Interface: caml_alloc_custom
@ 2006-01-20 15:07 Bauer, Christoph
  2006-01-20 15:27 ` [Caml-list] " Olivier Andrieu
  0 siblings, 1 reply; 2+ messages in thread
From: Bauer, Christoph @ 2006-01-20 15:07 UTC (permalink / raw)
  To: caml-list

Hi,

many thanks for the anwsers of my other questions.

I use Custom_blocks in OCaml to handle reference-countend
objects (Tcl_Obj). My program runs fine - but only with use=0, max=1
in caml_alloc_custom.

 caml_alloc_custom( &tcl_obj_ops, sizeof( Tcl_Obj * ), 0, 1 );

Otherwise (use=1, max=1; use=1, max=100) I get random 
crashes (e.g. in mark_slice). This makes me a bad feeling.

Maybe something more is involved (Callbacks+GC?), because with a simple
test (many allocations and finalizations) case couldn't reproduce 
this crash.

Does anybody have such experiences?

Could be here a problem (CAMLparam+CAMLreturn in C-called function):

static value copy_tcl_array(int objc,  Tcl_Obj * const * objv)
{
  CAMLparam0 ();
  CAMLlocal2 (result, t);
  int n;

  result = Val_int(0);
  for (n = objc-1; n >= 0; --n) {
    t = caml_alloc_small(2,0);
    Field(t, 0) = alloc_tcl_obj( objv[n] );
    Field(t, 1) = result; 
    result = t;
  }
  CAMLreturn (result);
}


... C-Code:
 r = callback(*f, copy_tcl_array(objc-2, objv+2));

Regards,
Christoph Bauer


For completness the tcl_obj_ops:

#define TclObj_val(x) (*((Tcl_Obj**) Data_custom_val(x)))

static
void finalize_tclobj( value v ) {
  /* printf("finalize tclobj\n"); */
  fflush(stdout);
  Tcl_DecrRefCount( TclObj_val (v) );
}

static
int compare_tclobj( value v1, value v2 ) {
  CAMLparam2( v1, v2 );
  CAMLreturn( strcmp( Tcl_GetString( TclObj_val(v1)), 
                      Tcl_GetString( TclObj_val(v2) )));
}

static 
long hash_tclobj( value v ) {
  CAMLparam1( v );
  char *s = Tcl_GetString( TclObj_val(v ) );
  long result = 0;
  while( *s ) result = (result * 7) + *s++;
  CAMLreturn( result );
}

static
struct custom_operations tcl_obj_ops = {
  "Tcl_Obj",
  &finalize_tclobj,
  &compare_tclobj,
  &hash_tclobj,
  custom_serialize_default,
  custom_deserialize_default
};


Christoph Bauer
Dipl. Inf.

LMS Deutschland GmbH
Luxemburgerstr. 7
D-67657 Kaiserslautern

T +49 631 303 22 152

mailto:Christoph.Bauer@lms-gmbh.de
http://www.lmsintl.com 


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Again C-Interface: caml_alloc_custom
  2006-01-20 15:07 Again C-Interface: caml_alloc_custom Bauer, Christoph
@ 2006-01-20 15:27 ` Olivier Andrieu
  0 siblings, 0 replies; 2+ messages in thread
From: Olivier Andrieu @ 2006-01-20 15:27 UTC (permalink / raw)
  To: Bauer, Christoph; +Cc: caml-list

 Bauer, Christoph [Friday 20 January 2006] :
 > Could be here a problem (CAMLparam+CAMLreturn in C-called function):
 > 
 > static value copy_tcl_array(int objc,  Tcl_Obj * const * objv)
 > {
 >   CAMLparam0 ();
 >   CAMLlocal2 (result, t);
 >   int n;
 > 
 >   result = Val_int(0);
 >   for (n = objc-1; n >= 0; --n) {
 >     t = caml_alloc_small(2,0);
 >     Field(t, 0) = alloc_tcl_obj( objv[n] );
                     ^^^^^^^^^^^^^

I guess alloc_tcl_obj creates the custom block. That's wrong because
the block t you've just allocated with caml_alloc_small is not
completely initialized yet.

Either do this (safe, higher-level way):
,----
|   t = caml_alloc (2, 0);
|   Store_field (t, 0, alloc_tcl_obj (objv[n]));
|   Store_field (t, 1, result);
`----

or that (lower-level) :
,----
|   CAMLlocal1(obj);
|   obj = alloc_tcl_obj (objv[n]);
|   t = caml_alloc_small (2, 0);
|   Field (t, 0) = obj;
|   Field (t, 1) = result;
`----

-- 
   Olivier


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-01-20 15:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-20 15:07 Again C-Interface: caml_alloc_custom Bauer, Christoph
2006-01-20 15:27 ` [Caml-list] " Olivier Andrieu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).