From 9c21b55e57a42cabe0ee81489ce232834885f7b1 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Thu, 19 Aug 2004 13:47:23 +0000 Subject: [PATCH] A better implementation of vbprintf() which avoids copying if there is enough room in the fastbuf's buffer. --- lib/ff-printf.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/ff-printf.c b/lib/ff-printf.c index 395e04b2..d22d421d 100644 --- a/lib/ff-printf.c +++ b/lib/ff-printf.c @@ -16,7 +16,24 @@ int vbprintf(struct fastbuf *b, byte *msg, va_list args) { byte *buf; - int r, len = 256; + int len, r; + + len = bdirect_write_prepare(b, &buf); + if (len >= 16) + { + r = vsnprintf(buf, len, msg, args); + if (r < 0) + len = 256; + else if (r < len) + { + bdirect_write_commit(b, buf+r); + return r; + } + else + len = r+1; + } + else + len = 256; while (1) { @@ -51,7 +68,8 @@ bprintf(struct fastbuf *b, byte *msg, ...) int main(void) { struct fastbuf *b = bfdopen_shared(1, 65536); - bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str"); + for (int i=0; i<10000; i++) + bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str"); bclose(b); return 0; } -- 2.39.5