Version française
Home     About     Download     Resources     Contact us    
Browse thread
Again C-Interface: caml_alloc_custom
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Bauer, Christoph <Christoph.Bauer@l...>
Subject: Again C-Interface: caml_alloc_custom
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