File: callback.3

package info (click to toggle)
ffcall 1.8-4.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,752 kB
  • ctags: 3,102
  • sloc: asm: 19,563; ansic: 11,737; sh: 5,402; makefile: 885
file content (264 lines) | stat: -rw-r--r-- 6,571 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
.TH CALLBACK 3 "14 January 2001"
.SH NAME
callback \- closures with variable arguments as first-class C functions
.SH SYNOPSIS
.B #include <callback.h>
.LP
.nf
.BI "void " function " (" data ", " alist ")"
.BI "  void* " data ";"
.BI "  va_alist " alist ";"
.BI "{"
.BI "  va_start_" type "(" alist "[, " return_type "]);"
.BI "  " arg " = va_arg_" type "(" alist "[, " arg_type "]);"
.BI "  va_return_" type "(" alist "[[, " return_type "], " return_value "]);"
.BI "}"
.fi
.LP
.IB callback " = alloc_callback(" "&function" ", " data ");"
.LP
.BI "free_callback(" callback ");"
.LP
.nf
.BI "is_callback(" callback ")"
.BI "callback_address(" callback ")"
.BI "callback_data(" callback ")"
.fi
.SH DESCRIPTION
.LP
These functions implement
.I closures
with variable arguments as first-class C functions.

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. Moreover, they can be called with variable
arguments and can return variable return values.

.IB callback " = alloc_callback(" "&function" ", " data ")"
allocates a callback. When
.I callback
gets called, it arranges to call
.IR function ","
passing
.I data
as first argument and, as second argument, the entire sequence of arguments
passed to
.IR callback .

Function calling conventions differ considerably on different machines,
therefore the arguments are accessed and the result value is stored
through the same macros as used by the
.I vacall
package, see below.

The callbacks are functions with indefinite extent:
.I callback
is only deallocated when
.BI free_callback( callback )
is called.

.BI "is_callback(" callback ")"
checks whether the C function
.I callback
was produced by a call to
.IR alloc_callback .
If this returns true, the arguments given to
.I alloc_callback
can be retrieved:
.RS 4
.LP
.BI "callback_address(" callback ")"
returns
.IR "&function" ,
.LP
.BI "callback_data(" callback ")"
returns
.IR data .
.RE

.SH VACALL MACROS

Within
.IR function ,
the following macros can be used to walk through the argument list and
specify a return value:
.RS 0
.TP
.BI "va_start_" type "(" alist "[, " return_type "]);"
starts the walk through the argument list and specifies the return type.
.TP
.IB arg " = va_arg_" type "(" alist "[, " arg_type "]);"
fetches the next argument from the argument list.
.TP
.BI "va_return_" type "(" alist "[[, " return_type "], " return_value "]);"
ends the walk through the argument list and specifies the return value.
.RE

The
.I type
in
.BI va_start_ type
and
.BI va_return_ type
shall be one of
.BR void ", " int ", " uint ", " long ", " ulong ", " longlong ", " ulonglong ", " double ", " struct ", " ptr
or (for ANSI C calling conventions only)
.BR char ", " schar ", " uchar ", " short ", " ushort ", " float ,
depending on the class of
.IR return_type .

The
.I type
specifiers in
.BI va_start_ type
and
.BI va_return_ type
must be the same.
The
.I return_type
specifiers passed to
.BI va_start_ type
and
.BI va_return_ type
must be the same.

The
.I type
in
.BI va_arg_ type
shall be one of
.BR int ", " uint ", " long ", " ulong ", " longlong ", " ulonglong ", " double ", " struct ", " ptr
or (for ANSI C calling conventions only)
.BR char ", " schar ", " uchar ", " short ", " ushort ", " float ,
depending on the class of
.IR arg_type .

In
.BI "va_start_struct(" alist ", " return_type ", " splittable );
the
.I splittable
flag specifies whether the struct
.I return_type
can be returned in registers such that every struct field fits entirely in
a single register. This needs to be specified for structs of size
2*sizeof(long). For structs of size <= sizeof(long),
.I splittable
is ignored and assumed to be 1. For structs of size > 2*sizeof(long),
.I splittable
is ignored and assumed to be 0. There are some handy macros for this:
.nf
.BI "va_word_splittable_1 (" type1 )
.BI "va_word_splittable_2 (" type1 ", " type2 )
.BI "va_word_splittable_3 (" type1 ", " type2 ", " type3 )
.BI "va_word_splittable_4 (" type1 ", " type2 ", " type3 ", " type4 )
.fi
For a struct with three slots
.nf
.BI "struct { " "type1 id1" "; " "type2 id2" "; " "type3 id3" "; }"
.fi
you can specify
.I splittable
as
.BI "va_word_splittable_3 (" type1 ", " type2 ", " type3 )
.RB .

.SH NOTES

Functions which want to emulate Kernighan & Ritchie style functions (i.e.,
in ANSI C, functions without a typed argument list) cannot use the
.I type
values
.BR char ", " schar ", " uchar ", " short ", " ushort ", " float .
As prescribed by the default K&R C expression promotions, they have
to use
.B int
instead of
.BR char ", " schar ", " uchar ", " short ", " ushort
and
.B double
instead of
.BR float .

The macros
.BR va_start_longlong(\|) ,
.BR va_start_ulonglong(\|) ,
.BR va_return_longlong(\|) ,
.BR va_return_ulonglong(\|) ,
.B va_arg_longlong(\|)
and
.B va_arg_ulonglong(\|)
work only if the C compiler has a working
.B long long
64-bit integer type.

The struct types used in
.B va_start_struct(\|)
and
.B va_struct(\|)
must only contain (signed or unsigned) int, long, long long or pointer fields.
Struct types containing (signed or unsigned) char, short, float, double or
other structs are not supported.

.SH SEE ALSO
.BR vacall (3),
.BR trampoline (3).

.SH BUGS

The current implementations have been tested on a selection of common
cases but there are probably still many bugs.

There are typically built-in limits on the size of the argument-list,
which may also include the size of any structure arguments.

The decision whether a struct is to be returned in registers or in memory
considers only the struct's size and alignment. This is inaccurate: for
example, gcc on m68k-next returns
.B "struct { char a,b,c; }"
in registers and
.B "struct { char a[3]; }"
in memory, although both types have the same size and the same alignment.

.B <callback.h>
cannot be included when
.B <varargs.h>
or
.B <stdarg.h>
is included.
(Name clash for
.BR va_alist ".)"

The argument list can only be walked once.

.SH NON-BUGS

All information is passed in CPU registers and the stack. The
.B callback
package is therefore multithread-safe.

.SH PORTING

Porting
.B callback
consists in first porting the
.B vacall
and
.B trampoline
packages, then choosing a CPU register for passing the closure from
.B trampoline
to
.BR vacall .
This register is normally the register designated by STATIC_CHAIN_REGNUM
in the gcc source, file
.RI gcc-2.7.2/config/ cpu / cpu .h.

.SH AUTHOR

Bruno Haible <haible@clisp.cons.org>

.SH ACKNOWLEDGEMENTS

Many ideas were cribbed from the gcc source.