1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
.TH TRAMPOLINE 3 "22 October 1997"
.SH NAME
trampoline \- closures as first-class C functions
.SH SYNOPSIS
.B #include <trampoline_r.h>
.LP
.B function = alloc_trampoline_r(address, data0, data1);
.LP
.B free_trampoline_r(function);
.LP
.nf
.B is_trampoline_r(function)
.B trampoline_r_address(function)
.B trampoline_r_data0(function)
.B trampoline_r_data1(function)
.fi
.SH DESCRIPTION
.LP
These functions implement
.I closures
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.
Closures as
.I first-class C functions
means that they fit into a function
pointer and can be called exactly like any other C function.
.IB function " = alloc_trampoline_r(" address ", " data0 ", " data1 ")"
allocates a closure. When
.I function
gets called, it stores in a special "lexical chain register" a pointer to a
storage area containing
.I data0
in its first word and
.I data1
in its second word and calls the C function at
.IR address .
The function at
.I address
is responsible for fetching
.I data0
and
.I data1
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
.BR gcc "'s"
local functions, except that the GNU C local functions have dynamic extent
(i.e. are deallocated when the creating function returns), while
.I trampoline
provides functions with indefinite extent:
.I function
is only deallocated when
.BI free_trampoline_r( function )
is called.
.BI "is_trampoline_r(" function ")"
checks whether the C function
.I function
was produced by a call to
.IR alloc_trampoline_r .
If this returns true, the arguments given to
.I alloc_trampoline_r
can be retrieved:
.RS 4
.LP
.BI "trampoline_r_address(" function ")"
returns
.IR address ,
.LP
.BI "trampoline_r_data0(" function ")"
returns
.IR data0 ,
.LP
.BI "trampoline_r_data1(" function ")"
returns
.IR data1 .
.RE
.SH SEE ALSO
.BR trampoline (3),
.BR gcc (1),
.BR varargs (3)
.SH PORTING
The way
.B gcc
builds local functions is described in the gcc source, file
.RI gcc-2.6.3/config/ cpu / cpu .h.
.SH AUTHOR
Bruno Haible <bruno@clisp.org>
.SH ACKNOWLEDGEMENTS
Many ideas were cribbed from the gcc source.
|