From ea43507bedb22fc8844411e431e059f90a535476 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Fri, 24 Aug 2018 03:02:15 +0300 Subject: [PATCH] precalculate_sizes: don't malloc needlessly --- src/render.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/render.c b/src/render.c index 8699660e..dafccbb1 100644 --- a/src/render.c +++ b/src/render.c @@ -183,26 +183,28 @@ free_params: } static int *precalculate_sizes(Con *con, render_params *p) { + if ((con->layout != L_SPLITH && con->layout != L_SPLITV) || p->children <= 0) { + return NULL; + } + int *sizes = smalloc(p->children * sizeof(int)); - if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && p->children > 0) { - assert(!TAILQ_EMPTY(&con->nodes_head)); + assert(!TAILQ_EMPTY(&con->nodes_head)); - Con *child; - int i = 0, assigned = 0; - int total = con_orientation(con) == HORIZ ? p->rect.width : p->rect.height; - TAILQ_FOREACH(child, &(con->nodes_head), nodes) { - double percentage = child->percent > 0.0 ? child->percent : 1.0 / p->children; - assigned += sizes[i++] = lround(percentage * total); - } - assert(assigned == total || - (assigned > total && assigned - total <= p->children * 2) || - (assigned < total && total - assigned <= p->children * 2)); - int signal = assigned < total ? 1 : -1; - while (assigned != total) { - for (i = 0; i < p->children && assigned != total; ++i) { - sizes[i] += signal; - assigned += signal; - } + Con *child; + int i = 0, assigned = 0; + int total = con_orientation(con) == HORIZ ? p->rect.width : p->rect.height; + TAILQ_FOREACH(child, &(con->nodes_head), nodes) { + double percentage = child->percent > 0.0 ? child->percent : 1.0 / p->children; + assigned += sizes[i++] = lround(percentage * total); + } + assert(assigned == total || + (assigned > total && assigned - total <= p->children * 2) || + (assigned < total && total - assigned <= p->children * 2)); + int signal = assigned < total ? 1 : -1; + while (assigned != total) { + for (i = 0; i < p->children && assigned != total; ++i) { + sizes[i] += signal; + assigned += signal; } } -- 2.39.5