X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftally%2Ftally.c;h=d67112c2cf6da49017904be9ec91b49f1f11ced5;hp=396474b250e0c04be0b8a581a2fddc1bb553c7a4;hb=237fc67d26def3f4458594b4aff7cb617f4522f7;hpb=da8558a174da778aeb9dd95e62e023d39349b948 diff --git a/ccan/tally/tally.c b/ccan/tally/tally.c index 396474b2..d67112c2 100644 --- a/ccan/tally/tally.c +++ b/ccan/tally/tally.c @@ -1,3 +1,4 @@ +/* Licensed under LGPLv3+ - see LICENSE file for details */ #include #include #include @@ -24,21 +25,27 @@ struct tally *tally_new(unsigned buckets) struct tally *tally; /* There is always 1 bucket. */ - if (buckets == 0) + if (buckets == 0) { buckets = 1; + } /* Overly cautious check for overflow. */ - if (sizeof(*tally) * buckets / sizeof(*tally) != buckets) + if (sizeof(*tally) * buckets / sizeof(*tally) != buckets) { return NULL; - tally = malloc(sizeof(*tally) + sizeof(tally->counts[0])*(buckets-1)); - if (tally) { - tally->max = ((size_t)1 << (SIZET_BITS - 1)); - tally->min = ~tally->max; - tally->total[0] = tally->total[1] = 0; - tally->buckets = buckets; - tally->step_bits = 0; - memset(tally->counts, 0, sizeof(tally->counts[0])*buckets); } + + tally = (struct tally *)malloc( + sizeof(*tally) + sizeof(tally->counts[0])*(buckets-1)); + if (tally == NULL) { + return NULL; + } + + tally->max = ((size_t)1 << (SIZET_BITS - 1)); + tally->min = ~tally->max; + tally->total[0] = tally->total[1] = 0; + tally->buckets = buckets; + tally->step_bits = 0; + memset(tally->counts, 0, sizeof(tally->counts[0])*buckets); return tally; } @@ -448,7 +455,7 @@ char *tally_histogram(const struct tally *tally, largest_bucket = tally->counts[i]; } - p = graph = malloc(height * (width + 1) + 1); + p = graph = (char *)malloc(height * (width + 1) + 1); if (!graph) { free(tmp); return NULL;