Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Bauer, Christoph" <Christoph.Bauer@lms-gmbh.de>
To: caml-list@inria.fr
Subject: Again C-Interface: caml_alloc_custom
Date: Fri, 20 Jan 2006 16:07:03 +0100	[thread overview]
Message-ID: <26EB47FDD566A7469FC862DAF373792F07D738@kaiserslautern1.lmsintl.com> (raw)

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 


             reply	other threads:[~2006-01-20 15:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-20 15:07 Bauer, Christoph [this message]
2006-01-20 15:27 ` [Caml-list] " Olivier Andrieu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=26EB47FDD566A7469FC862DAF373792F07D738@kaiserslautern1.lmsintl.com \
    --to=christoph.bauer@lms-gmbh.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox