bufferevent: refactor to use type check macros

This commit is contained in:
Dominic Chen 2017-04-13 14:58:13 -04:00
parent 266f43af77
commit 92cc0b9c3d
6 changed files with 19 additions and 12 deletions

View File

@ -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_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair) #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 #ifdef _WIN32
extern const struct bufferevent_ops bufferevent_ops_async; extern const struct bufferevent_ops bufferevent_ops_async;
#define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async) #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)

View File

@ -104,7 +104,7 @@ static inline struct bufferevent_async *
upcast(struct bufferevent *bev) upcast(struct bufferevent *bev)
{ {
struct bufferevent_async *bev_a; struct bufferevent_async *bev_a;
if (bev->be_ops != &bufferevent_ops_async) if (!BEV_IS_ASYNC(bev))
return NULL; return NULL;
bev_a = EVUTIL_UPCAST(bev, struct bufferevent_async, bev.bev); bev_a = EVUTIL_UPCAST(bev, struct bufferevent_async, bev.bev);
return bev_a; return bev_a;

View File

@ -118,11 +118,11 @@ static inline struct bufferevent_filtered *
upcast(struct bufferevent *bev) upcast(struct bufferevent *bev)
{ {
struct bufferevent_filtered *bev_f; struct bufferevent_filtered *bev_f;
if (bev->be_ops != &bufferevent_ops_filter) if (!BEV_IS_FILTER(bev))
return NULL; return NULL;
bev_f = (void*)( ((char*)bev) - bev_f = (void*)( ((char*)bev) -
evutil_offsetof(struct bufferevent_filtered, bev.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; return bev_f;
} }

View File

@ -354,11 +354,11 @@ static inline struct bufferevent_openssl *
upcast(struct bufferevent *bev) upcast(struct bufferevent *bev)
{ {
struct bufferevent_openssl *bev_o; struct bufferevent_openssl *bev_o;
if (bev->be_ops != &bufferevent_ops_openssl) if (!BEV_IS_OPENSSL(bev))
return NULL; return NULL;
bev_o = (void*)( ((char*)bev) - bev_o = (void*)( ((char*)bev) -
evutil_offsetof(struct bufferevent_openssl, bev.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; return bev_o;
} }
@ -803,7 +803,7 @@ consider_reading(struct bufferevent_openssl *bev_ssl)
if (bev_ssl->bev.read_suspended) if (bev_ssl->bev.read_suspended)
break; break;
/* Read all pending data. This won't hit the network /* Read all pending data. This won't hit the network
* again, and will (most importantly) put us in a state * again, and will (most importantly) put us in a state
* where we don't need to read anything else until the * where we don't need to read anything else until the

View File

@ -56,10 +56,10 @@ static inline struct bufferevent_pair *
upcast(struct bufferevent *bev) upcast(struct bufferevent *bev)
{ {
struct bufferevent_pair *bev_p; struct bufferevent_pair *bev_p;
if (bev->be_ops != &bufferevent_ops_pair) if (!BEV_IS_PAIR(bev))
return NULL; return NULL;
bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev.bev); 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; return bev_p;
} }

View File

@ -607,7 +607,7 @@ be_socket_destruct(struct bufferevent *bufev)
struct bufferevent_private *bufev_p = struct bufferevent_private *bufev_p =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev); EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
evutil_socket_t fd; 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); 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); EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
BEV_LOCK(bufev); 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_read);
event_del(&bufev->ev_write); event_del(&bufev->ev_write);
@ -662,7 +662,7 @@ bufferevent_priority_set(struct bufferevent *bufev, int priority)
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev); EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
BEV_LOCK(bufev); BEV_LOCK(bufev);
if (bufev->be_ops != &bufferevent_ops_socket) if (!BEV_IS_SOCKET(bufev))
goto done; goto done;
if (event_priority_set(&bufev->ev_read, priority) == -1) 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; int res = -1;
BEV_LOCK(bufev); BEV_LOCK(bufev);
if (bufev->be_ops != &bufferevent_ops_socket) if (!BEV_IS_SOCKET(bufev))
goto done; goto done;
bufev->ev_base = base; bufev->ev_base = base;