aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@linux.intel.com>2012-05-12 13:39:18 -0700
committerH. Peter Anvin <hpa@linux.intel.com>2012-05-12 15:19:32 -0700
commitd474cafc40ae5030873ed4d51a62d91146edd0c2 (patch)
tree22901ef79d80f2057445e9c8ea0badab301bfe50
parentc6a649166658c38d6b4ae6a2e039994566f3e5d9 (diff)
downloadklibc-d474cafc40ae5030873ed4d51a62d91146edd0c2.tar.gz
[klibc] Optimize fgetc() to read the buffer directly
If there is data in the buffer, optimize fgetc() to pull from the buffer directly. Continue to use _fread() to get data out of an empty buffer. This also speeds up fgets() since it calls fgetc(). Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--usr/klibc/Kbuild8
-rw-r--r--usr/klibc/fgetc.c18
-rw-r--r--usr/klibc/stdio/fgetc.c21
3 files changed, 25 insertions, 22 deletions
diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild
index d7335c22d973d..b3045a3cab141 100644
--- a/usr/klibc/Kbuild
+++ b/usr/klibc/Kbuild
@@ -19,8 +19,7 @@ klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
printf.o vprintf.o fprintf.o vfprintf.o perror.o \
statfs.o fstatfs.o umount.o \
creat.o open.o openat.o open_cloexec.o \
- fread2.o fgetc.o fgets.o \
- fwrite2.o fputc.o fputs.o puts.o putchar.o \
+ fread2.o fwrite2.o fgets.o fputc.o fputs.o puts.o putchar.o \
sleep.o usleep.o strtotimespec.o strtotimeval.o \
raise.o abort.o assert.o alarm.o pause.o \
__signal.o sysv_signal.o bsd_signal.o siglist.o sigabbrev.o \
@@ -61,8 +60,9 @@ klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
stdio/fclose.o stdio/fopen.o stdio/fdopen.o \
stdio/openmode.o stdio/fxopen.o \
stdio/fread.o stdio/fwrite.o stdio/fflush.o \
- stdio/fseek.o stdio/ftell.o stdio/rewind.o stdio/fileno.o \
- stdio/ungetc.o stdio/feof.o stdio/ferror.o
+ stdio/ungetc.o stdio/fgetc.o \
+ stdio/fseek.o stdio/ftell.o stdio/rewind.o \
+ stdio/fileno.o stdio/feof.o stdio/ferror.o
klib-$(CONFIG_KLIBC_ERRLIST) += errlist.o
diff --git a/usr/klibc/fgetc.c b/usr/klibc/fgetc.c
deleted file mode 100644
index 5b136b15a9183..0000000000000
--- a/usr/klibc/fgetc.c
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * fgetc.c
- *
- * Extremely slow fgetc implementation, using _fread(). If people
- * actually need character-oriented input to be fast, we may actually
- * have to implement buffering. Sigh.
- */
-
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-int fgetc(FILE *f)
-{
- unsigned char ch;
-
- return (_fread(&ch, 1, f) == 1) ? (int)ch : EOF;
-}
diff --git a/usr/klibc/stdio/fgetc.c b/usr/klibc/stdio/fgetc.c
new file mode 100644
index 0000000000000..83b2e4c07334a
--- /dev/null
+++ b/usr/klibc/stdio/fgetc.c
@@ -0,0 +1,21 @@
+/*
+ * fgetc.c
+ */
+
+#include "stdioint.h"
+
+int fgetc(FILE *file)
+{
+ struct _IO_file_pvt *f = stdio_pvt(file);
+ unsigned char ch;
+
+ if (__likely(f->ibytes)) {
+ ch = *f->data++;
+ f->ibytes--;
+ f->pub._io_filepos++;
+ } else if (_fread(&ch, 1, file) != 1) {
+ return EOF;
+ }
+
+ return (int)ch;
+}