diff options
author | Waldemar Brodkorb <wbx@openadk.org> | 2015-12-09 08:05:10 +0100 |
---|---|---|
committer | Waldemar Brodkorb <wbx@openadk.org> | 2015-12-17 20:35:19 +0100 |
commit | 219b69d72e878094f3ce03a9e70719709a9b4c43 (patch) | |
tree | c79c264c6e76d470e88ff57491ba3bf13c8aef46 /libc/stdio/fopencookie.c | |
parent | 2e116b2eeea5f47ca26458b1962783baced2784c (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/fopencookie.c')
-rw-r--r-- | libc/stdio/fopencookie.c | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/libc/stdio/fopencookie.c b/libc/stdio/fopencookie.c index c4927e0bd..216dac39d 100644 --- a/libc/stdio/fopencookie.c +++ b/libc/stdio/fopencookie.c @@ -39,21 +39,34 @@ FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode, #endif { FILE *stream; + _IO_cookie_file_t *new_f; + new_f = malloc(sizeof(_IO_cookie_file_t)); + if (new_f == NULL) { + return NULL; + } + new_f->__fp.__modeflags = __FLAG_FREEFILE; +#ifdef __STDIO_BUFFERS + new_f->__fp.__bufstart = NULL; /* We allocate a buffer below. */ +#endif +#ifdef __UCLIBC_HAS_THREADS__ + /* We only initialize the mutex in the non-freopen case. */ + STDIO_INIT_MUTEX(new_f->__fp.__lock); +#endif /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement * check without an fcntl call. */ - stream = _stdio_fopen(((intptr_t)(INT_MAX-1)), mode, NULL, INT_MAX); + stream = _stdio_fopen(((intptr_t)(INT_MAX-1)), mode, &new_f->__fp, INT_MAX); if (stream) { - stream->__filedes = -1; + stream->__filedes = __STDIO_STREAM_GLIBC_CUSTOM_FILEDES; #ifndef __BCC__ - stream->__gcs = io_functions; + new_f->__gcs = io_functions; #else - stream->__gcs.read = io_functions->read; - stream->__gcs.write = io_functions->write; - stream->__gcs.seek = io_functions->seek; - stream->__gcs.close = io_functions->close; + new_f->__gcs.read = io_functions->read; + new_f->__gcs.write = io_functions->write; + new_f->__gcs.seek = io_functions->seek; + new_f->__gcs.close = io_functions->close; #endif - stream->__cookie = cookie; + new_f->__cookie = cookie; __STDIO_STREAM_VALIDATE(stream); } |