~ubuntu-branches/debian/squeeze/ffcall/squeeze

« back to all changes in this revision

Viewing changes to callback/callback.3

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2010-06-26 15:29:30 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100626152930-c09y01gk3szcnykn
Tags: 1.10+cvs20100619-2
Ship to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.TH CALLBACK 3 "14 January 2001"
 
2
.SH NAME
 
3
callback \- closures with variable arguments as first-class C functions
 
4
.SH SYNOPSIS
 
5
.B #include <callback.h>
 
6
.LP
 
7
.nf
 
8
.BI "void " function " (" data ", " alist ")"
 
9
.BI "  void* " data ";"
 
10
.BI "  va_alist " alist ";"
 
11
.BI "{"
 
12
.BI "  va_start_" type "(" alist "[, " return_type "]);"
 
13
.BI "  " arg " = va_arg_" type "(" alist "[, " arg_type "]);"
 
14
.BI "  va_return_" type "(" alist "[[, " return_type "], " return_value "]);"
 
15
.BI "}"
 
16
.fi
 
17
.LP
 
18
.IB callback " = alloc_callback(" "&function" ", " data ");"
 
19
.LP
 
20
.BI "free_callback(" callback ");"
 
21
.LP
 
22
.nf
 
23
.BI "is_callback(" callback ")"
 
24
.BI "callback_address(" callback ")"
 
25
.BI "callback_data(" callback ")"
 
26
.fi
 
27
.SH DESCRIPTION
 
28
.LP
 
29
These functions implement
 
30
.I closures
 
31
with variable arguments as first-class C functions.
 
32
 
 
33
Closures as
 
34
.I first-class C functions
 
35
means that they fit into a function pointer and can be called exactly
 
36
like any other C function. Moreover, they can be called with variable
 
37
arguments and can return variable return values.
 
38
 
 
39
.IB callback " = alloc_callback(" "&function" ", " data ")"
 
40
allocates a callback. When
 
41
.I callback
 
42
gets called, it arranges to call
 
43
.IR function ","
 
44
passing
 
45
.I data
 
46
as first argument and, as second argument, the entire sequence of arguments
 
47
passed to
 
48
.IR callback .
 
49
 
 
50
Function calling conventions differ considerably on different machines,
 
51
therefore the arguments are accessed and the result value is stored
 
52
through the same macros as used by the
 
53
.I vacall
 
54
package, see below.
 
55
 
 
56
The callbacks are functions with indefinite extent:
 
57
.I callback
 
58
is only deallocated when
 
59
.BI free_callback( callback )
 
60
is called.
 
61
 
 
62
.BI "is_callback(" callback ")"
 
63
checks whether the C function
 
64
.I callback
 
65
was produced by a call to
 
66
.IR alloc_callback .
 
67
If this returns true, the arguments given to
 
68
.I alloc_callback
 
69
can be retrieved:
 
70
.RS 4
 
71
.LP
 
72
.BI "callback_address(" callback ")"
 
73
returns
 
74
.IR "&function" ,
 
75
.LP
 
76
.BI "callback_data(" callback ")"
 
77
returns
 
78
.IR data .
 
79
.RE
 
80
 
 
81
.SH VACALL MACROS
 
82
 
 
83
Within
 
84
.IR function ,
 
85
the following macros can be used to walk through the argument list and
 
86
specify a return value:
 
87
.RS 0
 
88
.TP
 
89
.BI "va_start_" type "(" alist "[, " return_type "]);"
 
90
starts the walk through the argument list and specifies the return type.
 
91
.TP
 
92
.IB arg " = va_arg_" type "(" alist "[, " arg_type "]);"
 
93
fetches the next argument from the argument list.
 
94
.TP
 
95
.BI "va_return_" type "(" alist "[[, " return_type "], " return_value "]);"
 
96
ends the walk through the argument list and specifies the return value.
 
97
.RE
 
98
 
 
99
The
 
100
.I type
 
101
in
 
102
.BI va_start_ type
 
103
and
 
104
.BI va_return_ type
 
105
shall be one of
 
106
.BR void ", " int ", " uint ", " long ", " ulong ", " longlong ", " ulonglong ", " double ", " struct ", " ptr
 
107
or (for ANSI C calling conventions only)
 
108
.BR char ", " schar ", " uchar ", " short ", " ushort ", " float ,
 
109
depending on the class of
 
110
.IR return_type .
 
111
 
 
112
The
 
113
.I type
 
114
specifiers in
 
115
.BI va_start_ type
 
116
and
 
117
.BI va_return_ type
 
118
must be the same.
 
119
The
 
120
.I return_type
 
121
specifiers passed to
 
122
.BI va_start_ type
 
123
and
 
124
.BI va_return_ type
 
125
must be the same.
 
126
 
 
127
The
 
128
.I type
 
129
in
 
130
.BI va_arg_ type
 
131
shall be one of
 
132
.BR int ", " uint ", " long ", " ulong ", " longlong ", " ulonglong ", " double ", " struct ", " ptr
 
133
or (for ANSI C calling conventions only)
 
134
.BR char ", " schar ", " uchar ", " short ", " ushort ", " float ,
 
135
depending on the class of
 
136
.IR arg_type .
 
137
 
 
138
In
 
139
.BI "va_start_struct(" alist ", " return_type ", " splittable );
 
140
the
 
141
.I splittable
 
142
flag specifies whether the struct
 
143
.I return_type
 
144
can be returned in registers such that every struct field fits entirely in
 
145
a single register. This needs to be specified for structs of size
 
146
2*sizeof(long). For structs of size <= sizeof(long),
 
147
.I splittable
 
148
is ignored and assumed to be 1. For structs of size > 2*sizeof(long),
 
149
.I splittable
 
150
is ignored and assumed to be 0. There are some handy macros for this:
 
151
.nf
 
152
.BI "va_word_splittable_1 (" type1 )
 
153
.BI "va_word_splittable_2 (" type1 ", " type2 )
 
154
.BI "va_word_splittable_3 (" type1 ", " type2 ", " type3 )
 
155
.BI "va_word_splittable_4 (" type1 ", " type2 ", " type3 ", " type4 )
 
156
.fi
 
157
For a struct with three slots
 
158
.nf
 
159
.BI "struct { " "type1 id1" "; " "type2 id2" "; " "type3 id3" "; }"
 
160
.fi
 
161
you can specify
 
162
.I splittable
 
163
as
 
164
.BI "va_word_splittable_3 (" type1 ", " type2 ", " type3 )
 
165
.RB .
 
166
 
 
167
.SH NOTES
 
168
 
 
169
Functions which want to emulate Kernighan & Ritchie style functions (i.e.,
 
170
in ANSI C, functions without a typed argument list) cannot use the
 
171
.I type
 
172
values
 
173
.BR char ", " schar ", " uchar ", " short ", " ushort ", " float .
 
174
As prescribed by the default K&R C expression promotions, they have
 
175
to use
 
176
.B int
 
177
instead of
 
178
.BR char ", " schar ", " uchar ", " short ", " ushort
 
179
and
 
180
.B double
 
181
instead of
 
182
.BR float .
 
183
 
 
184
The macros
 
185
.BR va_start_longlong(\|) ,
 
186
.BR va_start_ulonglong(\|) ,
 
187
.BR va_return_longlong(\|) ,
 
188
.BR va_return_ulonglong(\|) ,
 
189
.B va_arg_longlong(\|)
 
190
and
 
191
.B va_arg_ulonglong(\|)
 
192
work only if the C compiler has a working
 
193
.B long long
 
194
64-bit integer type.
 
195
 
 
196
The struct types used in
 
197
.B va_start_struct(\|)
 
198
and
 
199
.B va_struct(\|)
 
200
must only contain (signed or unsigned) int, long, long long or pointer fields.
 
201
Struct types containing (signed or unsigned) char, short, float, double or
 
202
other structs are not supported.
 
203
 
 
204
.SH SEE ALSO
 
205
.BR vacall (3),
 
206
.BR trampoline (3).
 
207
 
 
208
.SH BUGS
 
209
 
 
210
The current implementations have been tested on a selection of common
 
211
cases but there are probably still many bugs.
 
212
 
 
213
There are typically built-in limits on the size of the argument-list,
 
214
which may also include the size of any structure arguments.
 
215
 
 
216
The decision whether a struct is to be returned in registers or in memory
 
217
considers only the struct's size and alignment. This is inaccurate: for
 
218
example, gcc on m68k-next returns
 
219
.B "struct { char a,b,c; }"
 
220
in registers and
 
221
.B "struct { char a[3]; }"
 
222
in memory, although both types have the same size and the same alignment.
 
223
 
 
224
.B <callback.h>
 
225
cannot be included when
 
226
.B <varargs.h>
 
227
or
 
228
.B <stdarg.h>
 
229
is included.
 
230
(Name clash for
 
231
.BR va_alist ".)"
 
232
 
 
233
The argument list can only be walked once.
 
234
 
 
235
.SH NON-BUGS
 
236
 
 
237
All information is passed in CPU registers and the stack. The
 
238
.B callback
 
239
package is therefore multithread-safe.
 
240
 
 
241
.SH PORTING
 
242
 
 
243
Porting
 
244
.B callback
 
245
consists in first porting the
 
246
.B vacall
 
247
and
 
248
.B trampoline
 
249
packages, then choosing a CPU register for passing the closure from
 
250
.B trampoline
 
251
to
 
252
.BR vacall .
 
253
This register is normally the register designated by STATIC_CHAIN_REGNUM
 
254
in the gcc source, file
 
255
.RI gcc-2.7.2/config/ cpu / cpu .h.
 
256
 
 
257
.SH AUTHOR
 
258
 
 
259
Bruno Haible <bruno@clisp.org>
 
260
 
 
261
.SH ACKNOWLEDGEMENTS
 
262
 
 
263
Many ideas were cribbed from the gcc source.
 
264