[klibc] [klibc:master] stdio: Define all the _unlocked functions and macros

klibc-bot for Ben Hutchings ben at decadent.org.uk
Sat Jul 25 13:57:07 PDT 2020


Commit-ID:  8c056cab6c8cce0b5dbc2c3141060f89a6ffc905
Gitweb:     http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=8c056cab6c8cce0b5dbc2c3141060f89a6ffc905
Author:     Ben Hutchings <ben at decadent.org.uk>
AuthorDate: Sat, 25 Jul 2020 21:18:30 +0100
Committer:  Ben Hutchings <ben at decadent.org.uk>
CommitDate: Sat, 25 Jul 2020 21:44:14 +0100

[klibc] stdio: Define all the _unlocked functions and macros

Clang 9.0 can optimise stdio function calls to use the _unlocked
variants, presumably based on escape analysis.  We don't define many
of them, and adding a lot of -fno-builtin-* options to inhibit this
seems like a losing battle.

Since we don't support multi-threaded programs or locking in stdio,
define the _unlocked function names as aliases.

For completeness, also define corresponding _unlocked macros for the
macro-only pseudo-functions, and for the inline functions in stdio.h.

Signed-off-by: Ben Hutchings <ben at decadent.org.uk>

---
 usr/include/stdio.h        | 20 ++++++++++++++++++++
 usr/klibc/fgets.c          |  1 +
 usr/klibc/fputc.c          |  1 +
 usr/klibc/fputs.c          |  1 +
 usr/klibc/fread2.c         |  1 +
 usr/klibc/fwrite2.c        |  2 ++
 usr/klibc/stdio/clearerr.c |  1 +
 usr/klibc/stdio/feof.c     |  1 +
 usr/klibc/stdio/ferror.c   |  1 +
 usr/klibc/stdio/fflush.c   |  2 +-
 usr/klibc/stdio/fgetc.c    |  1 +
 usr/klibc/stdio/fileno.c   |  1 +
 12 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/usr/include/stdio.h b/usr/include/stdio.h
index 1d45fe1a..521213df 100644
--- a/usr/include/stdio.h
+++ b/usr/include/stdio.h
@@ -48,17 +48,24 @@ __extern int fseek(FILE *, off_t, int);
 #define fseeko fseek
 __extern void rewind(FILE *);
 __extern int fputs(const char *, FILE *);
+__extern int fputs_unlocked(const char *, FILE *);
 __extern int puts(const char *);
 __extern int fputc(int, FILE *);
+__extern int fputc_unlocked(int, FILE *);
 #define putc(c,f)  fputc((c),(f))
+#define putc_unlocked(c,f)  putc((c),(f))
 #define putchar(c) fputc((c),stdout)
+#define putchar_unlocked(c) putchar(c)
 
 __extern int fgetc(FILE *);
+__extern int fgetc_unlocked(FILE *);
 __extern char *fgets(char *, int, FILE *);
+__extern char *fgets_unlocked(char *, int, FILE *);
 #define getc(f) fgetc(f)
 __extern int getc_unlocked(FILE *);
 #define getc_unlocked(f) fgetc(f)
 #define getchar() fgetc(stdin)
+#define getchar_unlocked() getchar()
 __extern int ungetc(int, FILE *);
 
 __extern int printf(const char *, ...);
@@ -86,17 +93,24 @@ __extern int remove(const char *);
 __extern size_t _fread(void *, size_t, FILE *);
 __extern size_t _fwrite(const void *, size_t, FILE *);
 __extern int fflush(FILE *);
+__extern int fflush_unlocked(FILE *);
 
 __extern size_t fread(void *, size_t, size_t, FILE *);
+__extern size_t fread_unlocked(void *, size_t, size_t, FILE *);
 __extern size_t fwrite(const void *, size_t, size_t, FILE *);
+__extern size_t fwrite_unlocked(const void *, size_t, size_t, FILE *);
 
 __extern off_t ftell(FILE *__f);
 #define ftello ftell
 
 __extern int ferror(FILE * );
+__extern int ferror_unlocked(FILE * );
 __extern int feof(FILE *);
+__extern int feof_unlocked(FILE *);
 __extern int fileno(FILE *);
+__extern int fileno_unlocked(FILE *);
 __extern void clearerr(FILE *);
+__extern void clearerr_unlocked(FILE *);
 
 #ifndef __NO_STDIO_INLINES
 __extern_inline size_t
@@ -104,33 +118,39 @@ fread(void *__p, size_t __s, size_t __n, FILE * __f)
 {
 	return _fread(__p, __s * __n, __f) / __s;
 }
+#define fread_unlocked(p, s, n, f) fread((p), (s), (n), (f))
 
 __extern_inline size_t
 fwrite(const void *__p, size_t __s, size_t __n, FILE * __f)
 {
 	return _fwrite(__p, __s * __n, __f) / __s;
 }
+#define fwrite_unlocked(p, s, n, f) fwrite((p), (s), (n), (f))
 
 __extern_inline int fileno(FILE *__f)
 {
 	return __f->_IO_fileno;
 }
+#define fileno_unlocked(f) fileno(f)
 
 __extern_inline int ferror(FILE *__f)
 {
 	return __f->_IO_error;
 }
+#define ferror_unlocked(f) ferror(f)
 
 __extern_inline int feof(FILE *__f)
 {
 	return __f->_IO_eof;
 }
+#define feof_unlocked(f) feof(f)
 
 __extern_inline void clearerr(FILE *__f)
 {
 	__f->_IO_error = 0;
 	__f->_IO_eof = 0;
 }
+#define clearerr_unlocked(f) clearerr(f)
 #endif
 
 #endif				/* _STDIO_H */
diff --git a/usr/klibc/fgets.c b/usr/klibc/fgets.c
index dbf742c6..b6e6f3ea 100644
--- a/usr/klibc/fgets.c
+++ b/usr/klibc/fgets.c
@@ -25,3 +25,4 @@ char *fgets(char *s, int n, FILE *f)
 
 	return s;
 }
+__ALIAS(char *, fgets_unlocked, (char *, int, FILE *), fgets)
diff --git a/usr/klibc/fputc.c b/usr/klibc/fputc.c
index 386d86cb..7385d6fc 100644
--- a/usr/klibc/fputc.c
+++ b/usr/klibc/fputc.c
@@ -12,3 +12,4 @@ int fputc(int c, FILE *f)
 
 	return _fwrite(&ch, 1, f) == 1 ? ch : EOF;
 }
+__ALIAS(int, fputc_unlocked, (int, FILE *), fputc)
diff --git a/usr/klibc/fputs.c b/usr/klibc/fputs.c
index fb240d69..cbc2056b 100644
--- a/usr/klibc/fputs.c
+++ b/usr/klibc/fputs.c
@@ -13,3 +13,4 @@ int fputs(const char *s, FILE *file)
 {
 	return _fwrite(s, strlen(s), file);
 }
+__ALIAS(int, fputs_unlocked, (const char *, FILE *), fputs)
diff --git a/usr/klibc/fread2.c b/usr/klibc/fread2.c
index 7dca56b1..f5b2acb4 100644
--- a/usr/klibc/fread2.c
+++ b/usr/klibc/fread2.c
@@ -11,3 +11,4 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE * f)
 {
 	return _fread(ptr, size * nmemb, f) / size;
 }
+__ALIAS(size_t, fread_unlocked, (void *, size_t, size_t, FILE *), fread)
diff --git a/usr/klibc/fwrite2.c b/usr/klibc/fwrite2.c
index cebc017c..a8c14c98 100644
--- a/usr/klibc/fwrite2.c
+++ b/usr/klibc/fwrite2.c
@@ -11,3 +11,5 @@ size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE * f)
 {
 	return _fwrite(ptr, size * nmemb, f) / size;
 }
+__ALIAS(size_t, fwrite_unlocked, (const void *, size_t, size_t, FILE *),
+	fwrite)
diff --git a/usr/klibc/stdio/clearerr.c b/usr/klibc/stdio/clearerr.c
index fb565144..ebdd22de 100644
--- a/usr/klibc/stdio/clearerr.c
+++ b/usr/klibc/stdio/clearerr.c
@@ -6,3 +6,4 @@ void clearerr(FILE *__f)
 	__f->_IO_error = 0;
 	__f->_IO_eof = 0;
 }
+__ALIAS(void, clearerr_unlocked, (FILE *), clearerr)
diff --git a/usr/klibc/stdio/feof.c b/usr/klibc/stdio/feof.c
index 590b1c59..4215c113 100644
--- a/usr/klibc/stdio/feof.c
+++ b/usr/klibc/stdio/feof.c
@@ -5,3 +5,4 @@ int feof(FILE *__f)
 {
 	return __f->_IO_eof;
 }
+__ALIAS(int, feof_unlocked, (FILE *), feof)
diff --git a/usr/klibc/stdio/ferror.c b/usr/klibc/stdio/ferror.c
index 8b36e443..819743b9 100644
--- a/usr/klibc/stdio/ferror.c
+++ b/usr/klibc/stdio/ferror.c
@@ -5,3 +5,4 @@ int ferror(FILE *__f)
 {
 	return __f->_IO_error;
 }
+__ALIAS(int, ferror_unlocked, (FILE *), ferror)
diff --git a/usr/klibc/stdio/fflush.c b/usr/klibc/stdio/fflush.c
index dfccd24d..6ac5cf57 100644
--- a/usr/klibc/stdio/fflush.c
+++ b/usr/klibc/stdio/fflush.c
@@ -56,5 +56,5 @@ int fflush(FILE *file)
 		return err;
 	}
 }
-
+__ALIAS(int, fflush_unlocked, (FILE *), fflush)
 
diff --git a/usr/klibc/stdio/fgetc.c b/usr/klibc/stdio/fgetc.c
index a0e8650f..5d1fc06c 100644
--- a/usr/klibc/stdio/fgetc.c
+++ b/usr/klibc/stdio/fgetc.c
@@ -16,3 +16,4 @@ int fgetc(FILE *file)
 		return _fread(&ch, 1, file) == 1 ? ch : EOF;
 	}
 }
+__ALIAS(int, fgetc_unlocked, (FILE *), fgetc)
diff --git a/usr/klibc/stdio/fileno.c b/usr/klibc/stdio/fileno.c
index b5a10161..16c15dd8 100644
--- a/usr/klibc/stdio/fileno.c
+++ b/usr/klibc/stdio/fileno.c
@@ -5,3 +5,4 @@ int fileno(FILE *__f)
 {
 	return __f->_IO_fileno;
 }
+__ALIAS(int, fileno_unlocked, (FILE *), fileno)


More information about the klibc mailing list