2
<TITLE> CALLBACK manual page </TITLE>
5
<H1>CALLBACK manual page</H1>
8
<LI> <A HREF="#Name">Name</A>
9
<LI> <A HREF="#Synopsis">Synopsis</A>
10
<LI> <A HREF="#Description">Description</A>
11
<LI> <A HREF="#VACALL macros">VACALL macros</A>
12
<LI> <A HREF="#Notes">Notes</A>
13
<LI> <A HREF="#See also">See also</A>
14
<LI> <A HREF="#Bugs">Bugs</A>
15
<LI> <A HREF="#Non-Bugs">Non-Bugs</A>
16
<LI> <A HREF="#Porting">Porting</A>
17
<LI> <A HREF="#Author">Author</A>
18
<LI> <A HREF="#Acknowledgements">Acknowledgements</A>
28
callback - closures with variable arguments as first-class
36
<CODE>#include <callback.h></CODE>
40
<CODE>void <VAR>function</VAR> (<VAR>data</VAR>, <VAR>alist</VAR>)</CODE>
41
<CODE> void* <VAR>data</VAR>;</CODE>
42
<CODE> va_alist <VAR>alist</VAR>;</CODE>
44
<CODE> va_start_<VAR>type</VAR>(<VAR>alist</VAR>[, <VAR>return_type</VAR>]);</CODE>
45
<CODE> arg = va_arg_<VAR>type</VAR>(<VAR>alist</VAR>[, <VAR>arg_type</VAR>]);</CODE>
46
<CODE> va_return_<VAR>type</VAR>(<VAR>alist</VAR>[[, <VAR>return_type</VAR>], <VAR>return_value</VAR>]);</CODE>
51
<CODE><VAR>callback</VAR> = alloc_callback(<VAR>&function</VAR>, <VAR>data</VAR>);</CODE>
55
<CODE>free_callback(<VAR>callback</VAR>);</CODE>
59
<CODE>is_callback(<VAR>callback</VAR>)</CODE>
60
<CODE>callback_address(<VAR>callback</VAR>)</CODE>
61
<CODE>callback_data(<VAR>callback</VAR>)</CODE>
64
<A NAME="Description">
68
These functions implement <EM>closures</EM> with variable arguments
69
as first-class C functions.
71
Closures as <EM>first-class C functions</EM> means that they fit
72
into a function pointer and can be called exactly like any
73
other C function. Moreover, they can be called with variable
74
arguments and can return variable return values.
76
<CODE><VAR>callback</VAR> = alloc_callback(<VAR>&function</VAR>, <VAR>data</VAR>)</CODE>
78
callback. When <VAR>callback</VAR> gets called, it arranges to call
79
<VAR>function</VAR>, passing <VAR>data</VAR> as first argument and, as second
80
argument, the entire sequence of arguments passed to <VAR>callback</VAR>.
82
Function calling conventions differ considerably on different
83
machines, therefore the arguments are accessed and
84
the result value is stored through the same macros as used
85
by the <EM>vacall</EM> package, see below.
87
The callbacks are functions with indefinite extent: <CODE><VAR>callback</VAR></CODE>
88
is only deallocated when <CODE>free_callback(<VAR>callback</VAR>)</CODE> is
91
<CODE>is_callback(<VAR>callback</VAR>)</CODE>
92
checks whether the C function <CODE><VAR>callback</VAR></CODE>
93
was produced by a call to <CODE>alloc_callback</CODE>. If this
94
returns true, the arguments given to <CODE>alloc_callback</CODE> can be
97
<LI> <CODE>callback_address(<VAR>callback</VAR>)</CODE> returns <VAR>&function</VAR>,
98
<LI> <CODE>callback_data(<VAR>callback</VAR>)</CODE> returns <VAR>data</VAR>.
101
<A NAME="VACALL macros">
102
<H2>VACALL macros</H2>
105
Within <VAR>function</VAR>, the following macros can be used to walk
106
through the argument list and specify a return value:
109
<CODE>va_start_<VAR>type</VAR>(<VAR>alist</VAR>[, <VAR>return_type</VAR>]);</CODE>
111
starts the walk through the argument list and specifies the return type.
114
<CODE>arg = va_arg_<VAR>type</VAR>(<VAR>alist</VAR>[, <VAR>arg_type</VAR>]);</CODE>
116
fetches the next argument from the argument list.
119
<CODE>va_return_<VAR>type</VAR>(<VAR>alist</VAR>[[, <VAR>return_type</VAR>], <VAR>return_value</VAR>]);</CODE>
121
ends the walk through the argument list and specifies the return value.
123
The <VAR>type</VAR> in <CODE>va_start_<VAR>type</VAR></CODE>
124
and <CODE>va_return_<VAR>type</VAR></CODE> shall be one
125
of <CODE>void</CODE>, <CODE>int</CODE>, <CODE>uint</CODE>, <CODE>long</CODE>,
126
<CODE>ulong</CODE>, <CODE>longlong</CODE>, <CODE>ulonglong</CODE>,
127
<CODE>double</CODE>, <CODE>struct</CODE>, <CODE>ptr</CODE>
129
(for ANSI C calling conventions only)
130
<CODE>char</CODE>, <CODE>schar</CODE>, <CODE>uchar</CODE>,
131
<CODE>short</CODE>, <CODE>ushort</CODE>, <CODE>float</CODE>,
132
depending on the class of <VAR>return_type</VAR>.
134
The <VAR>type</VAR> specifiers in
135
<CODE>va_start_<VAR>type</VAR></CODE> and <CODE>va_return_<VAR>type</VAR></CODE>
137
The <VAR>return_type</VAR> specifiers passed to
138
<CODE>va_start_<VAR>type</VAR></CODE> and <CODE>va_return_<VAR>type</VAR></CODE>
141
The <VAR>type</VAR> in <CODE>va_arg_<VAR>type</VAR></CODE>
142
shall be one of <CODE>int</CODE>, <CODE>uint</CODE>, <CODE>long</CODE>,
143
<CODE>ulong</CODE>, <CODE>longlong</CODE>, <CODE>ulonglong</CODE>,
144
<CODE>double</CODE>, <CODE>struct</CODE>, <CODE>ptr</CODE>
145
or (for ANSI C calling conventions only)
146
<CODE>char</CODE>, <CODE>schar</CODE>, <CODE>uchar</CODE>,
147
<CODE>short</CODE>, <CODE>ushort</CODE>, <CODE>float</CODE>,
148
depending on the class of <VAR>arg_type</VAR>.
150
In <CODE>va_start_struct(<VAR>alist</VAR>, <VAR>return_type</VAR>, <VAR>splittable</VAR>);</CODE> the
151
<VAR>splittable</VAR> flag specifies whether the struct <VAR>return_type</VAR> can
152
be returned in registers such that every struct field fits
153
entirely in a single register. This needs to be specified
154
for structs of size <SAMP>2*sizeof(long)</SAMP>. For structs of size
155
<= <SAMP>sizeof(long)</SAMP>, splittable is ignored and assumed to be 1.
156
For structs of size > <SAMP>2*sizeof(long)</SAMP>, splittable is
157
ignored and assumed to be 0. There are some handy macros
160
<CODE>va_word_splittable_1 (<VAR>type1</VAR>)</CODE>
161
<CODE>va_word_splittable_2 (<VAR>type1</VAR>, <VAR>type2</VAR>)</CODE>
162
<CODE>va_word_splittable_3 (<VAR>type1</VAR>, <VAR>type2</VAR>, <VAR>type3</VAR>)</CODE>
163
<CODE>va_word_splittable_4 (<VAR>type1</VAR>, <VAR>type2</VAR>, <VAR>type3</VAR>, <VAR>type4</VAR>)</CODE>
165
For a struct with three slots
167
<CODE>struct { <VAR>type1 id1</VAR>; <VAR>type2 id2</VAR>; <VAR>type3 id3</VAR>; }</CODE>
169
you can specify <VAR>splittable</VAR> as
170
<CODE>va_word_splittable_3 (<VAR>type1</VAR>, <VAR>type2</VAR>, <VAR>type3</VAR>)</CODE>.
177
<LI> Functions which want to emulate Kernighan & Ritchie style
178
functions (i.e., in ANSI C, functions without a typed
179
argument list) cannot use the <VAR>type</VAR> values
180
<CODE>char</CODE>, <CODE>schar</CODE>, <CODE>uchar</CODE>,
181
<CODE>short</CODE>, <CODE>ushort</CODE>, <CODE>float</CODE>.
182
As prescribed by the default
183
K&R C expression promotions, they have to use <CODE>int</CODE> instead
184
of <CODE>char</CODE>, <CODE>schar</CODE>, <CODE>uchar</CODE>,
185
<CODE>short</CODE>, <CODE>ushort</CODE> and <CODE>double</CODE> instead of
188
<LI> The macros <CODE>va_start_longlong()</CODE>,
189
<CODE>va_start_ulonglong()</CODE>, <CODE>va_return_longlong()</CODE>,
190
<CODE>va_return_ulonglong()</CODE>, <CODE>va_arg_longlong()</CODE> and
191
<CODE>va_arg_ulonglong()</CODE> work only if the C compiler has a working
192
<CODE>long long</CODE> 64-bit integer type.
194
<LI> The struct types used in <CODE>va_start_struct()</CODE> and
195
<CODE>va_struct()</CODE> must only contain (signed or unsigned) int,
196
long, long long or pointer fields. Struct types containing
197
(signed or unsigned) char, short, float, double or other
198
structs are not supported.
206
<A HREF="vacall(3)"><CODE><B>vacall</B></CODE></A>(3), <A HREF="trampoline(3)"><CODE><B>trampoline</B></CODE></A>(3).
212
The current implementations have been tested on a selection
213
of common cases but there are probably still many
216
There are typically built-in limits on the size of the
217
argument-list, which may also include the size of any
220
The decision whether a struct is to be returned in registers or in memory
221
considers only the struct's size and alignment. This is inaccurate: for
222
example, gcc on m68k-next returns
223
<CODE>struct { char a,b,c; }</CODE>
225
<CODE>struct { char a[3]; }</CODE>
226
in memory, although both types have the same size and the same alignment.
228
<CODE><callback.h></CODE> cannot be included when <CODE><varargs.h></CODE> or
229
<CODE><stdarg.h></CODE> is included. (Name clash for <CODE>va_alist</CODE>.)
231
The argument list can only be walked once.
237
All information is passed in CPU registers and the stack.
238
The <CODE><B>callback</B></CODE> package is therefore multithread-safe.
244
Porting <CODE><B>callback</B></CODE> consists in first porting the <CODE><B>vacall</B></CODE> and
245
<CODE><B>trampoline</B></CODE> packages, then choosing a CPU register for
246
passing the closure from <EM>trampoline</EM> to <EM>vacall</EM>. This
247
register is normally the register designated by
248
<CODE>STATIC_CHAIN_REGNUM</CODE> in the gcc source, file
249
<SAMP>gcc-2.7.2/config/<VAR>cpu</VAR>/<VAR>cpu</VAR>.h</SAMP>.
255
Bruno Haible <bruno@clisp.org>
257
<A NAME="Acknowledgements">
258
<H2>Acknowledgements</H2>
261
Many ideas were cribbed from the gcc source.
266
<ADDRESS>CALLBACK manual page<BR>
267
Bruno Haible <bruno@clisp.org>
270
Last modified: 14 January 2001.