From: Dmitry Bely <dbely@mail.ru>
To: caml-list@inria.fr
Subject: Re: [Caml-list] camlidl and pointer to function
Date: 14 Nov 2001 23:21:54 +0300 [thread overview]
Message-ID: <eln1umql.fsf@mail.ru> (raw)
In-Reply-To: <itcduyq4.fsf@mail.ru>
Dmitry Bely <dbely@mail.ru> writes:
> "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl> 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
next prev parent reply other threads:[~2001-11-14 20:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-11-13 17:43 Dmitry Bely
2001-11-14 9:52 ` NASSOR Eric
2001-11-14 12:16 ` Dmitry Bely
[not found] ` <9stni6$hh6$1@qrnik.zagroda>
2001-11-14 14:56 ` Marcin 'Qrczak' Kowalczyk
2001-11-14 16:02 ` Dmitry Bely
2001-11-14 20:21 ` Dmitry Bely [this message]
2001-11-14 20:43 Krishnaswami, Neel
2001-11-15 8:23 ` Dmitry Bely
[not found] <9sul5k$ga4$1@qrnik.zagroda>
2001-11-14 21:24 ` Marcin 'Qrczak' Kowalczyk
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=eln1umql.fsf@mail.ru \
--to=dbely@mail.ru \
--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