4
TRAMPOLINE(3) TRAMPOLINE(3)
8
trampoline - closures as first-class C functions
11
##iinncclluuddee <<ttrraammppoolliinnee__rr..hh>>
13
ffuunnccttiioonn == aalllloocc__ttrraammppoolliinnee__rr((aaddddrreessss,, ddaattaa00,, ddaattaa11));;
15
ffrreeee__ttrraammppoolliinnee__rr((ffuunnccttiioonn));;
17
iiss__ttrraammppoolliinnee__rr((ffuunnccttiioonn))
18
ttrraammppoolliinnee__rr__aaddddrreessss((ffuunnccttiioonn))
19
ttrraammppoolliinnee__rr__ddaattaa00((ffuunnccttiioonn))
20
ttrraammppoolliinnee__rr__ddaattaa11((ffuunnccttiioonn))
22
DDEESSCCRRIIPPTTIIOONN
23
These functions implement _c_l_o_s_u_r_e_s as first-class C func-
24
tions. A closure consists of a regular C function and a
25
piece of data which gets passed to the C function when the
28
Closures as _f_i_r_s_t_-_c_l_a_s_s _C _f_u_n_c_t_i_o_n_s means that they fit
29
into a function pointer and can be called exactly like any
30
other C function. _f_u_n_c_t_i_o_n == aalllloocc__ttrraammppoolliinnee__rr((_a_d_d_r_e_s_s,,
31
_d_a_t_a_0,, _d_a_t_a_1)) allocates a closure. When _f_u_n_c_t_i_o_n gets
32
called, it stores in a special "lexical chain register" a
33
pointer to a storage area containing _d_a_t_a_0 in its first
34
word and _d_a_t_a_1 in its second word and calls the C function
35
at _a_d_d_r_e_s_s. The function at _a_d_d_r_e_s_s is responsible for
36
fetching _d_a_t_a_0 and _d_a_t_a_1 off the pointer. Note that the
37
"lexical chain register" is a call-used register, i.e. is
38
clobbered by function calls.
40
This is much like ggcccc's local functions, except that the
41
GNU C local functions have dynamic extent (i.e. are deal-
42
located when the creating function returns), while _t_r_a_m_p_o_-
43
_l_i_n_e provides functions with indefinite extent: _f_u_n_c_t_i_o_n
44
is only deallocated when ffrreeee__ttrraammppoolliinnee__rr((_f_u_n_c_t_i_o_n)) is
47
iiss__ttrraammppoolliinnee__rr((_f_u_n_c_t_i_o_n)) checks whether the C function
48
_f_u_n_c_t_i_o_n was produced by a call to _a_l_l_o_c___t_r_a_m_p_o_l_i_n_e___r. If
49
this returns true, the arguments given to _a_l_l_o_c___t_r_a_m_p_o_-
50
_l_i_n_e___r can be retrieved:
52
ttrraammppoolliinnee__rr__aaddddrreessss((_f_u_n_c_t_i_o_n)) returns _a_d_d_r_e_s_s,
54
ttrraammppoolliinnee__rr__ddaattaa00((_f_u_n_c_t_i_o_n)) returns _d_a_t_a_0,
56
ttrraammppoolliinnee__rr__ddaattaa11((_f_u_n_c_t_i_o_n)) returns _d_a_t_a_1.
60
ttrraammppoolliinnee(3), ggcccc(1), vvaarraarrggss(3)
70
TRAMPOLINE(3) TRAMPOLINE(3)
74
The way ggcccc builds local functions is described in the gcc
75
source, file gcc-2.6.3/config/_c_p_u/_c_p_u.h.
79
Bruno Haible <bruno@clisp.org>
82
AACCKKNNOOWWLLEEDDGGEEMMEENNTTSS
83
Many ideas were cribbed from the gcc source.