mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-12 22:10:12 -04:00
Make evbuffer_prepend handle empty buffers better
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.
This commit is contained in:
parent
5c0ebb33f4
commit
c87272b7b9
8
buffer.c
8
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user