mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-14 14:54:49 -04:00
address nick's comments and make evbuffer_pullup more efficient
svn:r680
This commit is contained in:
parent
7210592777
commit
00382110b2
38
buffer.c
38
buffer.c
@ -339,28 +339,37 @@ evbuffer_pullup(struct evbuffer *buf, int size)
|
|||||||
|
|
||||||
if (size == -1)
|
if (size == -1)
|
||||||
size = buf->total_len;
|
size = buf->total_len;
|
||||||
/* XXX Does it make more sense, if size > buf->total_len, to
|
/* if size > buf->total_len, we cannot guarantee to the user that she
|
||||||
* clip it downwards? */
|
* is going to have a long enough buffer afterwards; so we return
|
||||||
|
* NULL */
|
||||||
if (size == 0 || size > buf->total_len)
|
if (size == 0 || size > buf->total_len)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|
||||||
/* No need to pull up anything; the first size bytes are already here. */
|
/* No need to pull up anything; the first size bytes are
|
||||||
|
* already here. */
|
||||||
if (chain->off >= size)
|
if (chain->off >= size)
|
||||||
return chain->buffer + chain->misalign;
|
return chain->buffer + chain->misalign;
|
||||||
|
|
||||||
/* XXX is it possible that buf->chain will already have enough space
|
if (chain->buffer_len - chain->misalign >= size) {
|
||||||
* to hold size bytes? */
|
/* already have enough space in the first chain */
|
||||||
if ((tmp = evbuffer_chain_new(size)) == NULL) {
|
size_t old_off = chain->off;
|
||||||
event_warn("%s: out of memory\n", __func__);
|
buffer = chain->buffer + chain->misalign + chain->off;
|
||||||
return (NULL);
|
tmp = chain;
|
||||||
|
tmp->off = size;
|
||||||
|
size -= old_off;
|
||||||
|
chain = chain->next;
|
||||||
|
} else {
|
||||||
|
if ((tmp = evbuffer_chain_new(size)) == NULL) {
|
||||||
|
event_warn("%s: out of memory\n", __func__);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
buffer = tmp->buffer;
|
||||||
|
tmp->off = size;
|
||||||
|
buf->first = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp->off = size;
|
|
||||||
buffer = tmp->buffer;
|
|
||||||
|
|
||||||
/* Copy and free every chunk that will be entirely pulled into tmp */
|
/* Copy and free every chunk that will be entirely pulled into tmp */
|
||||||
for (chain = buf->first;
|
for (; chain != NULL && size >= chain->off; chain = next) {
|
||||||
chain != NULL && size >= chain->off; chain = next) {
|
|
||||||
next = chain->next;
|
next = chain->next;
|
||||||
|
|
||||||
memcpy(buffer, chain->buffer + chain->misalign, chain->off);
|
memcpy(buffer, chain->buffer + chain->misalign, chain->off);
|
||||||
@ -378,10 +387,9 @@ evbuffer_pullup(struct evbuffer *buf, int size)
|
|||||||
buf->last = tmp;
|
buf->last = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf->first = tmp;
|
|
||||||
tmp->next = chain;
|
tmp->next = chain;
|
||||||
|
|
||||||
return (tmp->buffer);
|
return (tmp->buffer + tmp->misalign);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user