sfstack.c revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/***********************************************************************
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* This software is part of the ast package *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* Copyright (c) 1985-2009 AT&T Intellectual Property *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* and is licensed under the *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* Common Public License, Version 1.0 *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* by AT&T Intellectual Property *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* A copy of the License is available at *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* http://www.opensource.org/licenses/cpl1.0.txt *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov* *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* Information and Software Systems Research *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov* AT&T Research *
5dbfd19ad5fcc2b779f40f80fa05c1bd28fd0b4eTheo Schlossnagle* Florham Park NJ *
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore* *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov* Glenn Fowler <gsf@research.att.com> *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* David Korn <dgk@research.att.com> *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov* Phong Vo <kpv@research.att.com> *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe* *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov***********************************************************************/
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include "sfhdr.h"
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Push/pop streams
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe**
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe** Written by Kiem-Phong Vo.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe*/
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define STKMTXLOCK(f1,f2) \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { if(f1) SFMTXLOCK(f1); \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f2) SFMTXLOCK(f2); \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define STKMTXRETURN(f1,f2,rv) \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { if(f1) SFMTXUNLOCK(f1); \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f2) SFMTXUNLOCK(f2); \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return(rv); \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#if __STD_C
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweSfio_t* sfstack(Sfio_t* f1, Sfio_t* f2)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweSfio_t* sfstack(f1,f2)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweSfio_t* f1; /* base of stack */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweSfio_t* f2; /* top of stack */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe reg int n;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe reg Sfio_t* rf;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe reg Sfrsrv_t* rsrv;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe reg Void_t* mtx;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXLOCK(f1,f2);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f1 && (f1->mode&SF_RDWR) != f1->mode && _sfmode(f1,0,0) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXRETURN(f1,f2, NIL(Sfio_t*));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f2 && (f2->mode&SF_RDWR) != f2->mode && _sfmode(f2,0,0) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXRETURN(f1,f2, NIL(Sfio_t*));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(!f1)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXRETURN(f1,f2, f2);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* give access to other internal functions */
89b9271284be1a4e3e3053d7bc12f9bbf8145b06Robert Mustacchi _Sfstack = sfstack;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f2 == SF_POPSTACK)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { if(!(f2 = f1->push))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXRETURN(f1,f2, NIL(Sfio_t*));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f2->mode &= ~SF_PUSH;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { if(f2->push)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXRETURN(f1,f2, NIL(Sfio_t*));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f1->pool && f1->pool != &_Sfpool && f1->pool != f2->pool &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f1 == f1->pool->sf[0])
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { /* get something else to pool front since f1 will be locked */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for(n = 1; n < f1->pool->n_sf; ++n)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { if(SFFROZEN(f1->pool->sf[n]) )
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe continue;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (*_Sfpmove)(f1->pool->sf[n],0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f2->pool && f2->pool != &_Sfpool && f2 != f2->pool->sf[0])
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (*_Sfpmove)(f2,0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* swap streams */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sfswap(f1,f2);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* but the reserved buffer and mutex must remain the same */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rsrv = f1->rsrv; f1->rsrv = f2->rsrv; f2->rsrv = rsrv;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mtx = f1->mutex; f1->mutex = f2->mutex; f2->mutex = mtx;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SFLOCK(f1,0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SFLOCK(f2,0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if(f2->push != f2)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { /* freeze the pushed stream */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f2->mode |= SF_PUSH;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f1->push = f2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rf = f1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe { /* unfreeze the just exposed stream */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f1->mode &= ~SF_PUSH;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f2->push = NIL(Sfio_t*);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rf = f2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SFOPEN(f1,0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SFOPEN(f2,0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe STKMTXRETURN(f1,f2, rf);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
ce83b99835cc4643ab0fefd88dea62427d9ced5eRobert Mustacchi