caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Interfacing C procedure using an array as parameter ?
@ 1997-04-28 14:46 Vincent B.G. Leleu
  1997-04-29 15:42 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Vincent B.G. Leleu @ 1997-04-28 14:46 UTC (permalink / raw)
  To: caml-list

[French version at the bottom]


	Hello all,


	Firstly, thanks to those of you who provided me with
	information about C++/OCaml interfacing, this has been
	very usefull.

	This is the first time I interface C/OCaml and also I have
	a problem : How do I use a parameter which is a pointer
	on the beginning of an integer array ?


	The C prototype :
	----------------

	void EndConfig(int *tid, int nb_tids);
	/* tid is a pointer on an array of task id
           nb_tids is the number of task id in the array */


	the Caml interface :
	-------------------

	external end_config : int vect -> unit;;

	value end_config(tids)
	  value tids;
	{
           /* what do I put here ? */
        }


	Though the RM provides an example with return list (p.122-3)
	I am a bit lost regarding the use of vectors.


		Thanks for any help,

		Vincent.


[Version francaise]

	Bonjour at tous,

	Tout d'abord merci aux personnes qui m'ont fourni des
	informations a propos de l'interfacing de C++/OCaml,
	elles ont ete tres utiles.

	C'est la premiere fois que j'interface C/OCaml et aussi
	j'ai un probleme : Comment utiliser un argument qui est
	un pointeur sur  le debut d'un tableau d'entiers ?

	Le prototypage C :
	-----------------

	void EndConfig(int *tid, int nb_tids);
	/* tid is a pointer on an array of task id
           nb_tids is the number of task id in the array */


	l'interface Caml :
	-----------------

	external end_config : int vect -> unit;;

	value end_config(tids)
	  value tids;
	{
           /* que dois-je mettre ici ? */
        }

	Bien que j'ai trouve un exemple dans le MR avec un retour
	de liste (p.122-3) je suis un peu perdu en ce qui concerne
	l'utilisation des vectors.


		Merci pour votre aide,

		Vincent.


______________________________________________________________

  Vincent B.G.Leleu
  Parallel Computing Research Centre
  Anglia Polytechnic University
  Victoria Rd. South, Chelmsford
  Essex CM1 1JJ
  England

  E-Mail : mailto:vincent@parc.anglia.ac.uk
  URL    : http://www.parc.anglia.ac.uk/~vincent
  Phone  : +44 1245 493131 ext. 3416
  Fax    : +44 1245 495263 - PARC
  Pager  : +44 1426 634330 (phone numbers only)





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

* Re: Interfacing C procedure using an array as parameter ?
  1997-04-28 14:46 Interfacing C procedure using an array as parameter ? Vincent B.G. Leleu
@ 1997-04-29 15:42 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 1997-04-29 15:42 UTC (permalink / raw)
  To: Vincent B.G. Leleu; +Cc: caml-list

> 	This is the first time I interface C/OCaml and also I have
> 	a problem : How do I use a parameter which is a pointer
> 	on the beginning of an integer array ?

To get you started, here are functions to convert between a C integer
array and a Caml integer array. (Warning: untested code ahead.)

value c_intarray_to_caml_intarray(int * carray, unsigned int nelts)
{
  value mlarray;
  int i;

  mlarray = nelts < Max_young_wosize ? alloc(nelts, 0) : alloc_shr(nelts, 0);
  for (i = 0; i < nelts; i++) Field(mlarray, i) = Val_int(carray[i]);
  return mlarray;
}

int caml_intarray_to_c_intarray(value mlarray,
                                int ** carray /*out parameter*/,
                                unsigned int * nelts /*out parameter*/)
{
  unsigned int len;
  int * p;
  int i;

  len = Wosize_val(mlarray);
  p = malloc(len * sizeof(int));
  if (p == NULL) return -1;
  for (i = 0; i < len; i++) p[i] = Int_val(Field(mlarray, i));
  *carray = p;
  *nelts = len;
  return 0;
}

Hope this helps,

- Xavier Leroy





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

end of thread, other threads:[~1997-04-29 17:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-28 14:46 Interfacing C procedure using an array as parameter ? Vincent B.G. Leleu
1997-04-29 15:42 ` Xavier Leroy

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