TRAMPOLINE(3) TRAMPOLINE(3) NNAAMMEE trampoline - closures as first-class C functions SSYYNNOOPPSSIISS ##iinncclluuddee <<ttrraammppoolliinnee__rr..hh>> ffuunnccttiioonn == aalllloocc__ttrraammppoolliinnee__rr((aaddddrreessss,, ddaattaa00,, ddaattaa11));; ffrreeee__ttrraammppoolliinnee__rr((ffuunnccttiioonn));; iiss__ttrraammppoolliinnee__rr((ffuunnccttiioonn)) ttrraammppoolliinnee__rr__aaddddrreessss((ffuunnccttiioonn)) ttrraammppoolliinnee__rr__ddaattaa00((ffuunnccttiioonn)) ttrraammppoolliinnee__rr__ddaattaa11((ffuunnccttiioonn)) DDEESSCCRRIIPPTTIIOONN These functions implement _c_l_o_s_u_r_e_s as first-class C func- tions. 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. 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 into a function pointer and can be called exactly like any other C function. _f_u_n_c_t_i_o_n == aalllloocc__ttrraammppoolliinnee__rr((_a_d_d_r_e_s_s,, _d_a_t_a_0,, _d_a_t_a_1)) allocates a closure. When _f_u_n_c_t_i_o_n gets called, it stores in a special "lexical chain register" a pointer to a storage area containing _d_a_t_a_0 in its first word and _d_a_t_a_1 in its second word and calls the C function at _a_d_d_r_e_s_s. The function at _a_d_d_r_e_s_s is responsible for fetching _d_a_t_a_0 and _d_a_t_a_1 off the pointer. Note that the "lexical chain register" is a call-used register, i.e. is clobbered by function calls. This is much like ggcccc's local functions, except that the GNU C local functions have dynamic extent (i.e. are deal- located when the creating function returns), while _t_r_a_m_p_o_- _l_i_n_e provides functions with indefinite extent: _f_u_n_c_t_i_o_n is only deallocated when ffrreeee__ttrraammppoolliinnee__rr((_f_u_n_c_t_i_o_n)) is called. iiss__ttrraammppoolliinnee__rr((_f_u_n_c_t_i_o_n)) checks whether the C function _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 this returns true, the arguments given to _a_l_l_o_c___t_r_a_m_p_o_- _l_i_n_e___r can be retrieved: ttrraammppoolliinnee__rr__aaddddrreessss((_f_u_n_c_t_i_o_n)) returns _a_d_d_r_e_s_s, ttrraammppoolliinnee__rr__ddaattaa00((_f_u_n_c_t_i_o_n)) returns _d_a_t_a_0, ttrraammppoolliinnee__rr__ddaattaa11((_f_u_n_c_t_i_o_n)) returns _d_a_t_a_1. SSEEEE AALLSSOO ttrraammppoolliinnee(3), ggcccc(1), vvaarraarrggss(3) 22 October 1997 1 TRAMPOLINE(3) TRAMPOLINE(3) PPOORRTTIINNGG The way ggcccc builds local functions is described in the gcc source, file gcc-2.6.3/config/_c_p_u/_c_p_u.h. AAUUTTHHOORR Bruno Haible <bruno@clisp.org> AACCKKNNOOWWLLEEDDGGEEMMEENNTTSS Many ideas were cribbed from the gcc source. 22 October 1997 2