.\" Copyright (C) 1995-2017 Bruno Haible .\" .\" This manual is free documentation. It is dually licensed under the .\" GNU FDL and the GNU GPL. This means that you can redistribute this .\" manual under either of these two licenses, at your choice. .\" .\" This manual is covered by the GNU FDL. Permission is granted to copy, .\" distribute and/or modify this document under the terms of the .\" GNU Free Documentation License (FDL), either version 1.2 of the .\" License, or (at your option) any later version published by the .\" Free Software Foundation (FSF); with no Invariant Sections, with no .\" Front-Cover Text, and with no Back-Cover Texts. .\" A copy of the license is at . .\" .\" This manual is covered by the GNU GPL. You can redistribute it and/or .\" modify it under the terms of the GNU General Public License (GPL), either .\" version 2 of the License, or (at your option) any later version published .\" by the Free Software Foundation (FSF). .\" A copy of the license is at . .\" .TH TRAMPOLINE 3 "1 January 2017" .SH NAME trampoline \- closures as first-class C functions .SH SYNOPSIS .B #include .LP .B function = alloc_trampoline(address, variable, data); .LP .B free_trampoline(function); .LP .nf .B is_trampoline(function) .B trampoline_address(function) .B trampoline_variable(function) .B trampoline_data(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(" address ", " variable ", " data ")" allocates a closure. When .I function gets called, it stores .I data in the variable .I variable and calls the C function at .IR address . The function at .I address is responsible for fetching .I data out of .I variable immediately, before execution of any other function call. 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( function ) is called. .BI "is_trampoline(" function ")" checks whether the C function .I function was produced by a call to .IR alloc_trampoline . If this returns true, the arguments given to .I alloc_trampoline can be retrieved: .RS 4 .LP .BI "trampoline_address(" function ")" returns .IR address , .LP .BI "trampoline_variable(" function ")" returns .IR variable , .LP .BI "trampoline_data(" function ")" returns .IR data . .RE .SH SEE ALSO .BR gcc (1), .BR stdarg (3), .BR callback (3) .SH BUGS Passing the data through a global variable is not reentrant. Don't call trampoline functions from within signal handlers. This is fixed in the .BR callback (3) package. .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 .SH ACKNOWLEDGEMENTS Many ideas were cribbed from the gcc source.