]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/findfp.c.patch
Libc-763.11.tar.gz
[apple/libc.git] / stdio / FreeBSD / findfp.c.patch
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
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/findfp.c,v 1.34 2009/12/05 19:31:38 ed Exp $");
6
7 +#include <TargetConditionals.h>
8 +
9 #include <sys/param.h>
10 #include <machine/atomic.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 +#include <libkern/OSAtomic.h>
16 +#include <errno.h>
17
18 +#include <pthread.h>
19 #include <spinlock.h>
20
21 #include "libc_private.h"
22 @@ -51,7 +56,11 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/f
23
24 int __sdidinit;
25
26 +#if !TARGET_OS_EMBEDDED
27 #define NDYNAMIC 10 /* add ten more whenever necessary */
28 +#else
29 +#define NDYNAMIC 1 /* add one at a time on embedded */
30 +#endif
31
32 #define std(flags, file) { \
33 ._flags = (flags), \
34 @@ -61,12 +70,32 @@ int __sdidinit;
35 ._read = __sread, \
36 ._seek = __sseek, \
37 ._write = __swrite, \
38 + ._extra = __sFX + file, \
39 }
40 +#define __sFXInit {.fl_mutex = PTHREAD_MUTEX_INITIALIZER}
41 + /* set counted */
42 +#define __sFXInit3 {.fl_mutex = PTHREAD_MUTEX_INITIALIZER, .counted = 1}
43 +
44 +static int __scounted; /* streams counted against STREAM_MAX */
45 +static int __stream_max;
46 +
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 */
55
56 -static FILE __sF[3] = {
57 +static struct __sFILEX __sFX[3] = {__sFXInit3, __sFXInit3, __sFXInit3};
58 +
59 +/*
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.
63 + */
64 +FILE __sF[3] = {
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];
71
72 +#if !TARGET_OS_EMBEDDED
73 struct glue __sglue = { &uglue, 3, __sF };
74 static struct glue *lastglue = &uglue;
75 +#else
76 +struct glue __sglue = { NULL, 3, __sF };
77 +static struct glue *lastglue = &__sglue;
78 +#endif
79
80 static struct glue * moreglue(int);
81
82 @@ -98,16 +132,25 @@ moreglue(n)
83 struct glue *g;
84 static FILE empty;
85 FILE *p;
86 + static struct __sFILEX emptyx = __sFXInit;
87 + struct __sFILEX *fx;
88
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));
92 if (g == NULL)
93 return (NULL);
94 p = (FILE *)ALIGN(g + 1);
95 + fx = (struct __sFILEX *)&p[n];
96 g->next = NULL;
97 g->niobs = n;
98 g->iobs = p;
99 - while (--n >= 0)
100 - *p++ = empty;
101 +
102 + while (--n >= 0) {
103 + *p = empty;
104 + p->_extra = fx;
105 + *p->_extra = emptyx;
106 + p++, fx++;
107 + }
108 return (g);
109 }
110
111 @@ -115,7 +158,7 @@ moreglue(n)
112 * Find a free FILE for fopen et al.
113 */
114 FILE *
115 -__sfp()
116 +__sfp(int count)
117 {
118 FILE *fp;
119 int n;
120 @@ -123,6 +166,15 @@ __sfp()
121
122 if (!__sdidinit)
123 __sinit();
124 +
125 + if (count) {
126 + if (__scounted >= __stream_max) {
127 + THREAD_UNLOCK();
128 + errno = EMFILE;
129 + return NULL;
130 + }
131 + OSAtomicIncrement32(&__scounted);
132 + }
133 /*
134 * The list must be locked because a FILE may be updated.
135 */
136 @@ -155,12 +207,25 @@ found:
137 fp->_lb._base = NULL; /* no line buffer */
138 fp->_lb._size = 0;
139 /* fp->_lock = NULL; */ /* once set always set (reused) */
140 - fp->_orientation = 0;
141 - memset(&fp->_mbstate, 0, sizeof(mbstate_t));
142 + INITEXTRA(fp);
143 + fp->_extra->counted = count ? 1 : 0;
144 return (fp);
145 }
146
147 /*
148 + * Mark as free and update count as needed
149 + */
150 +__private_extern__ void
151 +__sfprelease(FILE *fp)
152 +{
153 + if (fp->_counted) {
154 + OSAtomicDecrement32(&__scounted);
155 + fp->_counted = 0;
156 + }
157 + fp->_flags = 0;
158 +}
159 +
160 +/*
161 * XXX. Force immediate allocation of internal memory. Not used by stdio,
162 * but documented historically for certain applications. Bad applications.
163 */
164 @@ -209,8 +274,25 @@ _cleanup()
165 void
166 __sinit()
167 {
168 + THREAD_LOCK();
169 + if (__sdidinit == 0) {
170 +#if !TARGET_OS_EMBEDDED
171 + int i;
172 +#endif
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 */
177 +
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]);
183 + }
184 +#endif
185
186 - /* Make sure we clean up on exit. */
187 - __cleanup = _cleanup; /* conservative */
188 - __sdidinit = 1;
189 + __sdidinit = 1;
190 + }
191 + THREAD_UNLOCK();
192 }