]> git.ozlabs.org Git - ccan/blobdiff - ccan/tally/tally.c
tally: fix FreeBSD compile, memleak in tests.
[ccan] / ccan / tally / tally.c
index b3ac863ba68119aedc59973b72c1d3716bb35867..b1839befe3b4016287ded6203767be305e65b657 100644 (file)
@@ -1,4 +1,3 @@
-#include "config.h"
 #include <ccan/tally/tally.h>
 #include <ccan/build_assert/build_assert.h>
 #include <ccan/likely/likely.h>
@@ -7,6 +6,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <assert.h>
+#include <stdlib.h>
 
 #define SIZET_BITS (sizeof(size_t)*CHAR_BIT)
 
@@ -322,7 +322,7 @@ ssize_t tally_total(const struct tally *tally, ssize_t *overflow)
        }
 
        /* If result is negative, make sure we can represent it. */
-       if (tally->total[1] & (1 << (SIZET_BITS-1))) {
+       if (tally->total[1] & ((size_t)1 << (SIZET_BITS-1))) {
                /* Must have only underflowed once, and must be able to
                 * represent result at ssize_t. */
                if ((~tally->total[1])+1 != 0
@@ -455,14 +455,17 @@ char *tally_histogram(const struct tally *tally,
        }
 
        for (i = 0; i < height; i++) {
-               unsigned covered = 1;
-               count = (double)tally->counts[i] / largest_bucket * (width-1)+1;
+               unsigned covered = 1, row;
 
-               if (i == 0)
+               /* People expect minimum at the bottom. */
+               row = height - i - 1;
+               count = (double)tally->counts[row] / largest_bucket * (width-1)+1;
+
+               if (row == 0)
                        covered = snprintf(p, width, "%zi", tally->min);
-               else if (i == height - 1)
+               else if (row == height - 1)
                        covered = snprintf(p, width, "%zi", tally->max);
-               else if (i == bucket_of(tally->min, tally->step_bits, 0))
+               else if (row == bucket_of(tally->min, tally->step_bits, 0))
                        *p = '+';
                else
                        *p = '|';