summaryrefslogtreecommitdiff
path: root/src/stdio
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2015-06-16 07:11:19 +0000
committerRich Felker <dalias@aerifal.cx>2015-06-16 07:11:19 +0000
commit1b0cdc8700d29ef018bf226d74b2b58b23bce91c (patch)
tree53c58824a9d73de47296b5a8885aefc9a1f39d0b /src/stdio
parentf22a9edaf8a6f2ca1d314d18b3785558279a5c03 (diff)
downloadmusl-1b0cdc8700d29ef018bf226d74b2b58b23bce91c.tar.gz
musl-1b0cdc8700d29ef018bf226d74b2b58b23bce91c.tar.bz2
musl-1b0cdc8700d29ef018bf226d74b2b58b23bce91c.tar.xz
musl-1b0cdc8700d29ef018bf226d74b2b58b23bce91c.zip
refactor stdio open file list handling, move it out of global libc struct
functions which open in-memory FILE stream variants all shared a tail with __fdopen, adding the FILE structure to stdio's open file list. replacing this common tail with a function call reduces code size and duplication of logic. the list is also partially encapsulated now. function signatures were chosen to facilitate tail call optimization and reduce the need for additional accessor functions. with these changes, static linked programs that do not use stdio no longer have an open file list at all.
Diffstat (limited to 'src/stdio')
-rw-r--r--src/stdio/__fdopen.c8
-rw-r--r--src/stdio/__stdio_exit.c3
-rw-r--r--src/stdio/fclose.c6
-rw-r--r--src/stdio/fflush.c5
-rw-r--r--src/stdio/fmemopen.c8
-rw-r--r--src/stdio/ofl.c16
-rw-r--r--src/stdio/ofl_add.c11
-rw-r--r--src/stdio/open_memstream.c8
-rw-r--r--src/stdio/open_wmemstream.c8
9 files changed, 37 insertions, 36 deletions
diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
index ef8f47dc..8d6ce813 100644
--- a/src/stdio/__fdopen.c
+++ b/src/stdio/__fdopen.c
@@ -54,13 +54,7 @@ FILE *__fdopen(int fd, const char *mode)
if (!libc.threaded) f->lock = -1;
/* Add new FILE to open file list */
- OFLLOCK();
- f->next = libc.ofl_head;
- if (libc.ofl_head) libc.ofl_head->prev = f;
- libc.ofl_head = f;
- OFLUNLOCK();
-
- return f;
+ return __ofl_add(f);
}
weak_alias(__fdopen, fdopen);
diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c
index 716e5f73..191b4454 100644
--- a/src/stdio/__stdio_exit.c
+++ b/src/stdio/__stdio_exit.c
@@ -16,8 +16,7 @@ static void close_file(FILE *f)
void __stdio_exit(void)
{
FILE *f;
- OFLLOCK();
- for (f=libc.ofl_head; f; f=f->next) close_file(f);
+ for (f=*__ofl_lock(); f; f=f->next) close_file(f);
close_file(__stdin_used);
close_file(__stdout_used);
}
diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c
index 317b3c90..839d88af 100644
--- a/src/stdio/fclose.c
+++ b/src/stdio/fclose.c
@@ -14,11 +14,11 @@ int fclose(FILE *f)
__unlist_locked_file(f);
if (!(perm = f->flags & F_PERM)) {
- OFLLOCK();
+ FILE **head = __ofl_lock();
if (f->prev) f->prev->next = f->next;
if (f->next) f->next->prev = f->prev;
- if (libc.ofl_head == f) libc.ofl_head = f->next;
- OFLUNLOCK();
+ if (*head == f) *head = f->next;
+ __ofl_unlock();
}
r = fflush(f);
diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c
index 7bf862a6..3f462c80 100644
--- a/src/stdio/fflush.c
+++ b/src/stdio/fflush.c
@@ -35,13 +35,12 @@ int fflush(FILE *f)
r = __stdout_used ? fflush(__stdout_used) : 0;
- OFLLOCK();
- for (f=libc.ofl_head; f; f=f->next) {
+ for (f=*__ofl_lock(); f; f=f->next) {
FLOCK(f);
if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
FUNLOCK(f);
}
- OFLUNLOCK();
+ __ofl_unlock();
return r;
}
diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c
index d7849609..7c193a57 100644
--- a/src/stdio/fmemopen.c
+++ b/src/stdio/fmemopen.c
@@ -110,11 +110,5 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
if (!libc.threaded) f->lock = -1;
- OFLLOCK();
- f->next = libc.ofl_head;
- if (libc.ofl_head) libc.ofl_head->prev = f;
- libc.ofl_head = f;
- OFLUNLOCK();
-
- return f;
+ return __ofl_add(f);
}
diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c
new file mode 100644
index 00000000..b143999c
--- /dev/null
+++ b/src/stdio/ofl.c
@@ -0,0 +1,16 @@
+#include "stdio_impl.h"
+#include "libc.h"
+
+static FILE *ofl_head;
+static volatile int ofl_lock[2];
+
+FILE **__ofl_lock()
+{
+ LOCK(ofl_lock);
+ return &ofl_head;
+}
+
+void __ofl_unlock()
+{
+ UNLOCK(ofl_lock);
+}
diff --git a/src/stdio/ofl_add.c b/src/stdio/ofl_add.c
new file mode 100644
index 00000000..d7de9f15
--- /dev/null
+++ b/src/stdio/ofl_add.c
@@ -0,0 +1,11 @@
+#include "stdio_impl.h"
+
+FILE *__ofl_add(FILE *f)
+{
+ FILE **head = __ofl_lock();
+ f->next = *head;
+ if (*head) (*head)->prev = f;
+ *head = f;
+ __ofl_unlock();
+ return f;
+}
diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c
index 9eafdfba..58504c9f 100644
--- a/src/stdio/open_memstream.c
+++ b/src/stdio/open_memstream.c
@@ -79,11 +79,5 @@ FILE *open_memstream(char **bufp, size_t *sizep)
if (!libc.threaded) f->lock = -1;
- OFLLOCK();
- f->next = libc.ofl_head;
- if (libc.ofl_head) libc.ofl_head->prev = f;
- libc.ofl_head = f;
- OFLUNLOCK();
-
- return f;
+ return __ofl_add(f);
}
diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c
index 35370309..7ab2c643 100644
--- a/src/stdio/open_wmemstream.c
+++ b/src/stdio/open_wmemstream.c
@@ -81,11 +81,5 @@ FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
if (!libc.threaded) f->lock = -1;
- OFLLOCK();
- f->next = libc.ofl_head;
- if (libc.ofl_head) libc.ofl_head->prev = f;
- libc.ofl_head = f;
- OFLUNLOCK();
-
- return f;
+ return __ofl_add(f);
}