caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: ds.caml@sol42.com
To: undisclosed-recipients:;
Subject: [Caml-list]
Date: Wed, 14 Sep 2011 21:00:02 +0200	[thread overview]
Message-ID: <201109141900.p8EJ019C032522@walapai.inria.fr> (raw)

	by mail.SOL42.com with ESMTPA id 4e70f9360481a1;

	Wed, 14 Sep 2011 20:57:58 +0200

Content-Type: text/plain; charset=iso-8859-1

Mime-Version: 1.0 (Apple Message framework v1084)

Subject: Re: [Caml-list] Link a .so/.dll dynamically

From: ds.caml@sol42.com

In-Reply-To: <1316016912.24759.29.camel@aurora>

Date: Wed, 14 Sep 2011 20:57:58 +0200

Content-Transfer-Encoding: quoted-printable

Message-Id: <C73B38A0-5F67-4D2D-AF42-7D9A63722EBE@sol42.com>

References: <4E70C18F.3040304@lri.fr> <1316016912.24759.29.camel@aurora>

To: caml-list@inria.fr

X-Mailer: Apple Mail (2.1084)



On 14 Sep 2011, at 18:15, J=E9r=E9mie Dimino wrote:

> You can also use dlopen and dlsym on unix, and=20

> LoadLibrary and GetProcAddress on windows.



Right.  For truly dynamic loading, if you already have a set of .so =

files with similar API you might want to write some "loader" in C and =

compile that into your OCaml program.  No need to put an extra dynlink =

layer in between.  BTW Dynlink is not available on all platforms.



In this "loader" you would call dlopen() and dlsym() (or their Windows =

counterparts) to get the addresses of the functions in the .so, and make =

them available to the rest of your OCaml program.  You still need to =

translate OCAml-to-C function arguments and C-to-OCaml function results. =

 Note that this is somewhat insecure C plumbing, you can easily bring =

down the whole process in a number of ways.





While it is way too soon for announcements, I do have some very =

experimental code that allows to do exactly this in pure OCaml.  All you =

need to do is write a C wrapper, sort of like how you do it in Python =

(see http://docs.python.org/extending/extending.html for example).  The =

OCaml code looks like this:



# #load "dffi.cma";;

# open DL;;

# #install_printer Pointer.pp;;



# let lib =3D dlopen "/wherever/pgtest.dylib" [];;

val lib : DL.dllib =3D <abstr>



# let connect          =3D dlsym lib "pq_connect_db" and

    disconnect       =3D dlsym lib "pq_finish" and

    protocol_version =3D dlsym lib "pq_protocol_version";;

val connect : DL.dlfun =3D <abstr>

val disconnect : DL.dlfun =3D <abstr>

val protocol_version : DL.dlfun =3D <abstr>



# let [|conn|] =3D dlcall connect [|String "dbname=3Dname_of_database"|];;=



val conn : DL.cval =3D Ptr (PGconn*)0x0000000100100ef0



# dlcall protocol_version [|conn|];;

- : DL.cval array =3D [|Int 3|]



# dlcall disconnect [|conn|];;

- : DL.cval array =3D [||]



# dlclose lib;;

- : unit =3D ()



And pgtest.c (a PostgreSQL client lib protowrapper) looks like this:



#include <sys/types.h>

#include <unistd.h>

#include <errno.h>

#include <stdio.h>

#include <libpq-fe.h>



#include "dlutil.h"



FUNC(pq_connect_db) {

	ARGS; DECL;

	PGconn* conn;

	CHECKNUMARGS(1); CHECKSTR(0); /*1 arg, must be String*/

	conn =3D PQconnectdb((const char*)GETSTR(0));

	if (PQstatus(conn) =3D=3D CONNECTION_OK) {

		ALLOC(1); /*return 1 value*/

		SETPTR(0, PGconn*, conn); }

	else {

		PQfinish(conn);

		ALLOC(0); }

	RETURN; }



FUNC(pq_finish) {

	ARGS; DECL;

	CHECKNUMARGS(1); CHECKPTR(0); CHECKPTR_TYPE(0, PGconn*);

	PQfinish(GETPTR(0, PGconn*));

	ALLOC(0);

	RETURN; }



FUNC(pq_protocol_version) {

	ARGS; DECL;

	CHECKNUMARGS(1); CHECKPTR(0); CHECKPTR_TYPE(0, PGconn*);

	ALLOC(1);

	SETINT(0, PQprotocolVersion(GETPTR(0, const PGconn*)));

	RETURN; }



FUNC, ARGS, CHECKNUMARGS and the rest are nasty dlutil.h macros that do =

all the OCaml-C magic as described in the OCaml docs.



Functions all have the same signature, they receive an array of values =

amd can check the number and type of its elements.  They return another =

array, like multiple value returns in Lisp.  You can get pretty creative =

with this scheme.



This is very early code with plenty of missing stuff, and I haven't used =

it for anything serious yet, but if it might be of use to anyone as-is =

then just ask.





Regards.

-Daniel




             reply	other threads:[~2011-09-14 19:00 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-14 19:00 ds.caml [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-02-19 17:25 [Caml-list] Latif A
2021-10-11  7:57 [Caml-list] Vasilis Goumas
2021-09-29 15:47 [Caml-list] Vasilis Goumas
2021-04-19 13:49 [Caml-list] Aaron Gray
2021-04-11 13:58 [Caml-list] Ulugbek Abdullaev
2021-04-07 11:50 [Caml-list] Fabien / maufred
2021-01-24 16:20 [Caml-list] Latif A
2019-11-11  1:58 [Caml-list] Alexandre Macedo
2019-04-26 19:04 [Caml-list] Wei Wen Goh
2016-12-29 18:00 [Caml-list] Matthew Tovbin
2016-05-31  6:28 [Caml-list] Seshachalam M
2016-05-12 15:42 [Caml-list] Hongbo Zhang (BLOOMBERG/ 731 LEX)
2015-12-31 20:29 [Caml-list] Adil Hafeez
2015-10-09  6:33 [Caml-list] Gergely Szilvasy
     [not found] <20150821100011.4BF127F0D7@sympa.inria.fr>
2015-08-21 12:28 ` [Caml-list] Nils Becker
2015-03-09 14:44 [Caml-list] Secret
2014-12-16 23:43 [Caml-list] André Luiz Moura
2014-12-16  7:48 [Caml-list] Sergey Abramyan
2014-07-04 10:35 [Caml-list] freetolearn 2013
2013-11-05 16:14 [Caml-list] Zhi Han
2013-09-25 17:56 [Caml-list] Dylan Sanders
2013-08-23 12:26 [Caml-list] Alexey Rodriguez
2013-08-23 12:56 ` [Caml-list] Robert Jakob
2011-12-07 21:19 [Caml-list] Joaquin Cuenca Abela
2011-09-14 19:45 [Caml-list] ds.caml
2011-09-12  5:50 [Caml-list] arupkumar.das
2011-08-20 16:05 [Caml-list] Jeff Massung
2011-06-28 17:09 [Caml-list] Agnius Vasiliauskas
2011-05-29 10:45 [Caml-list] ds.caml
2011-04-05  0:04 [Caml-list] Ãßõ¸ÞÀÏ »õ·Î¿î
2011-01-03 21:57 [Caml-list] Fabien / maufred
2004-01-03  6:52 [Caml-list] (±¤°í)¡Ú¡ÙÀÜ¿©Çѵµ´ëÃâ!!ÀÌÁ¨¹Ï°íÇϼ¼¿©!! ÇÑÁ¤¹Î
2004-01-03  5:16 [Caml-list] Àú¿Í ¾ËÄá´ÞÄáÇÑ ¾ê±â¸¦ ÇØ¿©! can2u
2004-01-03  4:03 [Caml-list] [±¤°í] ½´ÆÛ ÆÄ¿ö Æä´Ï½º ¸¸µé±â @ Àڽۨ
2004-01-03  1:36 [Caml-list] (±¤°í)¢Â´ëÃâ!! ÀüÈ­·Î ½ºÇÇµå ´ëÃâ Çص帳´Ï´Ù(ÃÖÀú%) ±è´ë¸®
2004-01-02 22:30 [Caml-list] (±¤°í)ÄÄÇ»Å͸¸ ÄÑ ³õÀ¸¸é µ·ÀÌ µÈ´Ù´Âµ¥ ÁÁÀºÁ¤º¸ Àü´ÞÀÚ
2004-01-02 21:02 [Caml-list] ÁøÁ¤ÇÑ ¸®¾óÄ«Áö³ëÀÇ ¼¼°è·Î ÃÊ´ëÇÕ´Ï´Ù!!! kim
2004-01-02 20:53 [Caml-list] Àý´ë ¼ºÀθ¸ Ŭ¸¯ Çϼ¼¿ä^^ kfskdsdfsdfsdf
2004-01-02 20:45 kfskdsdfsdfsdf
2004-01-02 18:08 [Caml-list] ÔÆÄÏÖ®Âà ÔÆÄÏÀö½­»¢ÌøÏ¿¹ú¼ÊÂÃÐÐÉç
2004-01-02 17:04 [Caml-list] È«Çò¾­¼ÃÔö³¤ÂÊ×î¿ìµÄ¹ú¼ÒÖ®Ò»! Ó¡¶È
2004-01-02 17:04 [Caml-list] ¿ªÍØÓ¡¶ÈÊг¡ Ó¡¶È
2004-01-02 16:38 [Caml-list] ÎÒÃÇ·ÅÆúÏòÈÕË÷ÅâÊÇÔÚÆäÉî¿Ì·´Ê¡µÄ»ù´¡ÉÏ£¬Ëû²»·´Ê¡£¬ÎÒÃǾÍÓÐȨË÷Åâ cameco
2004-01-02  8:51 [Caml-list] (±¤°í)ÄÄÇ»Å͸¸ ÄÑ ³õÀ¸¸é µ·ÀÌ µÈ´Ù´Âµ¥ ÁÁÀºÁ¤º¸ Àü´ÞÀÚ
2004-01-02  8:50 ÁÁÀºÁ¤º¸ Àü´ÞÀÚ
2004-01-02  8:29 [Caml-list] ¸¶ÄÉÆà ÇÁ·Î±×·¥ ¹«·á ´Ù¿î·Îµå! camlliou
2004-01-02  2:32 [Caml-list] (±¤°í)¿¬Ã¼,´ëÃâ °ÆÁ¤ ³¡!Áï½Ã ÇØ°áÇØ µå¸³´Ï´Ù!@ ºô·Áµå¸²
2004-01-01 15:47 [Caml-list] (±¤°í)ÇÞ¸êÄ¡,±¹³»»êÁãÆ÷, »êÁö ¿Õµµ¸Å°¡°Ý »êÁö Àۼ۾ȳ» @ I³²ÇؾȸêÄ¡9
2003-10-09 13:27 [Caml-list] ¾È³çÇϼ¼¿ä^^ ±è¹ÎÁ¤
2003-08-20  2:00 [Caml-list] °í°´´ÔÀÌ ¿äûÇϽŠ½Å¿ëÆò°¡¼­ ÀÔ´Ï´Ù ±è¾Æ¿µ
2003-06-21 17:58 [Caml-list] ½Å¿ë È®ÀÎÇϼ¼¿ä ±è¼ö¿µ
2003-06-21 17:58 ±è¼ö¿µ
2003-06-11 15:05 ±è¼ö¿µ
2003-06-11 15:05 ±è¼ö¿µ
2003-06-11 11:49 [Caml-list] ÆóÒµ¼Ò԰ȫиİ棬»¶Ó­·ÃÎÊ£¡ bepark
2003-06-07 19:43 [Caml-list] [Á¤º¸] °Ç°­Á¤º¸ÀÔ´Ï´Ù °Ç°­ÁöÅ´ÀÌ
2003-06-05 21:23 [Caml-list] ´ç½ÅÀÇ ½Å¿ëÆò°¡ ÀÔ´Ï´Ù ÇѼҿµ
2003-06-05 21:23 ÇѼҿµ
2003-06-03 17:38 [Caml-list] »çÀ̹ö ¼ö»ç´ëÀÔ´Ï´Ù ÇѼҿµ
2003-06-03 17:38 ÇѼҿµ
2003-05-01  8:40 [Caml-list] ÄãÒªµÄÈí¼þ£¬ÎÒ°ïÄãÕÒµ½ÁË£¬¿´¿´ÊDz»ÊÇÄãÒª°¡£º£© uxltiszukx@bea.com
2001-08-03 14:39 [Caml-list] == Willem Duminy
2001-08-03 14:52 ` Sven

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=201109141900.p8EJ019C032522@walapai.inria.fr \
    --to=ds.caml@sol42.com \
    /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;
as well as URLs for NNTP newsgroup(s).