#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcomma"
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/stdio/findfp.c,v 1.34 2009/12/05 19:31:38 ed Exp $");
#include <TargetConditionals.h>
#include <sys/param.h>
#include <machine/atomic.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libkern/OSAtomic.h>
#include <errno.h>
#include <pthread.h>
#include <spinlock.h>
#include "libc_private.h"
#include "local.h"
#include "glue.h"
pthread_once_t __sdidinit;
#if !TARGET_OS_IPHONE
#define NDYNAMIC 10
#else
#define NDYNAMIC 1
#endif
#define std(flags, file) { \
._flags = (flags), \
._file = (file), \
._cookie = __sF + (file), \
._close = __sclose, \
._read = __sread, \
._seek = __sseek, \
._write = __swrite, \
._extra = __sFX + file, \
}
#define __sFXInit3 {.fl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER, .counted = 1}
static int64_t __scounted;
static int64_t __stream_max;
#if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
static FILE usual[FOPEN_MAX - 3];
static struct __sFILEX usual_extra[FOPEN_MAX - 3];
static struct glue uglue = { NULL, FOPEN_MAX - 3, usual };
#endif
static struct __sFILEX __sFX[3] = {__sFXInit3, __sFXInit3, __sFXInit3};
FILE __sF[3] = {
std(__SRD, STDIN_FILENO),
std(__SWR, STDOUT_FILENO),
std(__SWR|__SNBF, STDERR_FILENO)
};
FILE *__stdinp = &__sF[0];
FILE *__stdoutp = &__sF[1];
FILE *__stderrp = &__sF[2];
#if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
struct glue __sglue = { &uglue, 3, __sF };
static struct glue *lastglue = &uglue;
#else
struct glue __sglue = { NULL, 3, __sF };
static struct glue *lastglue = &__sglue;
#endif
static struct glue * moreglue(int);
static pthread_mutex_t filelist_lock = PTHREAD_MUTEX_INITIALIZER;
#define FILELIST_LOCK() do { pthread_mutex_lock(&filelist_lock); } while(0)
#define FILELIST_UNLOCK() do { pthread_mutex_unlock(&filelist_lock); } while(0)
static struct glue *
moreglue(int n)
{
struct glue *g;
FILE *p;
struct __sFILEX *fx;
size_t align;
align = __alignof__( (struct { FILE f; struct __sFILEX s; }){} );
g = (struct glue *)malloc(sizeof(*g) + align + n * sizeof(FILE) +
n * sizeof(struct __sFILEX));
if (g == NULL)
return (NULL);
p = (FILE *)roundup((uintptr_t)(g + 1), align);
fx = (struct __sFILEX *)&p[n];
g->next = NULL;
g->niobs = n;
g->iobs = p;
while (--n >= 0) {
bzero(p, sizeof(*p));
p->_extra = fx;
INITEXTRA(p);
p++, fx++;
}
return (g);
}
FILE *
__sfp(int count)
{
FILE *fp;
int n;
struct glue *g;
pthread_once(&__sdidinit, __sinit);
if (count) {
int64_t new = OSAtomicIncrement64(&__scounted);
if (new > __stream_max) {
if (new > (__stream_max = sysconf(_SC_STREAM_MAX))){
OSAtomicDecrement64(&__scounted);
errno = EMFILE;
return NULL;
}
}
if (new < 0) {
errno = EOVERFLOW;
return NULL;
}
}
FILELIST_LOCK();
for (g = &__sglue; g != NULL; g = g->next) {
for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
if (fp->_flags == 0)
goto found;
}
FILELIST_UNLOCK();
if ((g = moreglue(NDYNAMIC)) == NULL)
return (NULL);
FILELIST_LOCK();
lastglue->next = g;
lastglue = g;
fp = g->iobs;
found:
fp->_flags = 1;
FILELIST_UNLOCK();
fp->_p = NULL;
fp->_w = 0;
fp->_r = 0;
fp->_bf._base = NULL;
fp->_bf._size = 0;
fp->_lbfsize = 0;
fp->_file = -1;
fp->_ub._base = NULL;
fp->_ub._size = 0;
fp->_lb._base = NULL;
fp->_lb._size = 0;
INITEXTRA(fp);
fp->_extra->counted = count ? 1 : 0;
return (fp);
}
__private_extern__ void
__sfprelease(FILE *fp)
{
if (fp->_counted) {
OSAtomicDecrement64(&__scounted);
fp->_counted = 0;
}
pthread_mutex_destroy(&fp->_extra->fl_mutex);
FILELIST_LOCK();
fp->_flags = 0;
FILELIST_UNLOCK();
}
__warn_references(f_prealloc,
"warning: this program uses f_prealloc(), which is not recommended.");
void
f_prealloc(void)
{
struct glue *g;
int n;
n = getdtablesize() - FOPEN_MAX + 20;
for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
;
if ((n > 0) && ((g = moreglue(n)) != NULL)) {
FILELIST_LOCK();
lastglue->next = g;
lastglue = g;
FILELIST_UNLOCK();
}
}
void
_cleanup(void)
{
(void) _fwalk(__sflush);
}
void
__sinit(void)
{
#if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
int i;
#endif
__cleanup = _cleanup;
__stream_max = sysconf(_SC_STREAM_MAX);
__scounted = 3;
#if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
for (i = 0; i < FOPEN_MAX - 3; i++) {
usual[i]._extra = &usual_extra[i];
INITEXTRA(&usual[i]);
}
#endif
}
#pragma clang diagnostic pop