diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-04-29 01:45:27 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-04-29 01:45:27 -0400 |
commit | 9b51d50c2f55ec71a800a48f2955567695c9d26d (patch) | |
tree | 41d426a923f8a114a59892c3e8ddb2660c96e316 | |
parent | 1aa24e20241710020926a271609811ef96dea6c6 (diff) | |
download | postgresql-9b51d50c2f55ec71a800a48f2955567695c9d26d.tar.gz postgresql-9b51d50c2f55ec71a800a48f2955567695c9d26d.zip |
Rewrite pg_size_pretty() to avoid compiler bug.
Convert it to use successive shifts right instead of increasing a divisor.
This is probably a tad more efficient than the original coding, and it's
nicer-looking than the previous patch because we don't need a special case
to avoid overflow in the last branch. But the real reason to do it is to
avoid a Solaris compiler bug, as per results from buildfarm member moa.
-rw-r--r-- | src/backend/utils/adt/dbsize.c | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index 66221766f24..c1507a11e1d 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -381,39 +381,33 @@ pg_size_pretty(PG_FUNCTION_ARGS) int64 size = PG_GETARG_INT64(0); char *result = palloc(50 + VARHDRSZ); int64 limit = 10 * 1024; - int64 mult = 1; + int64 limit2 = limit * 2 - 1; - if (size < limit * mult) + if (size < limit) snprintf(VARDATA(result), 50, INT64_FORMAT " bytes", size); else { - mult *= 1024; - if (size < limit * mult) + size >>= 9; /* keep one extra bit for rounding */ + if (size < limit2) snprintf(VARDATA(result), 50, INT64_FORMAT " kB", - (size + mult / 2) / mult); + (size + 1) / 2); else { - mult *= 1024; - if (size < limit * mult) + size >>= 10; + if (size < limit2) snprintf(VARDATA(result), 50, INT64_FORMAT " MB", - (size + mult / 2) / mult); + (size + 1) / 2); else { - mult *= 1024; - if (size < limit * mult) + size >>= 10; + if (size < limit2) snprintf(VARDATA(result), 50, INT64_FORMAT " GB", - (size + mult / 2) / mult); + (size + 1) / 2); else { - /* Here we have to worry about avoiding overflow */ - int64 val; - - mult *= 1024; - val = size / mult; - if ((size % mult) >= (mult / 2)) - val++; + size >>= 10; snprintf(VARDATA(result), 50, INT64_FORMAT " TB", - val); + (size + 1) / 2); } } } |