summaryrefslogtreecommitdiff
path: root/libc/stdio/vsnprintf.c
diff options
context:
space:
mode:
authorWaldemar Brodkorb <wbx@openadk.org>2015-12-09 08:05:10 +0100
committerWaldemar Brodkorb <wbx@openadk.org>2015-12-17 20:35:19 +0100
commit219b69d72e878094f3ce03a9e70719709a9b4c43 (patch)
treec79c264c6e76d470e88ff57491ba3bf13c8aef46 /libc/stdio/vsnprintf.c
parent2e116b2eeea5f47ca26458b1962783baced2784c (diff)
libc/stdio: Rework custom streams interface similar to glibc.
Save 20 bytes per FILE structure, avoid indirect call for read/write/seek/close operations for normal streams. Additionally, custom streams has fileno = -2 now, like in glibc. bloat-o-meter report (UCLIBC_HAS_GLIBC_CUSTOM_STREAMS=y): function old new delta fopencookie 69 131 +62 ftello64 233 260 +27 fseeko64 298 319 +21 fclose 423 442 +19 .rodata 16696 16708 +12 fileno_unlocked 53 45 -8 __ns_name_pack 859 851 -8 vswscanf 184 144 -40 vdprintf 231 187 -44 vsscanf 210 151 -59 vswprintf 269 201 -68 vsnprintf 249 181 -68 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 5/7 up/down: 141/-295) Total: -154 bytes Signed-off-by: Leonid Lisovskiy <lly.dev@gmail.com> Signed-off-by: Waldemar Brodkorb <wbx@uclibc-ng.org>
Diffstat (limited to 'libc/stdio/vsnprintf.c')
-rw-r--r--libc/stdio/vsnprintf.c46
1 files changed, 14 insertions, 32 deletions
diff --git a/libc/stdio/vsnprintf.c b/libc/stdio/vsnprintf.c
index 31adc52b6..3a4c60794 100644
--- a/libc/stdio/vsnprintf.c
+++ b/libc/stdio/vsnprintf.c
@@ -22,15 +22,6 @@ int vsnprintf(char *__restrict buf, size_t size,
FILE f;
int rv;
-/* __STDIO_STREAM_RESET_GCS(&f); */
-#ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
- f.__cookie = &(f.__filedes);
- f.__gcs.read = NULL;
- f.__gcs.write = NULL;
- f.__gcs.seek = NULL;
- f.__gcs.close = NULL;
-#endif
-
f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES;
f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
@@ -96,15 +87,6 @@ int vsnprintf(char *__restrict buf, size_t size,
}
f.bufend = buf + size;
-/* __STDIO_STREAM_RESET_GCS(&f.f); */
-#ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
- f.f.__cookie = &(f.f.__filedes);
- f.f.__gcs.read = NULL;
- f.f.__gcs.write = NULL;
- f.f.__gcs.seek = NULL;
- f.f.__gcs.close = NULL;
-#endif
-
f.f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB;
f.f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
@@ -137,7 +119,7 @@ libc_hidden_def(vsnprintf)
typedef struct {
size_t pos;
size_t len;
- unsigned char *buf;
+ char *buf;
FILE *fp;
} __snpf_cookie;
@@ -175,34 +157,34 @@ static ssize_t snpf_write(register void *cookie, const char *buf,
int vsnprintf(char *__restrict buf, size_t size,
const char * __restrict format, va_list arg)
{
- FILE f;
+ _IO_cookie_file_t cf;
__snpf_cookie cookie;
int rv;
cookie.buf = buf;
cookie.len = size;
cookie.pos = 0;
- cookie.fp = &f;
+ cookie.fp = &cf.__fp;
- f.__cookie = &cookie;
- f.__gcs.write = snpf_write;
- f.__gcs.read = NULL;
- f.__gcs.seek = NULL;
- f.__gcs.close = NULL;
+ cf.__cookie = &cookie;
+ cf.__gcs.write = snpf_write;
+ cf.__gcs.read = NULL;
+ cf.__gcs.seek = NULL;
+ cf.__gcs.close = NULL;
- f.__filedes = -1; /* For debugging. */
- f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
+ cf.__fp.__filedes = __STDIO_STREAM_GLIBC_CUSTOM_FILEDES;
+ cf.__fp.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
#ifdef __UCLIBC_HAS_WCHAR__
- f.__ungot_width[0] = 0;
+ cf.__fp.__ungot_width[0] = 0;
#endif /* __UCLIBC_HAS_WCHAR__ */
#ifdef __STDIO_MBSTATE
- __INIT_MBSTATE(&(f.__state));
+ __INIT_MBSTATE(&(cf.__fp.__state));
#endif /* __STDIO_MBSTATE */
- f.__nextopen = NULL;
+ cf.__fp.__nextopen = NULL;
- rv = _vfprintf_internal(&f, format, arg);
+ rv = _vfprintf_internal(&cf.__fp, format, arg);
return rv;
}