From: Matthew Fernandez Date: Mon, 24 Oct 2022 00:40:38 +0000 (-0700) Subject: sfio: remove 'proc' data X-Git-Tag: 7.0.2~19^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=99ba8a134d5ce3205effbdfd323cc560d49932fe;p=graphviz sfio: remove 'proc' data The last non-trivial call to `_sfpopen` was removed in f552c0dece1435773de62334dee3310d1728e22f. Since then, `proc` members have been allocated and propagated around with data describing the absence of a subprocess (`pid == -1`). We can reduce memory overhead and decrease code complexity by removing these semantically dead code paths. --- diff --git a/lib/sfio/sfclose.c b/lib/sfio/sfclose.c index fb91274c6..09b9202b1 100644 --- a/lib/sfio/sfclose.c +++ b/lib/sfio/sfclose.c @@ -110,10 +110,6 @@ int sfclose(Sfio_t * f) free(f->rsrv); f->rsrv = NULL; - /* delete any associated sfpopen-data */ - if (f->proc) - rv = _sfpclose(f); - if (!local) { if (f->disc && (ex = SFRAISE(f, SF_FINAL, NULL)) != 0) { rv = ex; diff --git a/lib/sfio/sfdisc.c b/lib/sfio/sfdisc.c index 442525f97..0eb5d1bbe 100644 --- a/lib/sfio/sfdisc.c +++ b/lib/sfio/sfdisc.c @@ -26,10 +26,7 @@ Sfdisc_t *sfdisc(Sfio_t * f, Sfdisc_t * disc) SFMTXSTART(f, NULL); - if ((f->flags & SF_READ) && f->proc && (f->mode & SF_WRITE)) { /* make sure in read mode to check for read-ahead data */ - if (_sfmode(f, SF_READ, 0) < 0) - SFMTXRETURN(f, NULL); - } else if ((f->mode & SF_RDWR) != f->mode && _sfmode(f, 0, 0) < 0) + if ((f->mode & SF_RDWR) != f->mode && _sfmode(f, 0, 0) < 0) SFMTXRETURN(f, NULL); SFLOCK(f, 0); diff --git a/lib/sfio/sfhdr.h b/lib/sfio/sfhdr.h index 0ca76f3f0..7a732f8b3 100644 --- a/lib/sfio/sfhdr.h +++ b/lib/sfio/sfhdr.h @@ -192,17 +192,6 @@ extern "C" { uchar data[1]; /* data buffer */ }; -/* co-process structure */ - typedef struct _sfproc_s Sfproc_t; - struct _sfproc_s { - int pid; /* process id */ - uchar *rdata; /* read data being cached */ - int ndata; /* size of cached data */ - int size; /* buffer size */ - int file; /* saved file descriptor */ - int sigp; /* sigpipe protection needed */ - }; - /* extensions to sfvprintf/sfvscanf */ #define FP_SET(fp,fn) (fp < 0 ? (fn += 1) : (fn = fp) ) #define FP_WIDTH 0 @@ -541,8 +530,6 @@ extern "C" { extern Sfextern_t _Sfextern; extern Sftab_t _Sftable; - extern int _sfpopen(Sfio_t *, int, int); - extern int _sfpclose(Sfio_t *); extern int _sfmode(Sfio_t *, int, int); extern int _sfexcept(Sfio_t *, int, ssize_t, Sfdisc_t *); extern Sfrsrv_t *_sfrsrv(Sfio_t *, ssize_t); diff --git a/lib/sfio/sfio_t.h b/lib/sfio/sfio_t.h index 312f34b37..3e0a914b3 100644 --- a/lib/sfio/sfio_t.h +++ b/lib/sfio/sfio_t.h @@ -33,7 +33,6 @@ extern "C" { struct _sfdisc_s* disc; /* discipline */ \ struct _sfpool_s* pool; /* the pool containing this */ \ struct _sfrsrv_s* rsrv; /* reserved buffer */ \ - struct _sfproc_s* proc; /* coprocess id, etc. */ \ Sfoff_t lpos; /* last seek position */ \ size_t iosz; /* prefer size for I/O */ @@ -69,7 +68,6 @@ extern "C" { (struct _sfdisc_s*)(disc), /* disc */ \ (struct _sfpool_s*)0, /* pool */ \ (struct _sfrsrv_s*)0, /* rsrv */ \ - (struct _sfproc_s*)0, /* proc */ \ (Sfoff_t)0, /* lpos */ \ (size_t)0 /* iosz */ \ } @@ -95,7 +93,6 @@ extern "C" { (f)->disc = (struct _sfdisc_s*)0, /* disc */ \ (f)->pool = (struct _sfpool_s*)0, /* pool */ \ (f)->rsrv = (struct _sfrsrv_s*)0, /* rsrv */ \ - (f)->proc = (struct _sfproc_s*)0, /* proc */ \ (f)->lpos = (Sfoff_t)0, /* lpos */ \ (f)->iosz = (size_t)0 /* iosz */ \ ) diff --git a/lib/sfio/sfmode.c b/lib/sfio/sfmode.c index 1b0facc5d..939b4436f 100644 --- a/lib/sfio/sfmode.c +++ b/lib/sfio/sfmode.c @@ -11,9 +11,6 @@ #include #include #include -#ifndef _WIN32 -#include -#endif /* Functions to set a given stream to some desired mode ** @@ -34,13 +31,6 @@ ** 02/01/2001 (adaptive buffering) */ -/* the below is for protecting the application from SIGPIPE */ -#include -typedef void (*Sfsignal_f) (int); -#ifdef SIGPIPE -static int _Sfsigp = 0; /* # of streams needing SIGPIPE protection */ -#endif - /* done at exiting time */ static void _sfcleanup(void) { @@ -156,122 +146,6 @@ Sfrsrv_t *_sfrsrv(Sfio_t * f, ssize_t size) return size >= 0 ? rsrv : NULL; } -/** - * @param f - * @param fd - * @param pid - */ -int _sfpopen(Sfio_t *f, int fd, int pid) { - Sfproc_t *p; - - if (f->proc) - return 0; - - if (!(p = f->proc = malloc(sizeof(Sfproc_t)))) - return -1; - - p->pid = pid; - p->size = p->ndata = 0; - p->rdata = NULL; - p->file = fd; - p->sigp = (pid >= 0 && (f->flags & SF_WRITE)) ? 1 : 0; - -#ifdef SIGPIPE /* protect from broken pipe signal */ - if (p->sigp) { - Sfsignal_f handler; - - if ((handler = signal(SIGPIPE, SIG_IGN)) != SIG_DFL && handler != SIG_IGN) - signal(SIGPIPE, handler); /* honor user handler */ - _Sfsigp += 1; - } -#endif - - return 0; -} - -/** - * @param f stream to close - */ -int _sfpclose(Sfio_t * f) -{ - Sfproc_t *p; - int pid = 0, status; - - if (!(p = f->proc)) - return -1; - f->proc = NULL; - - free(p->rdata); - - if (p->pid < 0) - status = 0; - else { /* close the associated stream */ - if (p->file >= 0) - CLOSE(p->file); - - /* wait for process termination */ -#ifndef _WIN32 - while ((pid = waitpid(p->pid, &status, 0)) == -1 - && errno == EINTR); -#endif - if (pid < 0) - status = -1; - -#ifdef SIGPIPE - if (p->sigp && (_Sfsigp -= 1) <= 0) { - Sfsignal_f handler; - if ((handler = signal(SIGPIPE, SIG_DFL)) != SIG_DFL && handler != SIG_IGN) - signal(SIGPIPE, handler); /* honor user handler */ - _Sfsigp = 0; - } -#endif - } - - free(p); - return status; -} - -static int _sfpmode(Sfio_t * f, int type) -{ - Sfproc_t *p; - - if (!(p = f->proc)) - return -1; - - if (type == SF_WRITE) { /* save unread data */ - p->ndata = f->endb - f->next; - if (p->ndata > p->size) { - free(p->rdata); - if ((p->rdata = malloc(p->ndata))) - p->size = p->ndata; - else { - p->size = 0; - return -1; - } - } - if (p->ndata > 0) - memcpy(p->rdata, f->next, p->ndata); - f->endb = f->data; - } else { /* restore read data */ - if (p->ndata > f->size) /* may lose data!!! */ - p->ndata = f->size; - if (p->ndata > 0) { - memcpy(f->data, p->rdata, p->ndata); - f->endb = f->data + p->ndata; - p->ndata = 0; - } - } - - /* switch file descriptor */ - if (p->pid >= 0) { - type = f->file; - f->file = p->file; - p->file = type; - } - - return 0; -} - /** * @param f change r/w mode and sync file pointer for this stream * @param wanted desired mode @@ -390,10 +264,6 @@ int _sfmode(Sfio_t * f, int wanted, int local) f->next = f->endr = f->endw = f->endb = f->data; f->mode = SF_READ | SF_LOCK; - /* restore saved read data for coprocess */ - if (f->proc && _sfpmode(f, wanted) < 0) - goto err_notify; - break; case (SF_READ | SF_SYNCED): /* a previously sync-ed read stream */ @@ -428,10 +298,6 @@ int _sfmode(Sfio_t * f, int wanted, int local) break; } - /* save unread data before switching mode */ - if (f->proc && _sfpmode(f, wanted) < 0) - goto err_notify; - /* reset buffer and seek pointer */ if (!(f->mode & SF_SYNCED)) { intptr_t n = (intptr_t)(f->endb - f->next); diff --git a/lib/sfio/sfsetbuf.c b/lib/sfio/sfsetbuf.c index 2e219b9f2..7569a2fb0 100644 --- a/lib/sfio/sfsetbuf.c +++ b/lib/sfio/sfsetbuf.c @@ -84,11 +84,6 @@ void *sfsetbuf(Sfio_t * f, void * buf, size_t size) else { int rv; - /* make sure there is no hidden read data */ - if (f->proc && (f->flags & SF_READ) && (f->mode & SF_WRITE) && - _sfmode(f, SF_READ, local) < 0) - SFMTXRETURN(f, NULL); - /* synchronize first */ SFLOCK(f, local); rv = SFSYNC(f); @@ -189,10 +184,6 @@ void *sfsetbuf(Sfio_t * f, void * buf, size_t size) #endif errno = oerrno; } - - /* initialize side buffer for r+w unseekable streams */ - if (!f->proc && (f->bits & SF_BOTH)) - (void) _sfpopen(f, -1, -1); } } diff --git a/lib/sfio/sfwrite.c b/lib/sfio/sfwrite.c index 3749193c4..73614431c 100644 --- a/lib/sfio/sfwrite.c +++ b/lib/sfio/sfwrite.c @@ -61,9 +61,6 @@ ssize_t sfwrite(Sfio_t * f, const void * buf, size_t n) f->endb = f->data + n; f->here += n; } - - if ((f->mode & SF_READ) && f->proc) - f->next += n; } s = begs = (uchar *) buf;