Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libstdio/findfp.c

  • Property svn:executable set to *
1/*-
2 * Copyright (c) 1990, 1993
3 *The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)findfp.c8.2 (Berkeley) 1/4/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37//__FBSDID("$FreeBSD: src/lib/libc/stdio/findfp.c,v 1.34 2009/12/05 19:31:38 ed Exp $");
38
39#include <TargetConditionals.h>
40#include "libsaio.h"
41
42#include <sys/param.h>
43#include "stdio.h"
44#include "errno.h"
45#include "glue.h"
46
47
48#include "local.h"
49
50int__sdidinit;
51void(*__cleanup)(void);
52
53#define _PTHREAD_MUTEX_SIG_init0x32AAABA7
54#define PTHREAD_MUTEX_INITIALIZER {_PTHREAD_MUTEX_SIG_init, {0}}
55
56
57
58#if !TARGET_OS_EMBEDDED
59#defineNDYNAMIC 10/* add ten more whenever necessary */
60#else
61#defineNDYNAMIC 1/* add one at a time on embedded */
62#endif
63
64#definestd(flags, file) {\
65._flags = (flags),\
66._file = (file),\
67._cookie = __sF + (file),\
68._close = __sclose,\
69._read = __sread,\
70._seek = __sseek,\
71._write = __swrite,\
72._extra = __sFX + file, \
73}
74#define __sFXInit {.fl_mutex = PTHREAD_MUTEX_INITIALIZER}
75 /* set counted */
76#define __sFXInit3 {.fl_mutex = PTHREAD_MUTEX_INITIALIZER, .counted = 1}
77
78//static int __scounted;/* streams counted against STREAM_MAX */
79//static int __stream_max;
80
81#if !TARGET_OS_EMBEDDED
82/* usual and usual_extra are data pigs. See 7929728. For embedded we should
83 * always allocate dynamically, and probably should for desktop too. */
84/* the usual - (stdin + stdout + stderr) */
85static FILE usual[FOPEN_MAX - 3];
86static struct __sFILEX usual_extra[FOPEN_MAX - 3];
87static struct glue uglue = { NULL, FOPEN_MAX - 3, usual };
88#endif /* !TARGET_OS_EMBEDDED */
89
90static struct __sFILEX __sFX[3] = {__sFXInit3, __sFXInit3, __sFXInit3};
91
92/*
93 * We can't make this 'static' due to binary compatibility concerns.
94 * This also means we cannot change the sizeof(FILE) and must continue to
95 * use the __sFILEX stuff to add to FILE.
96 */
97FILE __sF[3] = {
98std(__SRD, STDIN_FILENO),
99std(__SWR, STDOUT_FILENO),
100std(__SWR|__SNBF, STDERR_FILENO)
101};
102
103FILE *__stdinp = &__sF[0];
104FILE *__stdoutp = &__sF[1];
105FILE *__stderrp = &__sF[2];
106
107#if !TARGET_OS_EMBEDDED
108struct glue __sglue = { &uglue, 3, __sF };
109static struct glue *lastglue = &uglue;
110#else
111struct glue __sglue = { NULL, 3, __sF };
112static struct glue *lastglue = &__sglue;
113#endif
114
115static struct glue *moreglue(int);
116
117
118#if NOT_YET
119#defineSET_GLUE_PTR(ptr, val)atomic_set_rel_ptr(&(ptr), (uintptr_t)(val))
120#else
121#defineSET_GLUE_PTR(ptr, val)ptr = val
122#endif
123
124static struct glue *
125moreglue(n)
126int n;
127{
128struct glue *g;
129static FILE empty;
130FILE *p;
131static struct __sFILEX emptyx = __sFXInit;
132struct __sFILEX *fx;
133
134g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) +
135 n * sizeof(struct __sFILEX));
136if (g == NULL)
137return (NULL);
138p = (FILE *)ALIGN(g + 1);
139fx = (struct __sFILEX *)&p[n];
140g->next = NULL;
141g->niobs = n;
142g->iobs = p;
143
144while (--n >= 0) {
145*p = empty;
146p->_extra = fx;
147*p->_extra = emptyx;
148p++, fx++;
149}
150return (g);
151}
152
153/*
154 * Find a free FILE for fopen et al.
155 */
156FILE *
157__sfp(int count)
158{
159FILE*fp;
160intn;
161struct glue *g;
162
163if (!__sdidinit)
164__sinit();
165
166/*if (count) {
167if (__scounted >= __stream_max) {
168 set_errno(EMFILE);
169return NULL;
170}
171//OSAtomicIncrement32(&__scounted);
172 __scounted++;
173}*/
174/*
175 * The list must be locked because a FILE may be updated.
176 */
177for (g = &__sglue; g != NULL; g = g->next) {
178for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
179if (fp->_flags == 0)
180goto found;
181}
182if ((g = moreglue(NDYNAMIC)) == NULL)
183return (NULL);
184SET_GLUE_PTR(lastglue->next, g); /* atomically append glue to list */
185lastglue = g;/* not atomic; only accessed when locked */
186fp = g->iobs;
187found:
188fp->_flags = 1;/* reserve this slot; caller sets real flags */
189fp->_p = NULL;/* no current pointer */
190fp->_w = 0;/* nothing to read or write */
191fp->_r = 0;
192fp->_bf._base = NULL;/* no buffer */
193fp->_bf._size = 0;
194fp->_lbfsize = 0;/* not line buffered */
195fp->_file = -1;/* no file */
196/*fp->_cookie = <any>; *//* caller sets cookie, _read/_write etc */
197fp->_ub._base = NULL;/* no ungetc buffer */
198fp->_ub._size = 0;
199fp->_lb._base = NULL;/* no line buffer */
200fp->_lb._size = 0;
201/*fp->_lock = NULL; *//* once set always set (reused) */
202INITEXTRA(fp);
203fp->_extra->counted = count ? 1 : 0;
204return (fp);
205}
206
207/*
208 * Mark as free and update count as needed
209 */
210__private_extern__ void
211__sfprelease(FILE *fp)
212{
213if (fp->_counted) {
214//OSAtomicDecrement32(&__scounted);
215 //__scounted--;
216fp->_counted = 0;
217}
218fp->_flags = 0;
219}
220
221/*
222 * exit() calls _cleanup() through *__cleanup, set whenever we
223 * open or buffer a file. This chicanery is done so that programs
224 * that do not use stdio need not link it all in.
225 *
226 * The name `_cleanup' is, alas, fairly well known outside stdio.
227 */
228
229void
230_cleanup()
231{
232/* (void) _fwalk(fclose); */
233 (void) _fwalk(__sflush);/* `cheating' */
234}
235
236/*
237 * __sinit() is called whenever stdio's internal variables must be set up.
238 */
239void
240__sinit()
241{
242if (__sdidinit == 0) {
243#if !TARGET_OS_EMBEDDED
244int i;
245#endif
246/* Make sure we clean up on exit. */
247__cleanup = _cleanup;/* conservative */
248//__stream_max = 9;
249//__scounted = 3;/* std{in,out,err} already exists */
250
251#if !TARGET_OS_EMBEDDED
252/* Set _extra for the usual suspects. */
253for (i = 0; i < FOPEN_MAX - 3; i++) {
254usual[i]._extra = &usual_extra[i];
255INITEXTRA(&usual[i]);
256}
257#endif
258
259__sdidinit = 1;
260}
261}
262

Archive Download this file

Revision: 2154