From 0b47b125cfe584f6d6c12a748208ceec3430990e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 12 Apr 2009 22:02:54 +0000 Subject: [PATCH] Add a new EVUTIL_UPCAST macro so that I do not need to keep figuring out the right offsetof magic over and over. svn:r1160 --- bufferevent_pair.c | 4 ++-- util-internal.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bufferevent_pair.c b/bufferevent_pair.c index 5e12ae8ae..444b954c1 100644 --- a/bufferevent_pair.c +++ b/bufferevent_pair.c @@ -39,6 +39,7 @@ #include "defer-internal.h" #include "bufferevent-internal.h" #include "mm-internal.h" +#include "util-internal.h" struct bufferevent_pair { struct bufferevent bev; @@ -56,8 +57,7 @@ upcast(struct bufferevent *bev) struct bufferevent_pair *bev_p; if (bev->be_ops != &bufferevent_ops_pair) return NULL; - bev_p = (void*)( ((char*)bev) - - evutil_offsetof(struct bufferevent_pair, bev) ); + bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev); assert(bev_p->bev.be_ops == &bufferevent_ops_pair); return bev_p; } diff --git a/util-internal.h b/util-internal.h index 957e431ab..31f2239f1 100644 --- a/util-internal.h +++ b/util-internal.h @@ -101,6 +101,9 @@ extern const char EVUTIL_TOLOWER_TABLE[]; #define EVUTIL_TOLOWER(c) (EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c]) #define EVUTIL_TOUPPER(c) (EVUTIL_TOUPPER_TABLE[(ev_uint8_t)c]) +#define EVUTIL_UPCAST(ptr, type, field) \ + ((type *)((char*)ptr) - evutil_offsetof(type, field)) + #ifdef __cplusplus } #endif