2
<TITLE> TRAMPOLINE_R manual page </TITLE>
5
<H1>TRAMPOLINE_R manual page</H1>
8
<LI> <A HREF="#Name">Name</A>
9
<LI> <A HREF="#Synopsis">Synopsis</A>
10
<LI> <A HREF="#Description">Description</A>
11
<LI> <A HREF="#See also">See also</A>
12
<LI> <A HREF="#Porting">Porting</A>
13
<LI> <A HREF="#Author">Author</A>
14
<LI> <A HREF="#Acknowledgements">Acknowledgements</A>
24
trampoline_r - closures as first-class C functions
31
<CODE>#include <trampoline_r.h></CODE>
32
<CODE><VAR>function</VAR> = alloc_trampoline_r(<VAR>address</VAR>, <VAR>data0</VAR>, <VAR>data1</VAR>);</CODE>
33
<CODE>free_trampoline_r(<VAR>function</VAR>);</CODE>
34
<CODE>is_trampoline_r(<VAR>function</VAR>)</CODE>
35
<CODE>trampoline_r_address(<VAR>function</VAR>)</CODE>
36
<CODE>trampoline_r_data0(<VAR>function</VAR>)</CODE>
37
<CODE>trampoline_r_data1(<VAR>function</VAR>)</CODE>
40
<A NAME="Description">
44
These functions implement <EM>closures</EM> as first-class
45
C functions. A closure consists of a regular C function and a
46
piece of data which gets passed to the C function when the
49
Closures as <EM>first-class C functions</EM> means that they fit
50
into a function pointer and can be called exactly like any
51
other C function. <CODE><VAR>function</VAR> = alloc_trampoline_r(<VAR>address</VAR>, <VAR>data0</VAR>, <VAR>data1</VAR>)</CODE>
52
allocates a closure. When <VAR>function</VAR> gets
53
called, it stores in a special "lexical chain register" a
54
pointer to a storage area containing <VAR>data0</VAR> in its first
55
word and <VAR>data1</VAR> in its second word and calls the C function
56
at <VAR>address</VAR>. The function at <VAR>address</VAR> is responsible for
57
fetching <VAR>data0</VAR> and <VAR>data1</VAR> off the pointer. Note that the
58
"lexical chain register" is a call-used register, i.e. is
59
clobbered by function calls.
61
This is much like gcc's local functions, except that the
62
GNU C local functions have dynamic extent (i.e. are
63
deallocated when the creating function returns), while <EM>trampoline</EM>
64
provides functions with indefinite extent: <CODE><VAR>function</VAR></CODE>
65
is only deallocated when <CODE>free_trampoline_r(<VAR>function</VAR>)</CODE> is
68
<CODE>is_trampoline_r(<VAR>function</VAR>)</CODE>
69
checks whether the C function <CODE><VAR>function</VAR></CODE>
70
was produced by a call to <CODE>alloc_trampoline_r</CODE>.
71
If this returns true, the arguments given to <CODE>alloc_trampoline_r</CODE>
74
<LI> <CODE>trampoline_r_address(<VAR>function</VAR>)</CODE> returns <VAR>address</VAR>,
75
<LI> <CODE>trampoline_r_data0(<VAR>function</VAR>)</CODE> returns <VAR>data0</VAR>,
76
<LI> <CODE>trampoline_r_data1(<VAR>function</VAR>)</CODE> returns <VAR>data1</VAR>.
82
<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)
88
The way gcc builds local functions is described in the gcc
89
source, file <SAMP>gcc-2.6.3/config/<VAR>cpu</VAR>/<VAR>cpu</VAR>.h</SAMP>.
95
Bruno Haible <bruno@clisp.org>
97
<A NAME="Acknowledgements">
98
<H2>Acknowledgements</H2>
101
Many ideas were cribbed from the gcc source.
106
<ADDRESS>TRAMPOLINE_R manual page<BR>
107
Bruno Haible <bruno@clisp.org>
110
Last modified: 22 October 1997.