Replace the revolting build pointer data structure by a proper data

type.  Make it abstract because that's possible.  Change data layout
so that the slots belonging to a sector are together in memory, it's
nicer to the cache.
(bp): The new type.  Users changed.
(get_wp): Update accordingly.
(alloc_bp): New.
(update_main, calc_all): Use it.  Before, calc_all() allocated 1/7
more than necessary.
This commit is contained in:
Markus Armbruster 2007-01-13 09:07:59 +00:00
parent ac9cdf5bb9
commit 5507e8a1dc
13 changed files with 51 additions and 39 deletions

View file

@ -29,6 +29,7 @@
*
* Known contributors to this file:
* Ville Virrankoski, 1996
* Markus Armbruster, 2007
*/
#include <config.h>
@ -36,18 +37,21 @@
#include "budg.h"
#include "update.h"
struct bp {
int val[7];
};
static int bud_key[I_MAX + 2] =
{ 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 5, 6, 0, 0, 7 };
static int *
get_wp(int *bp, struct sctstr *sp, int cm)
get_wp(struct bp *bp, struct sctstr *sp, int cm)
{
return (bp + (sp->sct_x + (sp->sct_y * WORLD_X)) +
WORLD_X * WORLD_Y * (cm - 1));
return &bp[sp->sct_x + sp->sct_y * WORLD_X].val[cm - 1];
}
int
gt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm)
gt_bg_nmbr(struct bp *bp, struct sctstr *sp, i_type comm)
{
int *wp;
int cm;
@ -61,7 +65,7 @@ gt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm)
}
void
pt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm, int amount)
pt_bg_nmbr(struct bp *bp, struct sctstr *sp, i_type comm, int amount)
{
int *wp;
int cm;
@ -73,7 +77,7 @@ pt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm, int amount)
}
void
fill_update_array(int *bp, struct sctstr *sp)
fill_update_array(struct bp *bp, struct sctstr *sp)
{
int k;
int *wp;
@ -87,3 +91,9 @@ fill_update_array(int *bp, struct sctstr *sp)
wp = get_wp(bp, sp, bud_key[I_MAX + 1]);
*wp = sp->sct_avail;
}
struct bp *
alloc_bp(void)
{
return calloc(WORLD_X * WORLD_Y, sizeof(struct bp));
}

View file

@ -55,7 +55,7 @@ static int babies(int, int, double, int, int);
*/
int
do_feed(struct sctstr *sp, struct natstr *np, short *vec,
int *workp, int *bp, int etu)
int *workp, struct bp *bp, int etu)
{
int work_avail;
int starved, sctwork;

View file

@ -46,12 +46,12 @@
#include "update.h"
#include <math.h>
static void landrepair(struct lndstr *, struct natstr *, int *, int);
static void upd_land(struct lndstr *, int, struct natstr *, int *, int);
static void landrepair(struct lndstr *, struct natstr *, struct bp *, int);
static void upd_land(struct lndstr *, int, struct natstr *, struct bp *, int);
static int feed_land(struct lndstr *, int);
int
prod_land(int etus, int natnum, int *bp, int build)
prod_land(int etus, int natnum, struct bp *bp, int build)
/* build = 1, maintain = 0 */
{
struct lndstr *lp;
@ -104,7 +104,7 @@ prod_land(int etus, int natnum, int *bp, int build)
static void
upd_land(struct lndstr *lp, int etus,
struct natstr *np, int *bp, int build)
struct natstr *np, struct bp *bp, int build)
/* build = 1, maintain = 0 */
{
struct lchrstr *lcp;
@ -214,7 +214,7 @@ upd_land(struct lndstr *lp, int etus,
}
static void
landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
{
int delta;
struct sctstr *sp;

View file

@ -58,7 +58,7 @@ update_main(void *unused)
int etu = etu_per_update;
int n;
int x;
int *bp;
struct bp *bp;
int cn, cn2, rel;
struct natstr *cnp;
struct natstr *np;
@ -92,7 +92,7 @@ update_main(void *unused)
memset(air_money, 0, sizeof(air_money));
memset(sea_money, 0, sizeof(sea_money));
memset(lnd_money, 0, sizeof(lnd_money));
bp = calloc(WORLD_X * WORLD_Y * 7, sizeof(int));
bp = alloc_bp();
for (n = 0; n < MAXNOC; n++) {
money[n] = 0;
if (!(np = getnatp(n)))

View file

@ -48,7 +48,7 @@
* Return adjusted build percentage.
*/
int
get_materials(struct sctstr *sp, int *bp, int *mvec, int pct)
get_materials(struct sctstr *sp, struct bp *bp, int *mvec, int pct)
{
int i, amt;

View file

@ -42,11 +42,11 @@
#include "ship.h"
#include "update.h"
static void planerepair(struct plnstr *, struct natstr *, int *, int);
static void upd_plane(struct plnstr *, int, struct natstr *, int *, int);
static void planerepair(struct plnstr *, struct natstr *, struct bp *, int);
static void upd_plane(struct plnstr *, int, struct natstr *, struct bp *, int);
int
prod_plane(int etus, int natnum, int *bp, int buildem)
prod_plane(int etus, int natnum, struct bp *bp, int buildem)
/* Build = 1, maintain =0 */
{
struct plnstr *pp;
@ -88,7 +88,7 @@ prod_plane(int etus, int natnum, int *bp, int buildem)
static void
upd_plane(struct plnstr *pp, int etus,
struct natstr *np, int *bp, int build)
struct natstr *np, struct bp *bp, int build)
{
struct plchrstr *pcp = &plchr[(int)pp->pln_type];
int mult, cost, eff;
@ -125,7 +125,7 @@ upd_plane(struct plnstr *pp, int etus,
}
static void
planerepair(struct plnstr *pp, struct natstr *np, int *bp, int etus)
planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
{
int build;
int mvec[I_MAX + 1];

View file

@ -44,7 +44,7 @@
#include "update.h"
void
prepare_sects(int etu, int *bp)
prepare_sects(int etu, struct bp *bp)
{
struct sctstr *sp;
struct natstr *np;

View file

@ -257,7 +257,7 @@ decay_fallout(struct sctstr *sp, int etus)
* Produce for a specific nation
*/
void
produce_sect(int natnum, int etu, int *bp, long p_sect[][2])
produce_sect(int natnum, int etu, struct bp *bp, long p_sect[][2])
{
struct sctstr *sp;
struct natstr *np;

View file

@ -47,12 +47,12 @@
#include "ship.h"
#include "update.h"
static void shiprepair(struct shpstr *, struct natstr *, int *, int);
static void upd_ship(struct shpstr *, int, struct natstr *, int *, int);
static void shiprepair(struct shpstr *, struct natstr *, struct bp *, int);
static void upd_ship(struct shpstr *, int, struct natstr *, struct bp *, int);
static int feed_ship(struct shpstr *, int);
int
prod_ship(int etus, int natnum, int *bp, int build)
prod_ship(int etus, int natnum, struct bp *bp, int build)
/* build = 1, maintain = 0 */
{
struct shpstr *sp;
@ -107,7 +107,7 @@ prod_ship(int etus, int natnum, int *bp, int build)
static void
upd_ship(struct shpstr *sp, int etus,
struct natstr *np, int *bp, int build)
struct natstr *np, struct bp *bp, int build)
/* build = 1, maintain = 0 */
{
struct sctstr *sectp;
@ -266,7 +266,7 @@ upd_ship(struct shpstr *sp, int etus,
* 8 * 8 * $40 = $2560!
*/
static void
shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
{
int delta;
struct sctstr *sp;