TRAMPOLINE(3)
π TRAMPOLINE(3) π
ok bestie, welcome to the Library Functions Manual page for trampoline β yes that's the manual name, and yes we're about to make closures as first-class C functions sound as cute as they actually are. β¨
π NAME π
trampoline β closures as first-class C functions π
β¨ SYNOPSIS β¨
#include <trampoline.h>
function = alloc_trampoline(address, variable, data);
free_trampoline(function);
is_trampoline(function)
trampoline_address(function)
trampoline_variable(function)
trampoline_data(function)
π¦ DESCRIPTION π¦
So these functions implement closures as first-class C functions. Periodt. A closure is literally just a regular C function plus a lil piece of data that gets passed to that C function whenever the closure gets called. π«Ά
And when we say first-class C functions, we mean they fit into a function pointer and can be called exactly like any other C function. No special treatment, no weird ceremony. π
function = alloc_trampoline(address, variable, data) allocates a closure. When function gets called, it stores data in the variable variable and calls the C function at address. The function at address is responsible for fetching data out of variable immediately, before execution of any other function call. Like, before literally anything else happens. Don't be late to that party. β°
This is much like gcc's local functions, except that the GNU C local functions have dynamic extent (i.e. are deallocated when the creating function returns), while trampoline provides functions with indefinite extent: function is only deallocated when free_trampoline(function) is called. So it sticks around. Main character energy, honestly. π
is_trampoline(function) checks whether the C function function was produced by a call to alloc_trampoline. If this returns true, the arguments given to alloc_trampoline can be retrieved:
trampoline_address(function)returnsaddress,trampoline_variable(function)returnsvariable,trampoline_data(function)returnsdata.
π SEE ALSO π
gcc(1), stdarg(3), callback(3)
π BUGS π
Passing the data through a global variable is not reentrant. Not me warning you about this. π Don't call trampoline functions from within signal handlers. This is fixed in the callback(3) package. π«Ά
π§ PORTING π§
The way gcc builds local functions is described in the gcc source, file gcc-2.6.3/config/cpu/cpu.h. iykyk. π
π AUTHOR π
Bruno Haible <bruno@clisp.org>
πΈ ACKNOWLEDGEMENTS πΈ
Many ideas were cribbed from the gcc source. Respectfully. π
and that's a wrap, bestie π this was the TRAMPOLINE(3) page, dated 1 January 2017, and it's giving iconic. slay, stay safe, and remember to free_trampoline(function) when you're done. periodt πβ¨
π¦ where this page comes from
- alpine 3.24: ffcall-doc 2.5-r0 Β· GPL-2.0-or-later
- arch rolling: ffcall 2.5-1 Β· GPL-2.0-or-later
- debian bookworm: libffcall-dev 2.4-2 Β· GPL-2.0-or-later
- debian trixie: libffcall-dev 2.5-2 Β· GPL-2.0-or-later
- nixpkgs unstable: libffcall 2.5
- ubuntu jammy: libffcall-dev 2.4-1 Β· GPL-2.0-or-later
- ubuntu noble: libffcall-dev 2.4-2.1build1 Β· GPL-2.0-or-later
- ubuntu resolute: libffcall-dev 2.5-2 Β· GPL-2.0-or-later
- ubuntu stonking: libffcall-dev 2.5-2ubuntu1 Β· GPL-2.0-or-later
- Copyright (C) 1995-2017 Bruno Haible
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms