From: Mark Ellzey Date: Wed, 6 May 2015 21:56:31 +0000 (-0700) Subject: expose bufferevent_incref/decref (with fewer modifications) X-Git-Tag: release-2.1.6-beta~90^2~91^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1ed6718d1dbf3fb3023122ef5242511a6522d84e;p=libevent expose bufferevent_incref/decref (with fewer modifications) --- diff --git a/bufferevent-internal.h b/bufferevent-internal.h index 134bb337..ad3dcfc9 100644 --- a/bufferevent-internal.h +++ b/bufferevent-internal.h @@ -327,14 +327,17 @@ int bufferevent_disable_hard_(struct bufferevent *bufev, short event); /** Internal: Set up locking on a bufferevent. If lock is set, use it. * Otherwise, use a new lock. */ int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock); -/** Internal: Increment the reference count on bufev. */ -void bufferevent_incref_(struct bufferevent *bufev); +/** Internal: backwards compat macro for the now public function + * Increment the reference count on bufev. */ +#define bufferevent_incref_(bufev) bufferevent_incref(bufev) /** Internal: Lock bufev and increase its reference count. * unlocking it otherwise. */ void bufferevent_incref_and_lock_(struct bufferevent *bufev); -/** Internal: Decrement the reference count on bufev. Returns 1 if it freed +/** Internal: backwards compat macro for the now public function + * Decrement the reference count on bufev. Returns 1 if it freed * the bufferevent.*/ -int bufferevent_decref_(struct bufferevent *bufev); +#define bufferevent_decref_(bufev) bufferevent_decref(bufev) + /** Internal: Drop the reference count on bufev, freeing as necessary, and * unlocking it otherwise. Returns 1 if it freed the bufferevent. */ int bufferevent_decref_and_unlock_(struct bufferevent *bufev); diff --git a/bufferevent.c b/bufferevent.c index d298d0b3..7d3d6935 100644 --- a/bufferevent.c +++ b/bufferevent.c @@ -777,7 +777,7 @@ bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_) } int -bufferevent_decref_(struct bufferevent *bufev) +bufferevent_decref(struct bufferevent *bufev) { BEV_LOCK(bufev); return bufferevent_decref_and_unlock_(bufev); @@ -793,11 +793,15 @@ bufferevent_free(struct bufferevent *bufev) } void -bufferevent_incref_(struct bufferevent *bufev) +bufferevent_incref(struct bufferevent *bufev) { struct bufferevent_private *bufev_private = EVUTIL_UPCAST(bufev, struct bufferevent_private, bev); + /* XXX: now that this function is public, we might want to + * - return the count from this function + * - create a new function to atomically grab the current refcount + */ BEV_LOCK(bufev); ++bufev_private->refcnt; BEV_UNLOCK(bufev); diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h index fe8a74d3..f041fa85 100644 --- a/include/event2/bufferevent.h +++ b/include/event2/bufferevent.h @@ -562,6 +562,32 @@ void bufferevent_lock(struct bufferevent *bufev); EVENT2_EXPORT_SYMBOL void bufferevent_unlock(struct bufferevent *bufev); + +/** + * Public interface to manually increase the reference count of a bufferevent + * this is useful in situations where a user may reference the bufferevent + * somewhere eles (unknown to libevent) + * + * @param bufev the bufferevent to increase the refcount on + * + */ +EVENT2_EXPORT_SYMBOL +void bufferevent_incref(struct bufferevent *bufev); + +/** + * Public interface to manually decrement the reference count of a bufferevent + * + * Warning: make sure you know what you're doing. This is mainly used in + * conjunction with bufferevent_incref(). This will free up all data associated + * with a bufferevent if the reference count hits 0. + * + * @param bufev the bufferevent to decrement the refcount on + * + * @return 1 if the bufferevent was freed, otherwise 0 (still referenced) + */ +EVENT2_EXPORT_SYMBOL +int bufferevent_decref(struct bufferevent *bufev); + /** Flags that can be passed into filters to let them know how to deal with the incoming data.