caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Need help: O'Caml C Interface
@ 1996-09-24 14:45 Juan Jose Quintela Carreira
  1996-09-24 17:11 ` Jerome Vouillon
  0 siblings, 1 reply; 4+ messages in thread
From: Juan Jose Quintela Carreira @ 1996-09-24 14:45 UTC (permalink / raw)
  To: caml-list, christo



Hi,
	If you changes the lines:
> 
> value ocaml_getCharWidth(value code)
> {
>   double x, y;
>   Push_roots(r,1);
> 
>   r[0] = alloc_tuple(2);
>   getCharWidth(Int_val(code),&x,&y);
-   Store_double_val(Field(r[0],0),x);
+   modify(&Field(r[0],0),copy_double(x); /* Field(r[0],0)=copy_double(x)*/
-   Store_double_val(Field(r[0],1),y);
+   modify(&Field(r[0],1),copy_double(y); /* Field(r[0],1)=copy_double(y)*/
>   Pop_roots();
>   return r[0];
> }
> 

The coment sentences also work in this example, but the former work always,
For details see the manual section:
	Chapter: Interfacing C with Objective Caml
	Section: Living in harmony with the garbage collector

Regards, Juan.





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

* Re: Need help: O'Caml C Interface
  1996-09-24 14:45 Need help: O'Caml C Interface Juan Jose Quintela Carreira
@ 1996-09-24 17:11 ` Jerome Vouillon
  0 siblings, 0 replies; 4+ messages in thread
From: Jerome Vouillon @ 1996-09-24 17:11 UTC (permalink / raw)
  To: Juan Jose Quintela Carreira; +Cc: caml-list, christo



> 	If you changes the lines:
> > 
> > value ocaml_getCharWidth(value code)
> > {
> >   double x, y;
> >   Push_roots(r,1);
> > 
> >   r[0] = alloc_tuple(2);
> >   getCharWidth(Int_val(code),&x,&y);
> -   Store_double_val(Field(r[0],0),x);
> +   modify(&Field(r[0],0),copy_double(x); /* Field(r[0],0)=copy_double(x)*/
> -   Store_double_val(Field(r[0],1),y);
> +   modify(&Field(r[0],1),copy_double(y); /* Field(r[0],1)=copy_double(y)*/
> >   Pop_roots();
> >   return r[0];
> > }
> > 
> 
> The coment sentences also work in this example, but the former work always,

Well, actually neither are correct, as copy_double allocate some memory.
Thus, the tuple should have been filled with dummy values prior to the 
first call to copy_double, and one cannot use `Field(..) = ..'.

   Jerome





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

* Re: Need help: O'Caml C Interface
  1996-09-23 15:09 Frank Christoph
@ 1996-09-24 13:36 ` Jerome Vouillon
  0 siblings, 0 replies; 4+ messages in thread
From: Jerome Vouillon @ 1996-09-24 13:36 UTC (permalink / raw)
  To: Frank Christoph; +Cc: caml-list


> value ocaml_getCharWidth(value code)
> {
>   double x, y;
>   Push_roots(r,1);
> 
>   r[0] = alloc_tuple(2);
>   getCharWidth(Int_val(code),&x,&y);
>   Store_double_val(Field(r[0],0),x);
>   Store_double_val(Field(r[0],1),y);
>   Pop_roots();
>   return r[0];
> }

I would write it this way:

value ocaml_getCharWidth(value code)
{
  double x, y;
  value res;
  Push_roots(r,2);

  getCharWidth(Int_val(code),&x,&y);
  r[0] = copy_double(x);
  r[1] = copy_double(y);
  res = alloc_tuple(2);
  Field(res, 0) = r[0];
  Field(res, 1) = r[1];
  Pop_roots();
  return res;
}

Store_double_val is used to store a double in an array of double values 
(which is a special kind of array). In other cases, you have to use 
copy_double to allocate a block containing the double.
So, your code failed because Field(r[0],0) was not a pointer to an array,
but an uninitialized pointer. 
*_roots are used to keep a pointer to both double values. Indeed, these
values can be moved by copy_double or alloc_tuple. The tuple is allocated
last, so that it can be directly filled (without having to use function
modify).

   Jerome










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

* Need help: O'Caml C Interface
@ 1996-09-23 15:09 Frank Christoph
  1996-09-24 13:36 ` Jerome Vouillon
  0 siblings, 1 reply; 4+ messages in thread
From: Frank Christoph @ 1996-09-23 15:09 UTC (permalink / raw)
  To: caml-list


I have a C function 

  void getCharWidth(int code, double *x, double *y);

which returns its values in the two pointer arguments.  I tried to write a
stub function that creates a 2-tuple and passes it back:

  external width : int -> float * float = "ocaml_getCharWidth"

via:

value ocaml_getCharWidth(value code)
{
  double x, y;
  Push_roots(r,1);

  r[0] = alloc_tuple(2);
  getCharWidth(Int_val(code),&x,&y);
  Store_double_val(Field(r[0],0),x);
  Store_double_val(Field(r[0],1),y);
  Pop_roots();
  return r[0];
}

where I used Store_double_val because, as far as I can see, there is no
"Val_double" macro.  However, this gives me a segmentation fault.  I'm not
really sure I understand how the *_roots functions work.  Do the two local
variables x and y need to be declared as of type value?  If so, how can I
pass them to getCharWidth (since Double_val doesn't return an lvalue)?  What
am I doing wrong?

------------------------------------------------------------------------
Frank Christoph                 Next Solution Co.      Tel: 0424-98-1811
christo@nextsolution.co.jp                             Fax: 0424-98-1500





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

end of thread, other threads:[~1996-09-25  7:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-24 14:45 Need help: O'Caml C Interface Juan Jose Quintela Carreira
1996-09-24 17:11 ` Jerome Vouillon
  -- strict thread matches above, loose matches on Subject: below --
1996-09-23 15:09 Frank Christoph
1996-09-24 13:36 ` Jerome Vouillon

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).