File ffcall/callback/trampoline_r/trampoline_r.html artifact bd8e2a74e1 part of check-in 4b31034008


<HEAD>
<TITLE> TRAMPOLINE_R manual page </TITLE>
</HEAD>
<BODY>
<H1>TRAMPOLINE_R manual page</H1>

<UL>
<LI> <A HREF="#Name">Name</A>
<LI> <A HREF="#Synopsis">Synopsis</A>
<LI> <A HREF="#Description">Description</A>
<LI> <A HREF="#See also">See also</A>
<LI> <A HREF="#Porting">Porting</A>
<LI> <A HREF="#Author">Author</A>
<LI> <A HREF="#Acknowledgements">Acknowledgements</A>
</UL>
<P>

<HR>

<A NAME="Name">
<H2>Name</H2>
</A>

trampoline_r -  closures as first-class C functions

<A NAME="Synopsis">
<H2>Synopsis</H2>
</A>

<PRE>
<CODE>#include &lt;trampoline_r.h&gt;</CODE>
<CODE><VAR>function</VAR> = alloc_trampoline_r(<VAR>address</VAR>, <VAR>data0</VAR>, <VAR>data1</VAR>);</CODE>
<CODE>free_trampoline_r(<VAR>function</VAR>);</CODE>
<CODE>is_trampoline_r(<VAR>function</VAR>)</CODE>
<CODE>trampoline_r_address(<VAR>function</VAR>)</CODE>
<CODE>trampoline_r_data0(<VAR>function</VAR>)</CODE>
<CODE>trampoline_r_data1(<VAR>function</VAR>)</CODE>
</PRE>

<A NAME="Description">
<H2>Description</H2>
</A>

These  functions implement <EM>closures</EM>  as first-class
C functions.  A closure consists of a regular C function  and  a
piece of data which gets passed to the C function when the
closure is called.
<P>
Closures as <EM>first-class C functions</EM> means  that  they  fit
into a function pointer and can be called exactly like any
other C function.  <CODE><VAR>function</VAR> =  alloc_trampoline_r(<VAR>address</VAR>, <VAR>data0</VAR>, <VAR>data1</VAR>)</CODE>
allocates  a  closure.  When <VAR>function</VAR> gets
called, it stores in a special &quot;lexical chain register&quot;  a
pointer  to  a  storage area containing <VAR>data0</VAR> in its first
word and <VAR>data1</VAR> in its second word and calls the C function
at  <VAR>address</VAR>.   The  function at <VAR>address</VAR> is responsible for
fetching <VAR>data0</VAR> and  <VAR>data1</VAR>  off  the  pointer.  Note  that  the
&quot;lexical  chain register&quot; is a call-used register, i.e. is
clobbered by function calls.
<P>
This is much like gcc's local functions, except  that  the
GNU  C local functions have dynamic extent (i.e. are
deallocated when the creating function returns), while <EM>trampoline</EM>
  provides  functions with indefinite extent: <CODE><VAR>function</VAR></CODE>
is  only  deallocated  when  <CODE>free_trampoline_r(<VAR>function</VAR>)</CODE>  is
called.
<P>
<CODE>is_trampoline_r(<VAR>function</VAR>)</CODE>
checks  whether  the  C  function <CODE><VAR>function</VAR></CODE>
was  produced by a call to <CODE>alloc_trampoline_r</CODE>.
If this returns true, the arguments given to <CODE>alloc_trampoline_r</CODE>
can  be  retrieved:
<UL>
<LI> <CODE>trampoline_r_address(<VAR>function</VAR>)</CODE> returns <VAR>address</VAR>,
<LI> <CODE>trampoline_r_data0(<VAR>function</VAR>)</CODE> returns <VAR>data0</VAR>,
<LI> <CODE>trampoline_r_data1(<VAR>function</VAR>)</CODE> returns <VAR>data1</VAR>.
</UL>

<A NAME="See also">
<H2>See also</H2>
</A>
<A HREF="trampoline(3)"><CODE><B>trampoline</B></CODE></A>(3), <A HREF="gcc(1)"><CODE><B>gcc</B></CODE></A>(1), <A HREF="varargs(3)"><CODE><B>varargs</B></CODE></A>(3)

<A NAME="Porting">
<H2>Porting</H2>
</A>

The way gcc builds local functions is described in the gcc
source, file <SAMP>gcc-2.6.3/config/<VAR>cpu</VAR>/<VAR>cpu</VAR>.h</SAMP>.

<A NAME="Author">
<H2>Author</H2>
</A>

Bruno Haible &lt;bruno@clisp.org&gt;

<A NAME="Acknowledgements">
<H2>Acknowledgements</H2>
</A>

Many ideas were cribbed from the gcc source.
<P>

<HR>

<ADDRESS>TRAMPOLINE_R manual page<BR>
Bruno Haible &lt;bruno@clisp.org&gt;
</ADDRESS>
<P>
Last modified: 22 October 1997.

</BODY>