forked from github/server
CID 22529 Resource leak
github issue #351 allocation call optimization for small units
This commit is contained in:
parent
530f729621
commit
fce3f63cc1
|
@ -449,7 +449,7 @@ static int nb_armor(const unit * u, int index)
|
||||||
static int
|
static int
|
||||||
damage_unit(unit * u, const char *dam, bool physical, bool magic)
|
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 h;
|
||||||
int i, dead = 0, hp_rem = 0, heiltrank;
|
int i, dead = 0, hp_rem = 0, heiltrank;
|
||||||
double magres = magic_resistance(u);
|
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);
|
assert(u->number <= u->hp);
|
||||||
h = u->hp / u->number;
|
h = u->hp / u->number;
|
||||||
/* HP verteilen */
|
/* HP verteilen */
|
||||||
|
if (u->number < 20) {
|
||||||
|
hp = hpstack;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hp = malloc(u->number * sizeof(int));
|
||||||
|
}
|
||||||
for (i = 0; i < u->number; i++)
|
for (i = 0; i < u->number; i++)
|
||||||
hp[i] = h;
|
hp[i] = h;
|
||||||
h = u->hp - (u->number * 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);
|
scale_number(u, u->number - dead);
|
||||||
u->hp = hp_rem;
|
u->hp = hp_rem;
|
||||||
|
|
||||||
free(hp);
|
if (hp != hpstack) {
|
||||||
|
free(hp);
|
||||||
|
}
|
||||||
|
|
||||||
return dead;
|
return dead;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue