From 068395038423cc42e05f7cd745f00e58b16a5f35 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 19 Jan 2010 14:01:36 -0500 Subject: [PATCH] Functions to access more fields of struct event. Once event_assign() or event_new() had been called, there was no way to get at a copy of the event's callback, callback argument, or configured events. This patch adds an accessor function for each, and an all-fields accessor for code that wants to re-assign one field of an event. This patch also adds a function to return sizeof(struct event), so that code with intense RAM needs can still retain ABI compatibility between versions of Libevent without having to heap-allocate every struct event individually. The code here was first proposed by Pavel Pisa. --- event.c | 43 ++++++++++++++++++++++++++++++++++++++++-- include/event2/event.h | 39 +++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/event.c b/event.c index 94ae3231..722e9366 100644 --- a/event.c +++ b/event.c @@ -1427,18 +1427,57 @@ _event_initialized(struct event *ev, int need_fd) return 1; } +void +event_get_assignment(const struct event *event, struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, event_callback_fn *callback_out, void **arg_out) +{ + if (base_out) + *base_out = event->ev_base; + if (fd_out) + *fd_out = event->ev_fd; + if (events_out) + *events_out = event->ev_events; + if (callback_out) + *callback_out = event->ev_callback; + if (arg_out) + *arg_out = event->ev_arg; +} + +size_t +event_get_struct_event_size(void) +{ + return sizeof(struct event); +} + evutil_socket_t -event_get_fd(struct event *ev) +event_get_fd(const struct event *ev) { return ev->ev_fd; } struct event_base * -event_get_base(struct event *ev) +event_get_base(const struct event *ev) { return ev->ev_base; } +short +event_get_events(const struct event *ev) +{ + return ev->ev_events; +} + +event_callback_fn +event_get_callback(const struct event *ev) +{ + return ev->ev_callback; +} + +void * +event_get_callback_arg(const struct event *ev) +{ + return ev->ev_arg; +} + int event_add(struct event *ev, const struct timeval *tv) { diff --git a/include/event2/event.h b/include/event2/event.h index 489026a6..6ee2543a 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -397,6 +397,8 @@ int event_base_got_break(struct event_base *); #define evsignal_pending(ev, tv) event_pending((ev), EV_SIGNAL, (tv)) #define evsignal_initialized(ev) _event_initialized((ev), 0) +typedef void (*event_callback_fn)(evutil_socket_t, short, void *); + /** Prepare an event structure to be added. @@ -432,7 +434,7 @@ int event_base_got_break(struct event_base *); @see event_add(), event_del(), event_once() */ -int event_assign(struct event *, struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); +int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *); /** Create and allocate a new event structure, ready to be added. @@ -441,7 +443,7 @@ int event_assign(struct event *, struct event_base *, evutil_socket_t, short, vo that must later be deallocated with event_free(). */ -struct event *event_new(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); +struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *); /** Deallocate a struct event * returned by event_new(). @@ -466,7 +468,7 @@ void event_free(struct event *); @return 0 if successful, or -1 if an error occurred @see event_once() */ -int event_base_once(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *, const struct timeval *); +int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *); /** Add an event to the set of monitored events. @@ -555,12 +557,39 @@ int _event_initialized(struct event *, int check_fd); /** Get the socket assigned to an event. */ -evutil_socket_t event_get_fd(struct event *ev); +evutil_socket_t event_get_fd(const struct event *ev); /** Get the event_base assigned to an event. */ -struct event_base *event_get_base(struct event *ev); +struct event_base *event_get_base(const struct event *ev); + +/** + Return the events (EV_READ, EV_WRITE, etc) assigned to an event. +*/ +short event_get_events(const struct event *ev); + +/** + Return the callback assigned to an event. +*/ +event_callback_fn event_get_callback(const struct event *ev); + +/** + Return the callback argument assigned to an event. +*/ +void *event_get_callback_arg(const struct event *ev); + +void event_get_assignment(const struct event *event, struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, event_callback_fn *callback_out, void **arg_out); + +/** + Return the size of struct event that the Libevent library was compiled + with. + + This will be the same as sizeof(struct event) if you're running with the + same version of Libevent that your application was built with, but + otherwise might not. + */ +size_t event_get_struct_event_size(void); #ifndef EVENT_FD /* We haven't included event_struct.h, so define these as function calls