From: Nick Mathewson Date: Fri, 15 May 2009 22:44:18 +0000 (+0000) Subject: Change the interface of evbuffer_add_reference: give the cleanup function more info. X-Git-Tag: release-2.0.3-alpha~225 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc4c7b95707864d09d710fbe4736f05a39e823a0;p=libevent Change the interface of evbuffer_add_reference: give the cleanup function more info. svn:r1294 --- diff --git a/ChangeLog b/ChangeLog index fa0ee380..b79ddfaf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -27,6 +27,7 @@ Changes in 2.0.2-alpha: o Shave 22 bytes off struct event on 32-bit platforms by shrinking and re-ordering fields. The savings on 64-bit platforms is likely higher. o Cap the maximum number of priorities at 256. o Change the semantics of evbuffer_cb_set_flags() to be set-flag only; add a new evbuffer_cb_clear_flags() to remove set flags. + o Change the interface of evbuffer_add_reference so that the cleanup callback gets more information Changes in 2.0.1-alpha: diff --git a/buffer.c b/buffer.c index b2f16c3c..aaab9776 100644 --- a/buffer.c +++ b/buffer.c @@ -185,7 +185,9 @@ evbuffer_chain_free(struct evbuffer_chain *chain) struct evbuffer_chain_reference, chain); if (info->cleanupfn) - (*info->cleanupfn)(info->extra); + (*info->cleanupfn)(chain->buffer, + chain->buffer_len, + info->extra); } #ifdef _EVENT_HAVE_MMAP if (chain->flags & EVBUFFER_MMAP) { @@ -2007,7 +2009,7 @@ evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...) int evbuffer_add_reference(struct evbuffer *outbuf, const void *data, size_t datlen, - void (*cleanupfn)(void *extra), void *extra) + evbuffer_ref_cleanup_cb cleanupfn, void *extra) { struct evbuffer_chain *chain; struct evbuffer_chain_reference *info; diff --git a/evbuffer-internal.h b/evbuffer-internal.h index b70e4305..bd41bfc2 100644 --- a/evbuffer-internal.h +++ b/evbuffer-internal.h @@ -188,7 +188,7 @@ struct evbuffer_chain_fd { /** callback for a reference buffer; lets us know what to do with it when * we're done with it. */ struct evbuffer_chain_reference { - void (*cleanupfn)(void *extra); + evbuffer_ref_cleanup_cb cleanupfn; void *extra; }; diff --git a/include/event2/buffer.h b/include/event2/buffer.h index b45398bd..352c3c9b 100644 --- a/include/event2/buffer.h +++ b/include/event2/buffer.h @@ -278,6 +278,10 @@ char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out, */ int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf); + +typedef void (*evbuffer_ref_cleanup_cb)(const void *data, + size_t datalen, void *extra); + /** Reference memory into an evbuffer without copying. @@ -293,10 +297,9 @@ int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf); @param extra optional argument to the cleanup callback @return 0 if successful, or -1 if an error occurred */ -/* XXXX Should the cleanupfn get a copy of the data pointer too? */ int evbuffer_add_reference(struct evbuffer *outbuf, const void *data, size_t datlen, - void (*cleanupfn)(void *extra), void *extra); + evbuffer_ref_cleanup_cb cleanupfn, void *extra); /** Move data from a file into the evbuffer for writing to a socket. diff --git a/test/regress_buffer.c b/test/regress_buffer.c index 1a04ef40..05d58635 100644 --- a/test/regress_buffer.c +++ b/test/regress_buffer.c @@ -218,10 +218,14 @@ test_evbuffer(void *ptr) static int reference_cb_called; static void -reference_cb(void *extra) +reference_cb(const void *data, size_t len, void *extra) { + tt_str_op(data, ==, "this is what we add as read-only memory."); + tt_int_op(len, ==, strlen(data)); tt_want(extra == (void *)0xdeadaffe); ++reference_cb_called; +end: + ; } static void @@ -722,10 +726,14 @@ test_evbuffer_callbacks(void *ptr) static int ref_done_cb_called_count = 0; static void *ref_done_cb_called_with = NULL; -static void ref_done_cb(void *data) +static const void *ref_done_cb_called_with_data = NULL; +static size_t ref_done_cb_called_with_len = 0; +static void ref_done_cb(const void *data, size_t len, void *info) { ++ref_done_cb_called_count; - ref_done_cb_called_with = data; + ref_done_cb_called_with = info; + ref_done_cb_called_with_data = data; + ref_done_cb_called_with_len = len; } static void @@ -764,6 +772,8 @@ test_evbuffer_add_reference(void *ptr) evbuffer_remove(buf1, tmp, 1); tt_int_op(tmp[0], ==, 'm'); tt_assert(ref_done_cb_called_with == (void*)111); + tt_assert(ref_done_cb_called_with_data == chunk1); + tt_assert(ref_done_cb_called_with_len == len1); tt_int_op(ref_done_cb_called_count, ==, 1); /* Drain some of the remaining chunk, then add it to another buffer */