From 62b9399cdf7a48bef39ee6024a1ffa02b3c9dd93 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sat, 25 Jan 2014 10:44:50 +0100 Subject: [PATCH] subs: Factor lnd_insque() out of lnd_sel(), ask_olist(), ... ... get_dlist(), att_reacting_units(). This loses malloc() error checking in ask_olist() and get_dlist(). No great loss, because we don't check in so many other places, including att_reacting_units(). We should use a wrapper that terminates on error, though. Left for another day. ask_olist(), get_dlist() and att_reacting_units() zero the struct ulist with memset(). lnd_insque() doesn't, so these functions need to zero any members not otherwise initialized explicitly now. Signed-off-by: Markus Armbruster --- include/land.h | 3 ++- src/lib/subs/attsub.c | 31 ++++++------------------------- src/lib/subs/lndsub.c | 27 ++++++++++++++++++--------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/include/land.h b/include/land.h index 455ba96d9..764bb1f84 100644 --- a/include/land.h +++ b/include/land.h @@ -30,7 +30,7 @@ * Thomas Ruschak, 1992 * Ken Stevens, 1995 * Steve McClure, 1998 - * Markus Armbruster, 2004-2013 + * Markus Armbruster, 2004-2014 */ #ifndef LAND_H @@ -170,6 +170,7 @@ extern double lnd_fire_range(struct lndstr *); extern void lnd_sweep(struct emp_qelem *, int, int, natid); extern int lnd_interdict(struct emp_qelem *, coord, coord, natid); extern void lnd_sel(struct nstr_item *, struct emp_qelem *); +extern struct ulist *lnd_insque(struct lndstr *, struct emp_qelem *); extern int lnd_check_mines(struct emp_qelem *); extern double lnd_pathcost(struct lndstr *, double); extern int lnd_mobtype(struct lndstr *); diff --git a/src/lib/subs/attsub.c b/src/lib/subs/attsub.c index 35f68a515..34f664cc8 100644 --- a/src/lib/subs/attsub.c +++ b/src/lib/subs/attsub.c @@ -29,7 +29,7 @@ * Known contributors to this file: * Ken Stevens, 1995 * Steve McClure, 1996-2000 - * Markus Armbruster, 2006-2013 + * Markus Armbruster, 2006-2014 */ #include @@ -1068,19 +1068,11 @@ ask_olist(int combat_mode, struct combat *off, struct combat *def, land_answer[(int)land.lnd_army] != 'Y') continue; } - if (!(llp = malloc(sizeof(struct ulist)))) { - logerror("Malloc failed in attack!\n"); - abort_attack(); - return; - } - memset(llp, 0, sizeof(struct ulist)); - emp_insque(&llp->queue, olist); + llp = lnd_insque(&land, olist); llp->supplied = 1; llp->mobil = mobcost; - llp->unit.land = land; llp->x = llp->unit.land.lnd_x; llp->y = llp->unit.land.lnd_y; - llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type]; llp->eff = llp->unit.land.lnd_effic; if (lnd_spyval(&land) > *a_spyp) *a_spyp = lnd_spyval(&land); @@ -1199,18 +1191,11 @@ get_dlist(struct combat *def, struct emp_qelem *list, int a_spy, continue; intelligence_report(player->cnum, &land, a_spy, "Scouts report defending unit:"); - if (!(llp = malloc(sizeof(struct ulist)))) { - logerror("Malloc failed in attack!\n"); - abort_attack(); - return; - } - memset(llp, 0, sizeof(struct ulist)); - emp_insque(&llp->queue, list); + llp = lnd_insque(&land, list); llp->supplied = lnd_supply_all(&land); - llp->unit.land = land; + llp->mobil = 0.0; llp->x = llp->unit.land.lnd_x; llp->y = llp->unit.land.lnd_y; - llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type]; llp->eff = llp->unit.land.lnd_effic; if (lnd_spyval(&land) > *d_spyp) *d_spyp = lnd_spyval(&land); @@ -1508,16 +1493,12 @@ att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy, wu(0, land.lnd_own, "%s reacts to %s.\n", prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own)); - llp = malloc(sizeof(struct ulist)); - - memset(llp, 0, sizeof(struct ulist)); + llp = lnd_insque(&land, list); llp->supplied = 1; + llp->mobil = 0.0; llp->x = origx; llp->y = origy; - llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type]; - llp->unit.land = land; llp->eff = land.lnd_effic; - emp_insque(&llp->queue, list); if (lnd_spyval(&land) > *d_spyp) *d_spyp = lnd_spyval(&land); diff --git a/src/lib/subs/lndsub.c b/src/lib/subs/lndsub.c index e789009e0..2aad16d63 100644 --- a/src/lib/subs/lndsub.c +++ b/src/lib/subs/lndsub.c @@ -29,7 +29,7 @@ * Known contributors to this file: * Ken Stevens, 1995 * Steve McClure, 1998-2000 - * Markus Armbruster, 2004-2012 + * Markus Armbruster, 2004-2014 */ #include @@ -396,8 +396,6 @@ void lnd_sel(struct nstr_item *ni, struct emp_qelem *list) { struct lndstr land; - struct lchrstr *lcp; - struct ulist *llp; int this_mot; int mobtype = MOB_MOVE; /* indeterminate */ @@ -436,20 +434,31 @@ lnd_sel(struct nstr_item *ni, struct emp_qelem *list) } } - lcp = &lchr[(int)land.lnd_type]; land.lnd_mission = 0; land.lnd_rflags = 0; land.lnd_harden = 0; memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath)); putland(land.lnd_uid, &land); - llp = malloc(sizeof(struct ulist)); - llp->chrp = (struct empobj_chr *)lcp; - llp->unit.land = land; - llp->mobil = land.lnd_mobil; - emp_insque(&llp->queue, list); + lnd_insque(&land, list); } } +/* + * Append LP to LIST. + * Return the new list link. + */ +struct ulist * +lnd_insque(struct lndstr *lp, struct emp_qelem *list) +{ + struct ulist *mlp = malloc(sizeof(struct ulist)); + + mlp->chrp = (struct empobj_chr *)&lchr[lp->lnd_type]; + mlp->unit.land = *lp; + mlp->mobil = lp->lnd_mobil; + emp_insque(&mlp->queue, list); + return mlp; +} + /* This function assumes that the list was created by lnd_sel */ void lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp, -- 2.43.0