summaryrefslogtreecommitdiff
path: root/thunk.h
blob: a8138b062d93ab00fb021850d7c3f8ac9f88b020 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 *  Machinery for generating simple C thunks using the C preprocessor.
 *  Copyright (C) 2016 Vito Caputo <vcaputo@pengaru.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#pragma once
#include <assert.h>
#include <stdlib.h>

typedef struct thunk_t thunk_t;

struct thunk_t {
	int (*dispatch)(thunk_t *);
};

/* Clever macro args counting from https://gcc.gnu.org/ml/gcc/2000-09/msg00604.html */
#define _C99_NARGS(...)								\
	_C99_NARGS1( , ##__VA_ARGS__)
#define _C99_NARGS1(...)							\
	_C99_NARGS2(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define _C99_NARGS2(_, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, _n, ...)		\
	_n

/* Macros for declaring the thunk environment struct members, we need a variant 
 * for each supported arity. */
#define _THUNK_GEN_STRUCT_0()

#define _THUNK_GEN_STRUCT_2(_t_a, _n_a)						\
	_t_a _n_a;

#define _THUNK_GEN_STRUCT_4(_t_a, _n_a, _t_b, _n_b)				\
	_t_a _n_a;								\
	_t_b _n_b;

#define _THUNK_GEN_STRUCT_6(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c)			\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;

#define _THUNK_GEN_STRUCT_8(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d)	\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;

#define _THUNK_GEN_STRUCT_10(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e)\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;								\
	_t_e _n_e;

#define _THUNK_GEN_STRUCT_12(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f)\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;								\
	_t_e _n_e;								\
	_t_f _n_f;

#define _THUNK_GEN_STRUCT_14(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g)\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;								\
	_t_e _n_e;								\
	_t_f _n_f;								\
	_t_g _n_g;

#define _THUNK_GEN_STRUCT_16(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h)\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;								\
	_t_e _n_e;								\
	_t_f _n_f;								\
	_t_g _n_g;								\
	_t_h _n_h;

#define _THUNK_GEN_STRUCT_18(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i)\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;								\
	_t_e _n_e;								\
	_t_f _n_f;								\
	_t_g _n_g;								\
	_t_h _n_h;								\
	_t_i _n_i;

#define _THUNK_GEN_STRUCT_20(_t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i, _t_j, _n_j)\
	_t_a _n_a;								\
	_t_b _n_b;								\
	_t_c _n_c;								\
	_t_d _n_d;								\
	_t_e _n_e;								\
	_t_f _n_f;								\
	_t_g _n_g;								\
	_t_h _n_h;								\
	_t_i _n_i;								\
	_t_j _n_j;

#define _THUNK_GEN_STRUCT__(_count, ...)					\
	_THUNK_GEN_STRUCT_##_count(__VA_ARGS__)

#define _THUNK_GEN_STRUCT_(_count, ...)						\
	_THUNK_GEN_STRUCT__(_count, __VA_ARGS__) 

#define _THUNK_GEN_STRUCT(...)							\
	_THUNK_GEN_STRUCT_(_C99_NARGS(__VA_ARGS__), __VA_ARGS__)


/* Macros for populating the thunk environment struct members when instantiating the
 * thunk, we need a variant for each supported arity. */
#define _THUNK_GEN_INSTANTIATE_0(_env)

#define _THUNK_GEN_INSTANTIATE_2(_env, _t_a, _n_a)				\
	_env->_n_a = _n_a;

#define _THUNK_GEN_INSTANTIATE_4(_env, _t_a, _n_a, _t_b, _n_b)			\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;

#define _THUNK_GEN_INSTANTIATE_6(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c)	\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;

#define _THUNK_GEN_INSTANTIATE_8(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;

#define _THUNK_GEN_INSTANTIATE_10(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;							\
	_env->_n_e = _n_e;

#define _THUNK_GEN_INSTANTIATE_12(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;							\
	_env->_n_e = _n_e;							\
	_env->_n_f = _n_f;

#define _THUNK_GEN_INSTANTIATE_14(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;							\
	_env->_n_e = _n_e;							\
	_env->_n_f = _n_f;							\
	_env->_n_g = _n_g;

#define _THUNK_GEN_INSTANTIATE_16(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;							\
	_env->_n_e = _n_e;							\
	_env->_n_f = _n_f;							\
	_env->_n_g = _n_g;							\
	_env->_n_h = _n_h;

#define _THUNK_GEN_INSTANTIATE_18(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;							\
	_env->_n_e = _n_e;							\
	_env->_n_f = _n_f;							\
	_env->_n_g = _n_g;							\
	_env->_n_h = _n_h;							\
	_env->_n_i = _n_i;

#define _THUNK_GEN_INSTANTIATE_20(_env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i, _t_j, _n_j)\
	_env->_n_a = _n_a;							\
	_env->_n_b = _n_b;							\
	_env->_n_c = _n_c;							\
	_env->_n_d = _n_d;							\
	_env->_n_e = _n_e;							\
	_env->_n_f = _n_f;							\
	_env->_n_g = _n_g;							\
	_env->_n_h = _n_h;							\
	_env->_n_i = _n_i;							\
	_env->_n_j = _n_j;

#define _THUNK_GEN_INSTANTIATE__(_env, _count, ...)				\
	_THUNK_GEN_INSTANTIATE_##_count(_env, __VA_ARGS__)

#define _THUNK_GEN_INSTANTIATE_(_env, _count, ...)				\
	_THUNK_GEN_INSTANTIATE__(_env, _count, __VA_ARGS__) 

#define _THUNK_GEN_INSTANTIATE(_env, ...)					\
	_THUNK_GEN_INSTANTIATE_(_env, _C99_NARGS(__VA_ARGS__), __VA_ARGS__)


/* Macros for dispatching the embedded thunk from its associated environment,
 * we need a variant for each supported arity. */
#define _THUNK_GEN_DISPATCH_0(_name, _env)

#define _THUNK_GEN_DISPATCH_2(_name, _env, _t_a, _n_a)				\
	_name(_env->_n_a)

#define _THUNK_GEN_DISPATCH_4(_name, _env, _t_a, _n_a, _t_b, _n_b)		\
	_name(_env->_n_a, _env->_n_b)

#define _THUNK_GEN_DISPATCH_6(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c)	\
	_name(_env->_n_a, _env->_n_b, _env->_n_c)

#define _THUNK_GEN_DISPATCH_8(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d)

#define _THUNK_GEN_DISPATCH_10(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d, _env->_n_e)

#define _THUNK_GEN_DISPATCH_12(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d, _env->_n_e, _env->_n_f)

#define _THUNK_GEN_DISPATCH_14(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d, _env->_n_e, _env->_n_f, _env->_n_g)

#define _THUNK_GEN_DISPATCH_16(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d, _env->_n_e, _env->_n_f, _env->_n_g, _env->_n_h)

#define _THUNK_GEN_DISPATCH_18(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d, _env->_n_e, _env->_n_f, _env->_n_g, _env->_n_h, _env->_n_i)

#define _THUNK_GEN_DISPATCH_20(_name, _env, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i, _t_j, _n_j)\
	_name(_env->_n_a, _env->_n_b, _env->_n_c, _env->_n_d, _env->_n_e, _env->_n_f, _env->_n_g, _env->_n_h, _env->_n_i, _env->_n_j)

#define _THUNK_GEN_DISPATCH__(_name, _env, _count, ...)				\
	_THUNK_GEN_DISPATCH_##_count(_name, _env, __VA_ARGS__)

#define _THUNK_GEN_DISPATCH_(_name, _env, _count, ...)				\
	_THUNK_GEN_DISPATCH__(_name, _env, _count, __VA_ARGS__) 

#define _THUNK_GEN_DISPATCH(_name, _env, ...)					\
	_THUNK_GEN_DISPATCH_(_name, _env, _C99_NARGS(__VA_ARGS__), __VA_ARGS__)


/* Macros for prototyping the function being thunked,
 * we need a variant for each supported arity. */
#define _THUNK_GEN_PROTO_0(_name)

#define _THUNK_GEN_PROTO_2(_name, _t_a, _n_a)					\
	_name(_t_a _n_a)

#define _THUNK_GEN_PROTO_4(_name, _t_a, _n_a, _t_b, _n_b)			\
	_name(_t_a _n_a, _t_b _n_b)

#define _THUNK_GEN_PROTO_6(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c)		\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c)

#define _THUNK_GEN_PROTO_8(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d)

#define _THUNK_GEN_PROTO_10(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d, _t_e _n_e)

#define _THUNK_GEN_PROTO_12(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d, _t_e _n_e, _t_f _n_f)

#define _THUNK_GEN_PROTO_14(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d, _t_e _n_e, _t_f _n_f, _t_g _n_g)

#define _THUNK_GEN_PROTO_16(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d, _t_e _n_e, _t_f _n_f, _t_g _n_g, _t_h _n_h)

#define _THUNK_GEN_PROTO_18(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d, _t_e _n_e, _t_f _n_f, _t_g _n_g, _t_h _n_h, _t_i _n_i)

#define _THUNK_GEN_PROTO_20(_name, _t_a, _n_a, _t_b, _n_b, _t_c, _n_c, _t_d, _n_d, _t_e, _n_e, _t_f, _n_f, _t_g, _n_g, _t_h, _n_h, _t_i, _n_i, _t_j, _n_j)\
	_name(_t_a _n_a, _t_b _n_b, _t_c _n_c, _t_d _n_d, _t_e _n_e, _t_f _n_f, _t_g _n_g, _t_h _n_h, _t_i _n_i, _t_j _n_j)

#define _THUNK_GEN_PROTO__(_name, _count, ...)					\
	_THUNK_GEN_PROTO_##_count(_name, __VA_ARGS__)

#define _THUNK_GEN_PROTO_(_name, _count, ...)					\
	_THUNK_GEN_PROTO__(_name, _count, __VA_ARGS__) 

#define _THUNK_GEN_PROTO(_name, ...)						\
	_THUNK_GEN_PROTO_(_name, _C99_NARGS(__VA_ARGS__), __VA_ARGS__)


/* Define a function for use with a thunk.  This generates the scaffolding
 * functions and structure for (un)packing the args with an appropriately
 * wrapped thunk_t.  Calls to this function may then be supplied to anything
 * accepting a (thunk_t *) by simply wrapping the call with THUNK().
 */
#define THUNK_DEFINE(_name, ...)						\
	int _THUNK_GEN_PROTO(_name, __VA_ARGS__);				\
	typedef struct __thunk_environment_##_name {				\
		/* struct for encapsulating the calling environment */		\
		thunk_t			__thunk;				\
		_THUNK_GEN_STRUCT	(__VA_ARGS__);				\
	} __thunk_environment_##_name;						\
										\
	int __thunk_dispatch_##_name(thunk_t *thunk) {				\
		/* dispatch thunk from associated environment */		\
		__thunk_environment_##_name *env;				\
		int r;								\
										\
		/* XXX: the embedded __thunk is always at the start, otherwise	\
		 * container_of() could be used... */				\
		env = (__thunk_environment_##_name *)thunk;			\
		assert(env);							\
										\
		r = _THUNK_GEN_DISPATCH(_name, env, __VA_ARGS__);		\
		free(env);							\
										\
		return r;							\
	}									\
										\
	thunk_t * _THUNK_GEN_PROTO(__thunk_instantiate_##_name, __VA_ARGS__) {	\
		/* allocate and populate environment, return it */		\
		__thunk_environment_##_name *env;				\
										\
		env = malloc(sizeof(*env));					\
		assert(env);							\
										\
		_THUNK_GEN_INSTANTIATE(env, __VA_ARGS__);			\
		env->__thunk.dispatch = __thunk_dispatch_##_name;		\
										\
		return &env->__thunk;						\
	}									\
										\
	int _THUNK_GEN_PROTO(_name, __VA_ARGS__)

/* Call the appropriate instantiate function which returns an embedded thunk_t,
 * this is how you prepare the thunks established via THUNK_DEFINE(). */
#define THUNK(_call)								\
	__thunk_instantiate_##_call

/* Call the function associated with a prepared thunk, supplying the bound environment
 * as arguments.  This is how code receiving generic thunks executes them. */
static inline int thunk_dispatch(thunk_t *thunk) {
	return thunk->dispatch(thunk);
}
© 2021 All Rights Reserved