X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=lib%2Foutput_buffer.c;h=beb8a1426a1a1e620fbe783f9e0648a95dd6c181;hb=afc22c98609ee80a99fbed24231181bdab2bc659;hp=7a8d792f26294c1afc956ba57dfa7acca2a4769a;hpb=a666cab8ed3d2a2fa782be345425a2e88734a007;p=fio.git diff --git a/lib/output_buffer.c b/lib/output_buffer.c index 7a8d792f..beb8a142 100644 --- a/lib/output_buffer.c +++ b/lib/output_buffer.c @@ -1,44 +1,41 @@ -#include #include #include #include "output_buffer.h" -#include "../log.h" +#include "../minmax.h" #define BUF_INC 1024 -void buf_output_init(struct buf_output *out, int index) +void buf_output_init(struct buf_output *out) { - out->max_buflen = BUF_INC; - out->buf = malloc(out->max_buflen); - memset(out->buf, 0, out->max_buflen); + out->max_buflen = 0; out->buflen = 0; + out->buf = NULL; } void buf_output_free(struct buf_output *out) { free(out->buf); + buf_output_init(out); } -void buf_output_add(struct buf_output *out, const char *buf, size_t len) +size_t buf_output_add(struct buf_output *out, const char *buf, size_t len) { - while (out->max_buflen - out->buflen < len) { - size_t newlen = out->max_buflen + BUF_INC - out->buflen; - - out->buf = realloc(out->buf, out->max_buflen + BUF_INC); - out->max_buflen += BUF_INC; - memset(&out->buf[out->buflen], 0, newlen); + if (out->max_buflen - out->buflen < len) { + size_t need = len - (out->max_buflen - out->buflen); + size_t old_max = out->max_buflen; + + need = max((size_t) BUF_INC, need); + out->max_buflen += need; + out->buf = realloc(out->buf, out->max_buflen); + + 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); out->buflen += len; -} - -void buf_output_flush(struct buf_output *out) -{ - if (out->buflen) { - log_local_buf(out->buf, out->buflen); - memset(out->buf, 0, out->max_buflen); - out->buflen = 0; - } + return len; }