From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA21330; Wed, 14 Nov 2001 21:25:12 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA21374 for ; Wed, 14 Nov 2001 21:25:11 +0100 (MET) Received: from relay.rinet.ru (relay.rinet.ru [195.54.192.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id fAEKPAX07872 for ; Wed, 14 Nov 2001 21:25:10 +0100 (MET) Received: (from uucp@localhost) by relay.rinet.ru (8.11.6/8.11.6) with UUCP id fAEKP9Z21243 for caml-list@inria.fr; Wed, 14 Nov 2001 23:25:09 +0300 (MSK) X-Envelope-To: caml-list@inria.fr Received: from bely.stormoff (BELY) [192.168.0.10] by stormoff with esmtp (Exim 2.05 #1 (Debian)) id 1646Wf-0006EH-00; Wed, 14 Nov 2001 23:20:49 +0300 X-Comment-To: Dmitry Bely To: caml-list@inria.fr Subject: Re: [Caml-list] camlidl and pointer to function References: <3BF23EE4.BB444C5C@crf.canon.fr> <9stni6$hh6$1@qrnik.zagroda> From: Dmitry Bely Date: 14 Nov 2001 23:21:54 +0300 In-Reply-To: Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Civil Service (Windows)) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Dmitry Bely writes: > "Marcin 'Qrczak' Kowalczyk" writes: > > > > The problem is that C library is binary-only (Intel image processing > > > library), and CallBack type is exactly > > > > > > typedef int (*CallBack)(int); > > > > Converting a function closure to a C function pointer can't be done > > portably, but it can be done with lots of ugly magic. > > > > Glasgow Haskell does this (by generating a piece of assembler on > > the heap) and it's convenient to use from the level of Haskell. > > Such functions need to be explicitly freed of course. > > > > GNU C does this for local functions, but only "downwards". If the > > function pointer doesn't need to live longer than the function which > > installs the callback, the GNU C extension can be used. It generates > > the piece of assembler on the stack. To use it - just define a function > > inside a function and take its address. > > Hmm, this trick assumes that the stack is executable (IMHO, the serious > security hole). Don't know about Linux/x86 page/segment attributes policy, > but I am almost sure that Windows NT/2000 x86 (which is my platform) will > not allow to execute the code in data/stack pages. Sorry, in fact i386 and above does not seems to have page-level execute permissions, so this trick will work on all i386 flat model-based OSes (I've tried it under Win2000). Now I would like to see this implemented in camlidl :-) Here is the way I am going to use right now: [---cut---] typedef [abstract] void* SomeType; // typedef RES (CALLBACK*)(PARAM); typedef [abstract] void* CALLBACK; typedef int RES; typedef int PARM; void convert( SomeType* in, SomeType* out, CALLBACK cb) quote(call,"\ { \n\ value v = *(value*)cb; \n\ RES _cb(PARM p) \n\ { \n\ value _v_p; \n\ RES _res; \n\ value _vres; \n\ \n\ _v_p = camlidl_c2ml_cback_PARM(&p,_ctx); \n\ _vres = callback(v,p); \n\ camlidl_ml2c_cback_RES(_vres,&_res,_ctx); \n\ return _res; \n\ } \n\ convert(in,out,_cb); \n\ } \n\ "); [---cut---] Unfortinately, I cannot define such macro for camlidl, as it does not allow C-style string concatenation inside quote statement... > > It would be nice if OCaml provided this functionality because not > > all C libraries provide the extra argument for simulating closures. > > Unfortunately it can't be implemented nicely. > > > > I've once seen a C library which tries to provide it for several > > platforms but I forgot its name. > > It would be nice if you recall it :-) Hope to hear from you soon, Dmitry ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr