From 2ffc61ba9c43748bcc55b649437d844ca2837901 Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Thu, 15 Feb 2001 22:12:50 +0000 Subject: [PATCH] Remove freelists managed by Balloc/Bfree. Change __dtoa to not free the string it allocated the previous time it was called. The caller now frees the string after usage if appropiate. PR: 15070 Reviewed by: deischen --- lib/libc/stdio/vfprintf.c | 23 +++++++++++++---- lib/libc/stdlib/netbsd_strtod.c | 45 +++++++++------------------------ lib/libc/stdlib/strtod.c | 42 ++++++++---------------------- 3 files changed, 41 insertions(+), 69 deletions(-) diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index ffdf522cae24..49d4971270d2 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -268,7 +268,7 @@ vfprintf(FILE *fp, const char *fmt0, va_list ap) #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ #define DEFPREC 6 -static char *cvt __P((double, int, int, char *, int *, int, int *)); +static char *cvt __P((double, int, int, char *, int *, int, int *, char **)); static int exponent __P((char *, int, int)); #else /* no FLOATING_POINT */ @@ -315,6 +315,7 @@ __vfprintf(FILE *fp, const char *fmt0, va_list ap) int expsize; /* character count for expstr */ int ndig; /* actual number of digits returned by cvt */ char expstr[7]; /* buffer for exponent string */ + char *dtoaresult; /* buffer allocated by dtoa */ #endif u_long ulval; /* integer arguments %[diouxX] */ u_quad_t uqval; /* %q integers */ @@ -423,6 +424,9 @@ __vfprintf(FILE *fp, const char *fmt0, va_list ap) } +#ifdef FLOATING_POINT + dtoaresult = NULL; +#endif /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ if (cantwrite(fp)) return (EOF); @@ -608,8 +612,12 @@ fp_begin: if (prec == -1) break; } flags |= FPT; + if (dtoaresult != NULL) { + free(dtoaresult); + dtoaresult = NULL; + } cp = cvt(_double, prec, flags, &softsign, - &expt, ch, &ndig); + &expt, ch, &ndig, &dtoaresult); if (ch == 'g' || ch == 'G') { if (expt <= -4 || expt > prec) ch = (ch == 'g') ? 'e' : 'E'; @@ -864,6 +872,10 @@ number: if ((dprec = prec) >= 0) done: FLUSH(); error: +#ifdef FLOATING_POINT + if (dtoaresult != NULL) + free(dtoaresult); +#endif if (__sferror(fp)) ret = EOF; if ((argtable != NULL) && (argtable != statargtable)) @@ -1195,11 +1207,11 @@ __grow_type_table (int nextarg, unsigned char **typetable, int *tablesize) #ifdef FLOATING_POINT -extern char *__dtoa __P((double, int, int, int *, int *, char **)); +extern char *__dtoa __P((double, int, int, int *, int *, char **, char **)); static char * cvt(double value, int ndigits, int flags, char *sign, int *decpt, - int ch, int *length) + int ch, int *length, char **dtoaresultp) { int mode, dsgn; char *digits, *bp, *rve; @@ -1221,7 +1233,8 @@ cvt(double value, int ndigits, int flags, char *sign, int *decpt, *sign = '-'; } else *sign = '\000'; - digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); + digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, + dtoaresultp); if ((ch != 'g' && ch != 'G') || flags & ALT) { /* print trailing zeros */ bp = digits + ndigits; diff --git a/lib/libc/stdlib/netbsd_strtod.c b/lib/libc/stdlib/netbsd_strtod.c index a402b688f0cf..b48f8279662c 100644 --- a/lib/libc/stdlib/netbsd_strtod.c +++ b/lib/libc/stdlib/netbsd_strtod.c @@ -142,7 +142,7 @@ __RCSID("$NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp $"); #include "memory.h" #endif #endif -char *__dtoa __P((double, int, int, int *, int *, char **)); +char *__dtoa __P((double, int, int, int *, int *, char **, char **)); #ifdef MALLOC #ifdef KR_headers @@ -382,8 +382,6 @@ Bigint { typedef struct Bigint Bigint; - static Bigint *freelist[Kmax+1]; - static Bigint * Balloc #ifdef KR_headers @@ -395,15 +393,10 @@ Balloc int x; Bigint *rv; - if ((rv = freelist[k]) != NULL) { - freelist[k] = rv->next; - } - else { - x = 1 << k; - rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)); - rv->k = k; - rv->maxwds = x; - } + x = 1 << k; + rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)); + rv->k = k; + rv->maxwds = x; rv->sign = rv->wds = 0; return rv; } @@ -416,10 +409,7 @@ Bfree (Bigint *v) #endif { - if (v) { - v->next = freelist[v->k]; - freelist[v->k] = v; - } + free(v); } #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ @@ -1900,10 +1890,11 @@ quorem char * __dtoa #ifdef KR_headers - (d, mode, ndigits, decpt, sign, rve) - double d; int mode, ndigits, *decpt, *sign; char **rve; + (d, mode, ndigits, decpt, sign, rve, resultp) + double d; int mode, ndigits, *decpt, *sign; char **rve, **resultp; #else - (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) + (double d, int mode, int ndigits, int *decpt, int *sign, char **rve, + char **resultp) #endif { /* Arguments ndigits, decpt, sign are similar to those @@ -1953,15 +1944,6 @@ __dtoa Bigint *mlo = NULL; /* pacify gcc */ double d2, ds, eps; char *s, *s0; - static Bigint *result; - static int result_k; - - if (result) { - result->k = result_k; - result->maxwds = 1 << result_k; - Bfree(result); - result = 0; - } if (word0(d) & Sign_bit) { /* set sign for everything, including 0's and NaNs */ @@ -2123,11 +2105,8 @@ __dtoa if (i <= 0) i = 1; } - j = sizeof(ULong); - for(result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i; - j <<= 1) result_k++; - result = Balloc(result_k); - s = s0 = (char *)result; + *resultp = (char *) malloc(i + 1); + s = s0 = *resultp; if (ilim >= 0 && ilim <= Quick_max && try_quick) { diff --git a/lib/libc/stdlib/strtod.c b/lib/libc/stdlib/strtod.c index 742398e728cb..1ed622c33217 100644 --- a/lib/libc/stdlib/strtod.c +++ b/lib/libc/stdlib/strtod.c @@ -372,8 +372,6 @@ Bigint { typedef struct Bigint Bigint; - static Bigint *freelist[Kmax+1]; - static Bigint * Balloc #ifdef KR_headers @@ -385,14 +383,10 @@ Balloc int x; Bigint *rv; - if ( (rv = freelist[k]) ) { - freelist[k] = rv->next; - } else { - x = 1 << k; - rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long)); - rv->k = k; - rv->maxwds = x; - } + x = 1 << k; + rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long)); + rv->k = k; + rv->maxwds = x; rv->sign = rv->wds = 0; return rv; } @@ -405,10 +399,7 @@ Bfree (Bigint *v) #endif { - if (v) { - v->next = freelist[v->k]; - freelist[v->k] = v; - } + free(v); } #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ @@ -1844,10 +1835,11 @@ quorem char * __dtoa #ifdef KR_headers - (d, mode, ndigits, decpt, sign, rve) - double d; int mode, ndigits, *decpt, *sign; char **rve; + (d, mode, ndigits, decpt, sign, rve, resultp) + double d; int mode, ndigits, *decpt, *sign; char **rve, **resultp; #else - (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) + (double d, int mode, int ndigits, int *decpt, int *sign, char **rve, + char **resultp) #endif { /* Arguments ndigits, decpt, sign are similar to those @@ -1895,15 +1887,6 @@ __dtoa Bigint *b, *b1, *delta, *mlo, *mhi, *S; double d2, ds, eps; char *s, *s0; - static Bigint *result; - static int result_k; - - if (result) { - result->k = result_k; - result->maxwds = 1 << result_k; - Bfree(result); - result = 0; - } if (word0(d) & Sign_bit) { /* set sign for everything, including 0's and NaNs */ @@ -2062,11 +2045,8 @@ __dtoa if (i <= 0) i = 1; } - j = sizeof(unsigned long); - for (result_k = 0; sizeof(Bigint) - sizeof(unsigned long) + j < i; - j <<= 1) result_k++; - result = Balloc(result_k); - s = s0 = (char *)result; + *resultp = (char *) malloc(i + 1); + s = s0 = *resultp; if (ilim >= 0 && ilim <= Quick_max && try_quick) {