From: Nick M. <ni...@us...> - 2010-03-26 18:59:02
|
Author: Nick Mathewson <ni...@to...> Date: Fri, 26 Mar 2010 14:51:39 -0400 Subject: Make evbuffer_prepend handle empty buffers better Commit: c87272b7b91b4f443092eb2a81f03c83457ab03a If the first chunk of a buffer is empty, and we're told to prepend to the buffer, we should be willing to use the entire first chunk. Instead, we were dependent on the value of chunk->misalign. --- buffer.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-) diff --git a/buffer.c b/buffer.c index 06974ba..5cbd10b 100644 --- a/buffer.c +++ b/buffer.c @@ -1398,8 +1398,13 @@ evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen) /* we cannot touch immutable buffers */ if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) { + /* If this chain is empty, we can treat it as + * 'empty at the beginning' rather than 'empty at the end' */ + if (chain->off == 0) + chain->misalign = chain->buffer_len; + if ((size_t)chain->misalign >= datlen) { - /* we have enough space */ + /* we have enough space to fit everything */ memcpy(chain->buffer + chain->misalign - datlen, data, datlen); chain->off += datlen; @@ -1408,6 +1413,7 @@ evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen) buf->n_add_for_cb += datlen; goto out; } else if (chain->misalign) { + /* we can only fit some of the data. */ memcpy(chain->buffer, (char*)data + datlen - chain->misalign, chain->misalign); -- 1.6.3 |