From 9f1a94ecec836aae50ce761f1a269881a5f42167 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 13 Apr 2009 03:06:47 +0000 Subject: [PATCH] add pin/unpin functions, and a deref-and-free pair. svn:r1165 --- buffer.c | 24 ++++++++++++++---------- evbuffer-internal.h | 3 +++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/buffer.c b/buffer.c index 3c2c7db1..d08a0709 100644 --- a/buffer.c +++ b/buffer.c @@ -231,23 +231,21 @@ evbuffer_chain_insert(struct evbuffer *buf, struct evbuffer_chain *chain) buf->total_len += chain->off; } -#ifdef NOT_USED_YET -static void -evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag) +void +_evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag) { assert((chain->flags & flag) == 0); chain->flags |= flag; } -static void -evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag) +void +_evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag) { assert((chain->flags & flag) != 0); chain->flags &= ~flag; if (chain->flags & EVBUFFER_DANGLING) evbuffer_chain_free(chain); } -#endif struct evbuffer * evbuffer_new(void) @@ -370,8 +368,7 @@ evbuffer_deferred_callback(struct deferred_cb *cb, void *arg) EVBUFFER_LOCK(buffer, EVTHREAD_WRITE); evbuffer_run_callbacks(buffer); - evbuffer_free(buffer); /* release the reference */ - EVBUFFER_UNLOCK(buffer, EVTHREAD_WRITE); + _evbuffer_decref_and_unlock(buffer); } static void @@ -386,11 +383,11 @@ evbuffer_remove_all_callbacks(struct evbuffer *buffer) } void -evbuffer_free(struct evbuffer *buffer) +_evbuffer_decref_and_unlock(struct evbuffer *buffer) { struct evbuffer_chain *chain, *next; + ASSERT_EVBUFFER_LOCKED(buffer); - EVBUFFER_LOCK(buffer, EVTHREAD_WRITE); if (--buffer->refcnt > 0) { EVBUFFER_UNLOCK(buffer, EVTHREAD_WRITE); return; @@ -410,6 +407,13 @@ evbuffer_free(struct evbuffer *buffer) mm_free(buffer); } +void +evbuffer_free(struct evbuffer *buffer) +{ + EVBUFFER_LOCK(buffer, EVTHREAD_WRITE); + _evbuffer_decref_and_unlock(buffer); +} + void evbuffer_lock(struct evbuffer *buf) { diff --git a/evbuffer-internal.h b/evbuffer-internal.h index 31e51154..d6d9b1c0 100644 --- a/evbuffer-internal.h +++ b/evbuffer-internal.h @@ -194,6 +194,9 @@ struct evbuffer_chain_reference { } while(0) void _evbuffer_incref(struct evbuffer *buf); +void _evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag); +void _evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag); +void _evbuffer_decref_and_unlock(struct evbuffer *buffer); #ifdef _EVENT_HAVE_SYS_UIO_H int _evbuffer_read_setup_vecs(struct evbuffer *buf, ssize_t howmuch, -- 2.40.0