1TRAMPOLINE(3) Library Functions Manual TRAMPOLINE(3)
2
3
4
6 trampoline - closures as first-class C functions
7
9 #include <trampoline.h>
10
11 function = alloc_trampoline(address, variable, data);
12
13 free_trampoline(function);
14
15 is_trampoline(function)
16 trampoline_address(function)
17 trampoline_variable(function)
18 trampoline_data(function)
19
21 These functions implement closures as first-class C functions. A clo‐
22 sure consists of a regular C function and a piece of data which gets
23 passed to the C function when the closure is called.
24
25 Closures as first-class C functions means that they fit into a function
26 pointer and can be called exactly like any other C function. function
27 = alloc_trampoline(address, variable, data) allocates a closure. When
28 function gets called, it stores data in the variable variable and calls
29 the C function at address. The function at address is responsible for
30 fetching data out of variable immediately, before execution of any
31 other function call.
32
33 This is much like gcc's local functions, except that the GNU C local
34 functions have dynamic extent (i.e. are deallocated when the creating
35 function returns), while trampoline provides functions with indefinite
36 extent: function is only deallocated when free_trampoline(function) is
37 called.
38
39 is_trampoline(function) checks whether the C function function was pro‐
40 duced by a call to alloc_trampoline. If this returns true, the argu‐
41 ments given to alloc_trampoline can be retrieved:
42
43 trampoline_address(function) returns address,
44
45 trampoline_variable(function) returns variable,
46
47 trampoline_data(function) returns data.
48
49
51 gcc(1), stdarg(3), callback(3)
52
53
55 Passing the data through a global variable is not reentrant. Don't call
56 trampoline functions from within signal handlers. This is fixed in the
57 callback(3) package.
58
59
61 The way gcc builds local functions is described in the gcc source, file
62 gcc-2.6.3/config/cpu/cpu.h.
63
64
66 Bruno Haible <bruno@clisp.org>
67
68
70 Many ideas were cribbed from the gcc source.
71
72
73
74
75 1 January 2017 TRAMPOLINE(3)