CID 22529 Resource leak

github issue #351
allocation call optimization for small units
This commit is contained in:
Enno Rehling 2015-11-05 11:22:16 +01:00
parent 530f729621
commit fce3f63cc1
1 changed files with 10 additions and 2 deletions

View File

@ -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;
}