From d0f65e91f071ebb0fbc6791ee168e2be5ba8e5ae Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 9 Oct 2009 20:54:38 +0100 Subject: [PATCH] routesearch: New macros NEW MCALLOC MCALLOC_INITEACH --- yarrg/rscommon.h | 14 ++++++++++++++ yarrg/rsmain.c | 2 +- yarrg/rssearch.c | 11 +++++------ yarrg/rsvalue.c | 19 +++++++++---------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/yarrg/rscommon.h b/yarrg/rscommon.h index 79f38a4..5cbf5b7 100644 --- a/yarrg/rscommon.h +++ b/yarrg/rscommon.h @@ -123,6 +123,20 @@ extern char **archnames; extern int *islandid2arch; +#define NEW(ptr) ((ptr)= mmalloc(sizeof(*ptr))) + +#define MCALLOC(array, count) ((array)= mcalloc(sizeof(*(array)) * (count))) + +#define MCALLOC_INITEACH(array, count, init_this) ({ \ + MCALLOC((array), (count)); \ + int initi; \ + typeof(&(array)[0]) this; \ + for (initi=0, this=(array); initi<(count); initi++, this++) { \ + init_this; \ + } \ + }) + + #define ONDEMAND(pointer_lvalue, calloc_size_count) \ ((pointer_lvalue) ? : \ ((pointer_lvalue) = mcalloc(sizeof(*(pointer_lvalue)) * calloc_size_count))) diff --git a/yarrg/rsmain.c b/yarrg/rsmain.c index 551018f..bde48a1 100644 --- a/yarrg/rsmain.c +++ b/yarrg/rsmain.c @@ -58,7 +58,7 @@ int main(int argc, const char **argv) { double val= value_route(ni, ia, 0); printf("route value is %g\n", val); } else if (!strcmp(arg,"search")) { - results= mcalloc(sizeof(*results)*argc); + MCALLOC(results, argc); max_dist= atoi(*argv++); int resultsix= 0; while ((arg= *argv++)) { diff --git a/yarrg/rssearch.c b/yarrg/rssearch.c index 0286584..6674c02 100644 --- a/yarrg/rssearch.c +++ b/yarrg/rssearch.c @@ -23,7 +23,8 @@ static Neighbour *get_neighbours(int isle) { SQL_BIND(ss_neigh, 1, isle); while (SQL_STEP(ss_neigh)) { - Neighbour *add= mmalloc(sizeof(*add)); + Neighbour *add; + NEW(add); add->islandid= sqlite3_column_int(ss_neigh, 0); add->dist= sqlite3_column_int(ss_neigh, 1); add->next= head; @@ -152,7 +153,7 @@ char **archnames; int *islandid2arch; void setup_search(void) { - neighbours= mcalloc(sizeof(*neighbours) * islandtablesz); + MCALLOC(neighbours, islandtablesz); SQL_PREPARE(ss_neigh, "SELECT biid, dist FROM routes WHERE aiid=?"); @@ -162,10 +163,8 @@ void setup_search(void) { " SELECT DISTINCT archipelago\n" " FROM islands\n" " )"); - archnames= mcalloc(sizeof(*archnames) * max_narches); - islandid2arch= mmalloc(sizeof(*islandid2arch) * islandtablesz); - int i; - for (i=0; itrades= 0; ip->route_tail_value= -1; @@ -337,7 +337,7 @@ static void read_trades(void) { IslandPair *ip= ipair_get_create(cols[1], cols[3]); TradesBlock *block= ip->trades; if (!block || ip->trades->ntrades >= TRADES_PER_BLOCK) { - block= mmalloc(sizeof(*block)); + NEW(block); block->next= ip->trades; ip->trades= block; block->ntrades= 0; @@ -379,7 +379,7 @@ static void read_islandtradeends(const char *bs, int srcdstoff) { goto found; /* not found, add new end */ - search= mmalloc(sizeof(*search)); + NEW(search); search->commodid= commodid; search->price= price; search->next= *trades; @@ -395,12 +395,11 @@ static void read_islandtradeends(const char *bs, int srcdstoff) { void setup_value(void) { sqlite3_stmt *sst; - int i; commodstabsz= sql_single_int("SELECT max(commodid) FROM commods") + 1; - commodstab= mmalloc(sizeof(*commodstab)*commodstabsz); - for (i=0; imass= this->volu= -1 + ); SQL_PREPARE(sst, "SELECT commodid,unitmass,unitvolume FROM commods"); @@ -413,8 +412,8 @@ void setup_value(void) { } sqlite3_finalize(sst); - ipairs= mcalloc(sizeof(*ipairs) * islandtablesz); - itradeends= mcalloc(sizeof(*itradeends) * islandtablesz); + MCALLOC(ipairs, islandtablesz); + MCALLOC(itradeends, islandtablesz); SQL_PREPARE(ss_ipair_dist, " SELECT dist FROM dists\n" -- 2.30.2