File: callback.man

package info (click to toggle)
ffcall 2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,024 kB
  • sloc: asm: 100,607; ansic: 50,932; sh: 5,630; makefile: 1,588; cpp: 2
file content (164 lines) | stat: -rw-r--r-- 8,876 bytes parent folder | download | duplicates (4)
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
CALLBACK(3)                Library Functions Manual                CALLBACK(3)



NAME
       callback - closures with variable arguments as first-class C functions

SYNOPSIS
       #include <callback.h>

       void function (void* data, va_alist alist)
       {
         va_start_type(alist[, return_type]);
         arg = va_arg_type(alist[, arg_type]);
         va_return_type(alist[[, return_type], return_value]);
       }

       callback = alloc_callback(&function, data);

       free_callback(callback);

       is_callback(callback)
       callback_address(callback)
       callback_data(callback)

DESCRIPTION
       These  functions  implement  closures with variable arguments as first-
       class C functions.

       Closures as 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.

       callback  =  alloc_callback(&function, data) allocates a callback. When
       callback gets called, it arranges to call  function,  passing  data  as
       first  argument  and,  as second argument, the entire sequence of argu‐
       ments passed to 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 vacall package, see below.

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

       is_callback(callback)  checks  whether the C function callback was pro‐
       duced by a call to alloc_callback.  If this returns true, the arguments
       given to alloc_callback can be retrieved:

           callback_address(callback) returns &function,

           callback_data(callback) returns data.


VACALL MACROS
       Within  function,  the following macros can be used to walk through the
       argument list and specify a return value:

       va_start_type(alist[, return_type]);
              starts the walk through the  argument  list  and  specifies  the
              return type.

       arg = va_arg_type(alist[, arg_type]);
              fetches the next argument from the argument list.

       va_return_type(alist[[, return_type], return_value]);
              ends the walk through the argument list and specifies the return
              value.

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

       The  type  specifiers  in  va_start_type and va_return_type must be the
       same.   The  return_type  specifiers  passed   to   va_start_type   and
       va_return_type must be the same.

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

       In va_start_struct(alist, return_type, splittable); the splittable flag
       specifies  whether  the struct 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), splittable is ignored and assumed to be 1. For
       structs  of size > 2*sizeof(long), splittable is ignored and assumed to
       be 0. There are some handy macros for this:
       va_word_splittable_1 (type1)
       va_word_splittable_2 (type1, type2)
       va_word_splittable_3 (type1, type2, type3)
       va_word_splittable_4 (type1, type2, type3, type4)
       For a struct with three slots
       struct { type1 id1; type2 id2; type3 id3; }
       you can  specify  splittable  as  va_word_splittable_3  (type1,  type2,
       type3) .


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

       The macros va_start_longlong(),  va_start_ulonglong(),  va_return_long‐
       long(), va_return_ulonglong(), va_arg_longlong() and va_arg_ulonglong()
       work only if the C compiler has a  working  long  long  64-bit  integer
       type.

       The  struct  types  used in va_start_struct() and 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.


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


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 mem‐
       ory considers only the struct's size and alignment. This is inaccurate:
       for example, gcc on m68k-next returns struct { char a,b,c; } in  regis‐
       ters  and struct { char a[3]; } in memory, although both types have the
       same size and the same alignment.

       The argument list can only be walked once.


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


PORTING
       Porting  callback  consists  in first porting the vacall and trampoline
       packages, then choosing a CPU register for  passing  the  closure  from
       trampoline  to  vacall.   This register is normally the register desig‐
       nated by STATIC_CHAIN_REGNUM in the  gcc  source,  file  gcc-2.7.2/con‐
       fig/cpu/cpu.h.


AUTHOR
       Bruno Haible <bruno@clisp.org>


ACKNOWLEDGEMENTS
       Many ideas were cribbed from the gcc source.




                                1 January 2017                     CALLBACK(3)