From 56faf02bae144742b4aa6b173a2ac034d953a7c3 Mon Sep 17 00:00:00 2001 From: Dominic Chen Date: Thu, 13 Apr 2017 14:58:13 -0400 Subject: [PATCH] bufferevent: refactor to use type check macros (cherry picked from commit 92cc0b9c3db38088f79c5d1e432c429fbc366968) --- bufferevent-internal.h | 7 +++++++ bufferevent_async.c | 2 +- bufferevent_filter.c | 4 ++-- bufferevent_openssl.c | 6 +++--- bufferevent_pair.c | 4 ++-- bufferevent_sock.c | 8 ++++---- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/bufferevent-internal.h b/bufferevent-internal.h index b448eead..f79e74d3 100644 --- a/bufferevent-internal.h +++ b/bufferevent-internal.h @@ -306,6 +306,13 @@ extern const struct bufferevent_ops bufferevent_ops_pair; #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter) #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair) +#if defined(EVENT__HAVE_OPENSSL) +extern const struct bufferevent_ops bufferevent_ops_openssl; +#define BEV_IS_OPENSSL(bevp) ((bevp)->be_ops == &bufferevent_ops_openssl) +#else +#define BEV_IS_OPENSSL(bevp) 0 +#endif + #ifdef _WIN32 extern const struct bufferevent_ops bufferevent_ops_async; #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async) diff --git a/bufferevent_async.c b/bufferevent_async.c index 6395e57a..1521d896 100644 --- a/bufferevent_async.c +++ b/bufferevent_async.c @@ -104,7 +104,7 @@ static inline struct bufferevent_async * upcast(struct bufferevent *bev) { struct bufferevent_async *bev_a; - if (bev->be_ops != &bufferevent_ops_async) + if (!BEV_IS_ASYNC(bev)) return NULL; bev_a = EVUTIL_UPCAST(bev, struct bufferevent_async, bev.bev); return bev_a; diff --git a/bufferevent_filter.c b/bufferevent_filter.c index b5efd5c0..d9e8540f 100644 --- a/bufferevent_filter.c +++ b/bufferevent_filter.c @@ -118,11 +118,11 @@ static inline struct bufferevent_filtered * upcast(struct bufferevent *bev) { struct bufferevent_filtered *bev_f; - if (bev->be_ops != &bufferevent_ops_filter) + if (!BEV_IS_FILTER(bev)) return NULL; bev_f = (void*)( ((char*)bev) - evutil_offsetof(struct bufferevent_filtered, bev.bev)); - EVUTIL_ASSERT(bev_f->bev.bev.be_ops == &bufferevent_ops_filter); + EVUTIL_ASSERT(BEV_IS_FILTER(&bev_f->bev.bev)); return bev_f; } diff --git a/bufferevent_openssl.c b/bufferevent_openssl.c index da3963af..252627ec 100644 --- a/bufferevent_openssl.c +++ b/bufferevent_openssl.c @@ -354,11 +354,11 @@ static inline struct bufferevent_openssl * upcast(struct bufferevent *bev) { struct bufferevent_openssl *bev_o; - if (bev->be_ops != &bufferevent_ops_openssl) + if (!BEV_IS_OPENSSL(bev)) return NULL; bev_o = (void*)( ((char*)bev) - evutil_offsetof(struct bufferevent_openssl, bev.bev)); - EVUTIL_ASSERT(bev_o->bev.bev.be_ops == &bufferevent_ops_openssl); + EVUTIL_ASSERT(BEV_IS_OPENSSL(&bev_o->bev.bev)); return bev_o; } @@ -803,7 +803,7 @@ consider_reading(struct bufferevent_openssl *bev_ssl) if (bev_ssl->bev.read_suspended) break; - + /* Read all pending data. This won't hit the network * again, and will (most importantly) put us in a state * where we don't need to read anything else until the diff --git a/bufferevent_pair.c b/bufferevent_pair.c index 1e93f572..f88cd751 100644 --- a/bufferevent_pair.c +++ b/bufferevent_pair.c @@ -56,10 +56,10 @@ static inline struct bufferevent_pair * upcast(struct bufferevent *bev) { struct bufferevent_pair *bev_p; - if (bev->be_ops != &bufferevent_ops_pair) + if (!BEV_IS_PAIR(bev)) return NULL; bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev.bev); - EVUTIL_ASSERT(bev_p->bev.bev.be_ops == &bufferevent_ops_pair); + EVUTIL_ASSERT(BEV_IS_PAIR(&bev_p->bev.bev)); return bev_p; } diff --git a/bufferevent_sock.c b/bufferevent_sock.c index 93aedb33..e305ec55 100644 --- a/bufferevent_sock.c +++ b/bufferevent_sock.c @@ -607,7 +607,7 @@ be_socket_destruct(struct bufferevent *bufev) struct bufferevent_private *bufev_p = EVUTIL_UPCAST(bufev, struct bufferevent_private, bev); evutil_socket_t fd; - EVUTIL_ASSERT(bufev->be_ops == &bufferevent_ops_socket); + EVUTIL_ASSERT(BEV_IS_SOCKET(bufev)); fd = event_get_fd(&bufev->ev_read); @@ -632,7 +632,7 @@ be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd) EVUTIL_UPCAST(bufev, struct bufferevent_private, bev); BEV_LOCK(bufev); - EVUTIL_ASSERT(bufev->be_ops == &bufferevent_ops_socket); + EVUTIL_ASSERT(BEV_IS_SOCKET(bufev)); event_del(&bufev->ev_read); event_del(&bufev->ev_write); @@ -662,7 +662,7 @@ bufferevent_priority_set(struct bufferevent *bufev, int priority) EVUTIL_UPCAST(bufev, struct bufferevent_private, bev); BEV_LOCK(bufev); - if (bufev->be_ops != &bufferevent_ops_socket) + if (!BEV_IS_SOCKET(bufev)) goto done; if (event_priority_set(&bufev->ev_read, priority) == -1) @@ -685,7 +685,7 @@ bufferevent_base_set(struct event_base *base, struct bufferevent *bufev) int res = -1; BEV_LOCK(bufev); - if (bufev->be_ops != &bufferevent_ops_socket) + if (!BEV_IS_SOCKET(bufev)) goto done; bufev->ev_base = base; -- 2.40.0