sfreserve.c revision 7c2fbfb345896881c631598ee3852ce9ce33fb07
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* Copyright (c) 1985-2008 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* http://www.opensource.org/licenses/cpl1.0.txt *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "sfhdr.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Reserve a segment of data or buffer.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin**
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
7c2fbfb345896881c631598ee3852ce9ce33fb07April ChinVoid_t* sfreserve(Sfio_t* f, ssize_t size, int type)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVoid_t* sfreserve(f,size,type)
7c2fbfb345896881c631598ee3852ce9ce33fb07April ChinSfio_t* f; /* file to peek */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinssize_t size; /* size of peek */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint type; /* LOCKR: lock stream, LASTR: last record */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg ssize_t n, now, sz, iosz;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg Sfrsrv_t* rsrv;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg Void_t* data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg int mode, local;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin SFMTXDECL(f);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin SFMTXENTER(f,NIL(Void_t*));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sz = size < 0 ? -size : size;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* see if we need to bias toward SF_WRITE instead of the default SF_READ */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(type < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mode = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if((mode = type&SF_WRITE) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin type &= ~SF_WRITE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* return the last record */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(type == SF_LASTR )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((n = f->endb - f->next) > 0 && n == f->val )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { data = (Void_t*)f->next;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->next += n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if((rsrv = f->rsrv) && (n = -rsrv->slen) > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { rsrv->slen = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin _Sfi = f->val = n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)rsrv->data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { _Sfi = f->val = -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = NIL(Void_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f, data);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(type > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(type == 1 ) /* upward compatibility mode */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin type = SF_LOCKR;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type != SF_LOCKR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f, NIL(Void_t*));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(size == 0 && (type < 0 || type == SF_LOCKR) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f, NIL(Void_t*));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFLOCK(f,0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((n = f->endb - f->next) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin goto done;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* iterate until get to a stream that has data or buffer space */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for(local = 0;; local = SF_LOCAL)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { _Sfi = f->val = -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!mode && !(mode = f->flags&SF_READ) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mode = SF_WRITE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((int)f->mode != mode && _sfmode(f,mode,local) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { SFOPEN(f,0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f, NIL(Void_t*));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFLOCK(f,local);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((n = now = f->endb - f->next) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(n > 0 && n >= sz) /* all done */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin /* set amount to perform IO */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if(size == 0 || (f->mode&SF_WRITE))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin iosz = -1;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin else if(size < 0 && n == 0 && f->push) /* maybe stack-pop */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin iosz = sz; /* so only get what is asked for */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin { iosz = sz - n; /* get enough to fulfill requirement */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if(size < 0 && iosz < (f->size - n) )
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin iosz = f->size - n; /* get as much as possible */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if(iosz <= 0) /* nothing to do */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* do a buffer refill or flush */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin now = n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(f->mode&SF_WRITE)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void)SFFLSBUF(f, iosz);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type == SF_LOCKR && f->extent < 0 && (f->flags&SF_SHARE) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(n == 0) /* peek-read only if there is no buffered data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { f->mode |= SF_RV;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void)SFFILBUF(f, iosz );
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((n = f->endb - f->next) < sz)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(f->mode&SF_PKRD)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { f->endb = f->endr = f->next;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->mode &= ~SF_PKRD;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin else
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin { /* sfreserve(f,0,0) == sfread(f, sfreserve(f,-1,SF_LOCKR), 0) */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if(size == 0 && type == 0)
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin f->mode |= SF_RV;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin (void)SFFILBUF(f, iosz );
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((n = f->endb - f->next) <= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(n >= sz) /* got it */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(n == now || sferror(f) || sfeof(f)) /* no progress */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* request was only to assess data availability */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(type == SF_LOCKR && size > 0 && n > 0 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone: /* compute the buffer to be returned */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = NIL(Void_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(size == 0 || n == 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(n > 0) /* got data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)f->next;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type == SF_LOCKR && size == 0 && (rsrv = _sfrsrv(f,0)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)rsrv->data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(n >= sz) /* got data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)f->next;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(f->flags&SF_STRING) /* try extending string buffer */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((f->mode&SF_WRITE) && (f->flags&SF_MALLOC) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { (void)SFWR(f,f->next,sz,f->disc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((n = f->endb - f->next) >= sz )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)f->next;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(f->mode&SF_WRITE) /* allocate side buffer */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(type == SF_LOCKR && (rsrv = _sfrsrv(f, sz)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)rsrv->data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type != SF_LOCKR && sz > f->size && (rsrv = _sfrsrv(f,sz)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((n = SFREAD(f,(Void_t*)rsrv->data,sz)) >= sz) /* read side buffer */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = (Void_t*)rsrv->data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else rsrv->slen = -n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFOPEN(f,0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(data)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(type == SF_LOCKR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { f->mode |= SF_PEEK;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((f->mode & SF_READ) && size == 0 && data != f->next)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->mode |= SF_GETR; /* so sfread() will unlock */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->endr = f->endw = f->data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(data == (Void_t*)f->next)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->next += (size >= 0 ? size : n);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin _Sfi = f->val = n; /* return true buffer size */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f, data);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}