delegate.c: Abstract Function Pointers in C

description

C libraries and application components often make use of function callbacks to interface with other code. Sometimes the required signature of the function callback is annoying and/or introduces obscurity and indirection which, hitherto, one had to silently suffer.

delegate.c implements a sort of abstract function pointer by capturing a function signature (delegate()) and exposing it through a simple interface (invoke()). It provides something loosely approximating lambda expressions; or even more analogous, C# delegates.

The implementation depends on GCC's type introspection builtins, and on libffi. Both are widely portable.

All the magic in the delegate() macro should be constant folded by GCC, even at optimization level 0. The generated code should not be any slower than using libffi directly (however one might do so).

The original purpose of this code was to abstract the libevent API. The core implementation is entirely generic. This generic implementation is then used to provide an event_set() / event_add() wrapper: event_delegate().

Passing a typical void (*)(int, short, void *) callback is statically detected by event_delegate(). In such a case, only the third argument is necessary; the first two are implicit. libffi is entirely bypassed in this case, introducing no run-time penalty. This is useful for micro-optimizing those few callbacks which get all the traffic.

news

2009-11-21

Fix bug that would continually re-add the event even if already pending.

2009-04-25

Toss the code up as a separate project.

usage

/* Simple example of delegate() and invoke(). */
struct delegate del = DELEGATE_INITIALIZER;
delegate(&del, &my_func, arg0, arg1, ...);
invoke(&del);

/* Execute exit(EXIT_SUCCESS) on SIGTERM or after 60 seconds. */
struct event_delegate event = EVENT_DELEGATE_INITIALIZER;
int sig = SIGTERM;
short events = EV_SIGNAL|EV_PERSIST;
struct timeval timeout = { 60, 0 };
event_delegate(&event, sig, events, &timeout, &exit, EXIT_SUCCESS);
event_dispatch();

A small regression utility can be built from the source. Defining DELEGATE_MAIN will expose the main() definition.

license

Copyright (c) 2009 William Ahern

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

download

delegate-20091121.tgz

source

git clone http://25thandClement.com/~william/projects/delegate.git

other projects

airctl | bsdauth | cnippets | libarena | libevnet | authldap | streamlocal | libnostd | zoned | dns.c | delegate.c | llrb.h | lpegk | json.c | cqueues | siphash.h | hexdump.c | timeout.c | luapath | luaossl | lunix | phf | runlua | tarsum | prosody-openbsd | AnonNet