summaryrefslogtreecommitdiff
path: root/main/streams
diff options
context:
space:
mode:
Diffstat (limited to 'main/streams')
-rw-r--r--main/streams/streams.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/main/streams/streams.c b/main/streams/streams.c
index ce370941a8..f269f06f3e 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -590,8 +590,15 @@ PHPAPI int _php_stream_fill_read_buffer(php_stream *stream, size_t size)
* stream read buffer */
while (brig_inp->head) {
bucket = brig_inp->head;
- /* grow buffer to hold this bucket
- * TODO: this can fail for persistent streams */
+ /* reduce buffer memory consumption if possible, to avoid a realloc */
+ if (stream->readbuf && stream->readbuflen - stream->writepos < bucket->buflen) {
+ if (stream->writepos > stream->readpos) {
+ memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
+ }
+ stream->writepos -= stream->readpos;
+ stream->readpos = 0;
+ }
+ /* grow buffer to hold this bucket */
if (stream->readbuflen - stream->writepos < bucket->buflen) {
stream->readbuflen += bucket->buflen;
stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,