From fce3f63cc156ead17e996c3aa2708e6c5a0ee7cd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 11:22:16 +0100 Subject: [PATCH] CID 22529 Resource leak github issue #351 allocation call optimization for small units --- src/randenc.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/randenc.c b/src/randenc.c index aeb623cb9..55802ff2f 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -449,7 +449,7 @@ static int nb_armor(const unit * u, int index) static int damage_unit(unit * u, const char *dam, bool physical, bool magic) { - int *hp = malloc(u->number * sizeof(int)); + int *hp, hpstack[20]; int h; int i, dead = 0, hp_rem = 0, heiltrank; double magres = magic_resistance(u); @@ -462,6 +462,12 @@ damage_unit(unit * u, const char *dam, bool physical, bool magic) assert(u->number <= u->hp); h = u->hp / u->number; /* HP verteilen */ + if (u->number < 20) { + hp = hpstack; + } + else { + hp = malloc(u->number * sizeof(int)); + } for (i = 0; i < u->number; i++) hp[i] = h; h = u->hp - (u->number * h); @@ -517,7 +523,9 @@ damage_unit(unit * u, const char *dam, bool physical, bool magic) scale_number(u, u->number - dead); u->hp = hp_rem; - free(hp); + if (hp != hpstack) { + free(hp); + } return dead; }