From ed36e6abea71afcbe691aeeb529bcc668e88bc7d Mon Sep 17 00:00:00 2001 From: Ross Lagerwall Date: Mon, 12 Mar 2012 20:42:39 +0200 Subject: [PATCH] Add event_self_cbarg() to be used in conjunction with event_new(). event_self_cbarg() returns a magic value which makes event_new() pass the event itself as the callback argument. --- event.c | 10 ++++++++++ include/event2/event.h | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/event.c b/event.c index 8935ad69..b98ba953 100644 --- a/event.c +++ b/event.c @@ -125,6 +125,8 @@ struct event_base *event_global_current_base_ = NULL; static int use_monotonic; +static void *event_self_cbarg_ptr_ = NULL; + /* Prototypes */ static inline int event_add_internal(struct event *ev, const struct timeval *tv, int tv_is_absolute); @@ -1910,6 +1912,12 @@ event_set(struct event *ev, evutil_socket_t fd, short events, EVUTIL_ASSERT(r == 0); } +void * +event_self_cbarg(void) +{ + return &event_self_cbarg_ptr_; +} + struct event * event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) { @@ -1917,6 +1925,8 @@ event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)( ev = mm_malloc(sizeof(struct event)); if (ev == NULL) return (NULL); + if (arg == &event_self_cbarg_ptr_) + arg = ev; if (event_assign(ev, base, fd, events, cb, arg) < 0) { mm_free(ev); return (NULL); diff --git a/include/event2/event.h b/include/event2/event.h index 54871eeb..9b584836 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -845,6 +845,25 @@ int event_base_got_break(struct event_base *); */ typedef void (*event_callback_fn)(evutil_socket_t, short, void *); +/** + Return a value used to specify that the event itself must be used as the callback argument. + + The function event_new() takes a callback argument which is passed + to the event's callback function. To specify that the argument to be + passed to the callback function is the event that event_new() returns, + pass in the return value of event_self_cbarg() as the callback argument + for event_new(). + + For example: +
+      struct event *ev = event_new(base, sock, events, callback, %event_self_cbarg());
+  
+ + @return a value to be passed as the callback argument to event_new(). + @see event_new() + */ +void *event_self_cbarg(void); + /** Allocate and asssign a new event structure, ready to be added.