Change the interface of evbuffer_add_reference: give the cleanup function more info.

svn:r1294
This commit is contained in:
Nick Mathewson 2009-05-15 22:44:18 +00:00
parent bba69e03f8
commit dc4c7b9570
5 changed files with 24 additions and 8 deletions

View File

@ -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 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 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 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: Changes in 2.0.1-alpha:

View File

@ -185,7 +185,9 @@ evbuffer_chain_free(struct evbuffer_chain *chain)
struct evbuffer_chain_reference, struct evbuffer_chain_reference,
chain); chain);
if (info->cleanupfn) if (info->cleanupfn)
(*info->cleanupfn)(info->extra); (*info->cleanupfn)(chain->buffer,
chain->buffer_len,
info->extra);
} }
#ifdef _EVENT_HAVE_MMAP #ifdef _EVENT_HAVE_MMAP
if (chain->flags & EVBUFFER_MMAP) { if (chain->flags & EVBUFFER_MMAP) {
@ -2007,7 +2009,7 @@ evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
int int
evbuffer_add_reference(struct evbuffer *outbuf, evbuffer_add_reference(struct evbuffer *outbuf,
const void *data, size_t datlen, 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 *chain;
struct evbuffer_chain_reference *info; struct evbuffer_chain_reference *info;

View File

@ -188,7 +188,7 @@ struct evbuffer_chain_fd {
/** callback for a reference buffer; lets us know what to do with it when /** callback for a reference buffer; lets us know what to do with it when
* we're done with it. */ * we're done with it. */
struct evbuffer_chain_reference { struct evbuffer_chain_reference {
void (*cleanupfn)(void *extra); evbuffer_ref_cleanup_cb cleanupfn;
void *extra; void *extra;
}; };

View File

@ -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); 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. 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 @param extra optional argument to the cleanup callback
@return 0 if successful, or -1 if an error occurred @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, int evbuffer_add_reference(struct evbuffer *outbuf,
const void *data, size_t datlen, 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. Move data from a file into the evbuffer for writing to a socket.

View File

@ -218,10 +218,14 @@ test_evbuffer(void *ptr)
static int reference_cb_called; static int reference_cb_called;
static void 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); tt_want(extra == (void *)0xdeadaffe);
++reference_cb_called; ++reference_cb_called;
end:
;
} }
static void static void
@ -722,10 +726,14 @@ test_evbuffer_callbacks(void *ptr)
static int ref_done_cb_called_count = 0; static int ref_done_cb_called_count = 0;
static void *ref_done_cb_called_with = NULL; 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_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 static void
@ -764,6 +772,8 @@ test_evbuffer_add_reference(void *ptr)
evbuffer_remove(buf1, tmp, 1); evbuffer_remove(buf1, tmp, 1);
tt_int_op(tmp[0], ==, 'm'); tt_int_op(tmp[0], ==, 'm');
tt_assert(ref_done_cb_called_with == (void*)111); 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); tt_int_op(ref_done_cb_called_count, ==, 1);
/* Drain some of the remaining chunk, then add it to another buffer */ /* Drain some of the remaining chunk, then add it to another buffer */