1 --- findfp.c.orig 2011-02-15 16:29:38.000000000 -0800
2 +++ findfp.c 2011-02-16 10:36:00.000000000 -0800
3 @@ -36,13 +36,18 @@ static char sccsid[] = "@(#)findfp.c 8.2
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/findfp.c,v 1.34 2009/12/05 19:31:38 ed Exp $");
7 +#include <TargetConditionals.h>
10 #include <machine/atomic.h>
15 +#include <libkern/OSAtomic.h>
21 #include "libc_private.h"
22 @@ -51,7 +56,11 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/f
26 +#if !TARGET_OS_EMBEDDED
27 #define NDYNAMIC 10 /* add ten more whenever necessary */
29 +#define NDYNAMIC 1 /* add one at a time on embedded */
32 #define std(flags, file) { \
34 @@ -61,12 +70,32 @@ int __sdidinit;
38 + ._extra = __sFX + file, \
40 +#define __sFXInit {.fl_mutex = PTHREAD_MUTEX_INITIALIZER}
42 +#define __sFXInit3 {.fl_mutex = PTHREAD_MUTEX_INITIALIZER, .counted = 1}
44 +static int __scounted; /* streams counted against STREAM_MAX */
45 +static int __stream_max;
47 +#if !TARGET_OS_EMBEDDED
48 +/* usual and usual_extra are data pigs. See 7929728. For embedded we should
49 + * always allocate dynamically, and probably should for desktop too. */
50 /* the usual - (stdin + stdout + stderr) */
51 static FILE usual[FOPEN_MAX - 3];
52 +static struct __sFILEX usual_extra[FOPEN_MAX - 3];
53 static struct glue uglue = { NULL, FOPEN_MAX - 3, usual };
54 +#endif /* !TARGET_OS_EMBEDDED */
56 -static FILE __sF[3] = {
57 +static struct __sFILEX __sFX[3] = {__sFXInit3, __sFXInit3, __sFXInit3};
60 + * We can't make this 'static' due to binary compatibility concerns.
61 + * This also means we cannot change the sizeof(FILE) and must continue to
62 + * use the __sFILEX stuff to add to FILE.
65 std(__SRD, STDIN_FILENO),
66 std(__SWR, STDOUT_FILENO),
67 std(__SWR|__SNBF, STDERR_FILENO)
68 @@ -76,8 +105,13 @@ FILE *__stdinp = &__sF[0];
69 FILE *__stdoutp = &__sF[1];
70 FILE *__stderrp = &__sF[2];
72 +#if !TARGET_OS_EMBEDDED
73 struct glue __sglue = { &uglue, 3, __sF };
74 static struct glue *lastglue = &uglue;
76 +struct glue __sglue = { NULL, 3, __sF };
77 +static struct glue *lastglue = &__sglue;
80 static struct glue * moreglue(int);
82 @@ -98,16 +132,25 @@ moreglue(n)
86 + static struct __sFILEX emptyx = __sFXInit;
87 + struct __sFILEX *fx;
89 - g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
90 + g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) +
91 + n * sizeof(struct __sFILEX));
94 p = (FILE *)ALIGN(g + 1);
95 + fx = (struct __sFILEX *)&p[n];
105 + *p->_extra = emptyx;
111 @@ -115,7 +158,7 @@ moreglue(n)
112 * Find a free FILE for fopen et al.
120 @@ -123,6 +166,15 @@ __sfp()
126 + if (__scounted >= __stream_max) {
131 + OSAtomicIncrement32(&__scounted);
134 * The list must be locked because a FILE may be updated.
136 @@ -155,12 +207,25 @@ found:
137 fp->_lb._base = NULL; /* no line buffer */
139 /* fp->_lock = NULL; */ /* once set always set (reused) */
140 - fp->_orientation = 0;
141 - memset(&fp->_mbstate, 0, sizeof(mbstate_t));
143 + fp->_extra->counted = count ? 1 : 0;
148 + * Mark as free and update count as needed
150 +__private_extern__ void
151 +__sfprelease(FILE *fp)
153 + if (fp->_counted) {
154 + OSAtomicDecrement32(&__scounted);
161 * XXX. Force immediate allocation of internal memory. Not used by stdio,
162 * but documented historically for certain applications. Bad applications.
164 @@ -209,8 +274,25 @@ _cleanup()
169 + if (__sdidinit == 0) {
170 +#if !TARGET_OS_EMBEDDED
173 + /* Make sure we clean up on exit. */
174 + __cleanup = _cleanup; /* conservative */
175 + __stream_max = sysconf(_SC_STREAM_MAX);
176 + __scounted = 3; /* std{in,out,err} already exists */
178 +#if !TARGET_OS_EMBEDDED
179 + /* Set _extra for the usual suspects. */
180 + for (i = 0; i < FOPEN_MAX - 3; i++) {
181 + usual[i]._extra = &usual_extra[i];
182 + INITEXTRA(&usual[i]);
186 - /* Make sure we clean up on exit. */
187 - __cleanup = _cleanup; /* conservative */