diff --git a/src/frontend/dvec.c b/src/frontend/dvec.c index d356294f9..54f4efab8 100644 --- a/src/frontend/dvec.c +++ b/src/frontend/dvec.c @@ -40,6 +40,29 @@ dvec_alloc(char *name, int type, short flags, int length, void *storage) } +void +dvec_realloc(struct dvec *v, int length, void *storage) +{ + if (isreal(v)) { + if (storage) { + tfree(v->v_realdata); + v->v_realdata = (double *) storage; + } else { + v->v_realdata = TREALLOC(double, v->v_realdata, length); + } + } else { + if (storage) { + tfree(v->v_compdata); + v->v_compdata = (ngcomplex_t *) storage; + } else { + v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, length); + } + } + + v->v_length = length; +} + + void dvec_trunc(struct dvec *v, int length) { diff --git a/src/frontend/plotting/plotit.c b/src/frontend/plotting/plotit.c index 781df50bc..ca9f9016e 100644 --- a/src/frontend/plotting/plotit.c +++ b/src/frontend/plotting/plotit.c @@ -96,13 +96,7 @@ xtend(struct dvec *v, int length) i = v->v_length; - if (isreal(v)) { - v->v_realdata = TREALLOC(double, v->v_realdata, length); - v->v_length = length; - } else { - v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, length); - v->v_length = length; - } + dvec_realloc(v, length, NULL); if (isreal(v)) { double d = NAN; @@ -138,15 +132,12 @@ compress(struct dvec *d, double *xcomp, double *xind) if (isreal(d)) { double *dd = TMALLOC(double, newlen); bcopy(d->v_realdata + ilo, dd, (size_t) newlen * sizeof(double)); - tfree(d->v_realdata); - d->v_realdata = dd; + dvec_realloc(d, newlen, dd); } else { ngcomplex_t *cc = TMALLOC(ngcomplex_t, newlen); bcopy(d->v_compdata + ilo, cc, (size_t) newlen * sizeof(ngcomplex_t)); - tfree(d->v_compdata); - d->v_compdata = cc; + dvec_realloc(d, newlen, cc); } - d->v_length = newlen; } } @@ -938,9 +929,7 @@ plotit(wordlist *wl, char *hcopy, char *devname) goto quit; } - tfree(v->v_realdata); - v->v_realdata = newdata; - v->v_length = newlen; + dvec_realloc(v, newlen, newdata); /* Why go to all this trouble if agraf ignores it? */ nointerp = TRUE; diff --git a/src/frontend/vectors.c b/src/frontend/vectors.c index 09d3b0b62..2431c86bf 100644 --- a/src/frontend/vectors.c +++ b/src/frontend/vectors.c @@ -576,8 +576,7 @@ vec_get(const char *vec_name) for (nv = vv->va_vlist; nv; nv = nv->va_next) i++; - d->v_realdata = TREALLOC(double, d->v_realdata, i); - d->v_length = i; + dvec_realloc(d, i, NULL); i = 0; for (nv = vv->va_vlist; nv; nv = nv->va_next) @@ -1054,8 +1053,7 @@ vec_transpose(struct dvec *v) } koffset += blocksize; /* koffset = k*blocksize = k*dim0*dim1 */ } - tfree(oldreal); - v->v_realdata = newreal; + dvec_realloc(v, v->v_length, newreal); } else { newcomp = TMALLOC(ngcomplex_t, v->v_length); oldcomp = v->v_compdata; @@ -1073,8 +1071,7 @@ vec_transpose(struct dvec *v) } koffset += blocksize; /* koffset = k*blocksize = k*dim0*dim1 */ } - tfree(oldcomp); - v->v_compdata = newcomp; + dvec_realloc(v, v->v_length, newcomp); } } diff --git a/src/include/ngspice/dvec.h b/src/include/ngspice/dvec.h index 2c4446d9e..eb5a9a9ae 100644 --- a/src/include/ngspice/dvec.h +++ b/src/include/ngspice/dvec.h @@ -69,6 +69,7 @@ struct dveclist { }; struct dvec *dvec_alloc(char *name, int type, short flags, int length, void *storage); +void dvec_realloc(struct dvec *v, int length, void *storage); void dvec_trunc(struct dvec *v, int length); void dvec_free(struct dvec *); diff --git a/src/ngsconvert.c b/src/ngsconvert.c index 3065e4f10..a8e7d2647 100644 --- a/src/ngsconvert.c +++ b/src/ngsconvert.c @@ -174,12 +174,7 @@ oldread(char *name) np = i / nv; for (v = pl->pl_dvecs; v; v = v->v_next) { - v->v_length = (int) np; - if (isreal(v)) { - v->v_realdata = TMALLOC(double, np); - } else { - v->v_compdata = TMALLOC(ngcomplex_t, np); - } + dvec_realloc(v, (int) np, NULL); } for (i = 0; i < np; i++) { /* Read in the output vector for point i. If the type is