From: Martin Mares Date: Thu, 19 Aug 2004 13:47:23 +0000 (+0000) Subject: A better implementation of vbprintf() which avoids copying if there is enough X-Git-Tag: holmes-import~915 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=9c21b55e57a42cabe0ee81489ce232834885f7b1;p=libucw.git A better implementation of vbprintf() which avoids copying if there is enough room in the fastbuf's buffer. --- 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; }