X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=lib%2Foutput_buffer.c;h=c1fdfc95f610aca10ece35ebdc2dac437c01af66;hp=da7087470ec41f131f0f03d8112086c223ab6bf8;hb=f6cbf8ac4f70c800bbbfc23c5dcf44ed619c0acc;hpb=e250c0a967d683e40889cc33ffc4c4003adc8d35;ds=sidebyside diff --git a/lib/output_buffer.c b/lib/output_buffer.c index da708747..c1fdfc95 100644 --- a/lib/output_buffer.c +++ b/lib/output_buffer.c @@ -4,6 +4,7 @@ #include "output_buffer.h" #include "../log.h" +#include "../minmax.h" #define BUF_INC 1024 @@ -21,12 +22,18 @@ void buf_output_free(struct buf_output *out) size_t buf_output_add(struct buf_output *out, const char *buf, size_t len) { - while (out->max_buflen - out->buflen < len) { + if (out->max_buflen - out->buflen < len) { + size_t need = len - (out->max_buflen - out->buflen); size_t old_max = out->max_buflen; - out->max_buflen += BUF_INC; + need = max((size_t) BUF_INC, need); + out->max_buflen += need; out->buf = realloc(out->buf, out->max_buflen); - memset(&out->buf[old_max], 0, BUF_INC); + + old_max = max(old_max, out->buflen + len); + if (old_max + need > out->max_buflen) + need = out->max_buflen - old_max; + memset(&out->buf[old_max], 0, need); } memcpy(&out->buf[out->buflen], buf, len); @@ -39,7 +46,7 @@ size_t buf_output_flush(struct buf_output *out) size_t ret = 0; if (out->buflen) { - ret = log_local_buf(out->buf, out->buflen); + ret = log_info_buf(out->buf, out->buflen); memset(out->buf, 0, out->max_buflen); out->buflen = 0; }