Skip to content

Commit

Permalink
expose bufferevent_incref/decref (with fewer modifications)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Ellzey committed May 6, 2015
1 parent a695a72 commit 1ed6718
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
11 changes: 7 additions & 4 deletions bufferevent-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions bufferevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions include/event2/bufferevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1ed6718

Please sign in to comment.