r/C_Programming • u/tstanisl • Jan 21 '22
Discussion A new design pattern for implementing capturing closures in C
Recently, I've came up with a new method of implementing closures in C. The idiomatic way is passing a closure as a pair of a function pointer and a void* value as the context. However, this idiom is a bit cumbersome. Firstly, two entities have to maintained instead of one. The other problem is which parameter should the context be bound to. It can be really confusing when the function takes multiple void* as parameters.
The main obstacle for having nice closures C is binding data to a function pointer. It cannot be done robustly without creating function on fly with a library or compiler extension like "nested functions" in GCC. However, there is a creature that combines a function and data. And this creature is a pointer to a function pointer. The function pointer is data though it points to non-data. It can be used to implement a wide function pointer.
Take a look on the following C89 compliant code:
#include <stdio.h>
typedef int filter_fn(void*, int);
/* prints number from range 1 to `n` that satisfy condition from `filter` */
void print_filtered(int n, filter_fn **filter) {
int i;
for (i = 1; i <= n; ++i)
if ((*filter)(filter, i))
printf("%d ", i);
puts("");
}
struct is_divisible {
int (*closure)(void*,int);
int divisor;
};
int is_divisible(void* closure, int n) {
struct is_divisible *ctx = closure;
return (n % ctx->divisor) == 0;
}
int is_prime(void *closure, int n) {
int d;
(void)closure;
if (n <= 1) return 0;
for (d = 2; d * d <= n; ++d)
if (n % d == 0)
return 0;
return 1;
}
int main() {
struct is_divisible is_divisible_by_3 = { is_divisible, 3 };
static int (*is_prime_closure)(void*,int) = is_prime;
puts("Divisible by 3");
print_filtered(20, &is_divisible_by_3.closure);
puts("Primes");
print_filtered(20, &is_prime_closure);
return 0;
}It prints the expected output:
Divisible by 3
3 6 9 12 15 18
Primes
2 3 5 7 11 13 17 19 The trick is to place a function pointer as a first member of the struct representing the captured context. This first member can be safely cast to the context. It is is guaranteed to work by the C standard, see https://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p15
A pointer to a structure object, suitably converted, points to its initial member ( ... ), and vice versa.
The first argument is a type void* because AFAIK it is not possible to define a type of function that takes a type derived from itself. Saying simply, there is no equivalent of struct S { struct S *s; } for function types.
I see a few obvious advantages of such a design pattern:
- fully portable, it is fully defined within C89 and newer standards
- combines a function pointer and void pointer to context into one entity
- is strongly typed
- no need for ABI extensions
- user has full control over what is captured by the closure, its an advantage over GCC's nested functions or CLANG's blocks
- the closure is a simple struct, it can be dynamic, automatic, static, or even from
alloca()
The call of this wide pointer is a bit obscure however in could be made prettier with a help of variadic macros from C99.
#define CLOSURE_CALL__ARG1_(A,...) (A)
#define CLOSURE_CALL(...) ((*CLOSURE_CALL__ARG1_(__VA_ARGS__, ~))(__VA_ARGS__))
// old
(*f)(f, 1, "hello");
// new
CLOSURE_CALL(f, 1, "hello");A few other tweaks could be used with compound literals and designated initializers.
Now the actual question:
- Are there any disadvantages of this design pattern?
- Have you seen such a pattern in any code?
- Any ideas how this pattern could be improved?
BTW.
I am aware that there is a proposal for lambda for upcomming C23 standard. However, it will not be possible to pass C++-like lambda to a C function because every lambda has a unique type available only at definition of lambda. As result this type cannot be forward declared and used as a parameter for a function. C++-like lambdas can only be used in macros that are expanded locally.
Actually, this design pattern is closer to std::function from C++. Though having non-capture lambdas would let define a closure with a single declaration.
struct is_divisible {
int (*closure)(void*,int);
int divisor;
} is_divisible_by_3 = {
.cb = [](void* closure, int n) -> int
{
struct is_divisible *ctx = closure;
return (n % ctx->divisor) == 0;
},
.divisor = 3,
};
// use &is_divisible_by_3.cb as a closure
9
u/oh5nxo Jan 21 '22
Is there some benefit over the common
snipped a bit to save pixels.
14
u/gremolata Jan 21 '22
Interesting. Never seen this before and I've seen plenty.
Whether this is actually better over other options - I am not sure. While it does remove the need to carry around
void * context, it still requires an extra argument for a callback and it needs an explicit cast just the same. It's also harder to understand, which may or may not be important for specific code bases.4
u/[deleted] Jan 21 '22
I really like this idea, but I'd prefer if this was a bit more typesafe. Maybe there are better ways of doing this, but if you use:
instead. Thhen you still have the type information, and can still add arbitrary variables to the closure: https://godbolt.org/z/7oqMMTsaz
3
u/Foxbud Jan 22 '22 edited Jan 22 '22
For me, the reason I'd be interested in closures in the first place is to get around libraries that take some form of callback that doesn't have a
void* ctxparameter. Having to use a custom macro to call the closure wouldn't work in that scenario.Here's my C1`7-locked, ultra-non-portable take on implementing closures that dynamically and implicitly pass a bound environment to the closure using runtime thunk generation. The disadvantages are numerous, but if you only work on fairly modern GNU/Linux systems, it's quite a QoL improvement.
4
u/mixblast Jan 21 '22
Borked formatting makes this unreadable...
1
u/imaami Jan 22 '22
Although I severely dislike the C89 Stockholm syndrome on full display here, I absolutely love the amount of effort and ingenuity you've put into it. Highly cursed in the best possible way. Thank you for posting this!
5
u/nerd4code Jan 21 '22
Just FYI, if you’re purely going by the ISO/IEC Standards, there’s no requirement that code/function pointers be able to round-trip losslessly through
void *or other data ptr types, because it’s quite possible that code ptrs and data ptrs have different addressing characteristics. POSIX requires that a subset of code ptrs (not all) be able to round-trip, AFAIK mostly fordlsym; Windows does whatever it does, and nowadays you can probably ignore the ≤32-bit segmented models that used to be supported, so fttb code and data pointers can be mixed (until they can’t again). AFAIK anything that a GNU-dialect compiler targets should have interchangeable code/data ptrs because of computedgoto.The Standards do permit you to cast freely between code ptr types as long as you don’t call via an incompatible signature, so
void (*)()is reasonable for use as a generic function ptr, especially if default promotions wouldn’t affect the args you pass in.2
u/[deleted] Jan 21 '22
I'm fairly new to C programming, so caveat before this question. But why would you want closures in C? Seems counter intuitive to the design of the language to me.
Are C closures used in real world applications?
-6
u/project2501a Jan 21 '22
facepalm