From 35ed981cd578b81397be0159096bbd85a9df55dd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 22 Oct 2018 21:51:11 +0200 Subject: [PATCH] help the VS heap profiler understand the code. reorder calloc arguments. rename ursprung -> origin. --- src/alchemy.c | 4 ++-- src/battle.c | 4 ++-- src/bind_faction.c | 16 ++-------------- src/gmtool.c | 2 +- src/kernel/ally.c | 4 +--- src/kernel/building.c | 2 +- src/kernel/faction.c | 37 ++++++++++++++++++++----------------- src/kernel/faction.h | 10 ++++++++-- src/kernel/faction.test.c | 12 ++++++------ src/kernel/plane.c | 8 ++++---- src/kernel/region.c | 2 +- src/kernel/save.c | 12 ++++++------ src/kernel/types.h | 6 ------ src/listbox.c | 4 ++-- src/modules/autoseed.c | 2 +- src/spells/borders.c | 2 +- src/util/umlaut.c | 2 +- 17 files changed, 59 insertions(+), 70 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index d2dd64a04..c84374e28 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -66,7 +66,7 @@ void new_potiontype(item_type * itype, int level) { potion_type *ptype; - ptype = (potion_type *)calloc(sizeof(potion_type), 1); + ptype = (potion_type *)calloc(1, sizeof(potion_type)); itype->flags |= ITF_POTION; ptype->itype = itype; ptype->level = level; @@ -181,7 +181,7 @@ int use_potion(unit * u, const item_type * itype, int amount, struct order *ord) static void a_initeffect(variant *var) { - var->v = calloc(sizeof(effect_data), 1); + var->v = calloc(1, sizeof(effect_data)); } static void diff --git a/src/battle.c b/src/battle.c index 6ab969c93..8b71f17e1 100644 --- a/src/battle.c +++ b/src/battle.c @@ -3226,7 +3226,7 @@ fighter *make_fighter(battle * b, unit * u, side * s1, bool attack) assert(w != WMAX); } assert(w >= 0); - fig->weapons = (weapon *)calloc(sizeof(weapon), (size_t)(w + 1)); + fig->weapons = (weapon *)calloc((size_t)(w + 1), sizeof(weapon)); memcpy(fig->weapons, weapons, (size_t)w * sizeof(weapon)); for (i = 0; i != w; ++i) { @@ -3452,7 +3452,7 @@ battle *make_battle(region * r) break; } if (!bf) { - bf = (bfaction *)calloc(sizeof(bfaction), 1); + bf = (bfaction *)calloc(1, sizeof(bfaction)); ++b->nfactions; bf->faction = u->faction; bf->next = b->factions; diff --git a/src/bind_faction.c b/src/bind_faction.c index 632fcec3d..bb0920395 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -382,20 +382,8 @@ static int tolua_faction_set_origin(lua_State * L) static int tolua_faction_get_origin(lua_State * L) { faction *self = (faction *)tolua_tousertype(L, 1, 0); - - ursprung *origin = self->ursprung; - int x, y; - while (origin != NULL && origin->id != 0) { - origin = origin->next; - } - if (origin) { - x = origin->x; - y = origin->y; - } - else { - x = 0; - y = 0; - } + int x = 0, y = 0; + faction_getorigin(self, 0, &x, &y); lua_pushinteger(L, x); lua_pushinteger(L, y); diff --git a/src/gmtool.c b/src/gmtool.c index cd0580fcc..36c05a244 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -1361,7 +1361,7 @@ static void update_view(view * vi) state *state_open(void) { - state *st = calloc(sizeof(state), 1); + state *st = (state *)calloc(1, sizeof(state)); st->display.pl = get_homeplane(); st->cursor.pl = get_homeplane(); st->cursor.x = 0; diff --git a/src/kernel/ally.c b/src/kernel/ally.c index 1fd7c0efd..0660f148a 100644 --- a/src/kernel/ally.c +++ b/src/kernel/ally.c @@ -56,10 +56,8 @@ ally * ally_add(ally **al_p, struct faction *f) { if (f && al->faction == f) return al; al_p = &al->next; } - al = (ally *)malloc(sizeof(ally)); + al = (ally *)calloc(1, sizeof(ally)); al->faction = f; - al->status = 0; - al->next = 0; *al_p = al; return al; } diff --git a/src/kernel/building.c b/src/kernel/building.c index f5698eaa9..3a3d7e4e5 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -135,7 +135,7 @@ building_type *bt_get_or_create(const char *name) if (name != NULL) { building_type *btype = bt_find_i(name); if (btype == NULL) { - btype = calloc(sizeof(building_type), 1); + btype = (building_type *)calloc(1, sizeof(building_type)); btype->_name = str_strdup(name); btype->flags = BTF_DEFAULT; btype->auraregen = 1.0; diff --git a/src/kernel/faction.c b/src/kernel/faction.c index a78cd943a..21ea35833 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -119,7 +119,7 @@ static void free_faction(faction * f) i_freeall(&f->items); - freelist(f->ursprung); + freelist(f->origin); } #define FMAXHASH 2039 @@ -727,10 +727,10 @@ bool faction_alive(const faction *f) { void faction_getorigin(const faction * f, int id, int *x, int *y) { - ursprung *ur; + origin *ur; assert(f && x && y); - for (ur = f->ursprung; ur; ur = ur->next) { + for (ur = f->origin; ur; ur = ur->next) { if (ur->id == id) { *x = ur->x; *y = ur->y; @@ -739,24 +739,27 @@ void faction_getorigin(const faction * f, int id, int *x, int *y) } } -void faction_setorigin(faction * f, int id, int x, int y) -{ - ursprung *ur; - assert(f != NULL); - for (ur = f->ursprung; ur; ur = ur->next) { - if (ur->id == id) { - ur->x = ur->x + x; - ur->y = ur->y + y; - return; - } - } - - ur = calloc(1, sizeof(ursprung)); +static origin *new_origin(int id, int x, int y) { + origin *ur = (origin *)calloc(1, sizeof(origin)); ur->id = id; ur->x = x; ur->y = y; + return ur; +} - addlist(&f->ursprung, ur); +void faction_setorigin(faction * f, int id, int x, int y) +{ + origin **urp; + assert(f != NULL); + for (urp = &f->origin; *urp; urp = &(*urp)->next) { + origin *ur = *urp; + if (ur->id == id) { + ur->x += x; + ur->y += y; + return; + } + } + *urp = new_origin(id, x, y); } diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 63c5a9993..0fe38cb1d 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -37,7 +37,7 @@ extern "C" { struct attrib_type; struct gamedata; struct selist; - + /* faction flags */ #define FFL_NOAID (1<<0) /* Hilfsflag Kampf */ #define FFL_ISNEW (1<<1) @@ -54,6 +54,12 @@ extern "C" { #define FFL_NPC (1<<25) /* eine Partei mit Monstern */ #define FFL_SAVEMASK (FFL_DEFENDER|FFL_NPC|FFL_NOIDLEOUT|FFL_CURSED) + typedef struct origin { + struct origin *next; + int id; + int x, y; + } origin; + typedef struct faction { struct faction *next; struct faction *nexthash; @@ -72,7 +78,7 @@ extern "C" { const struct locale *locale; int lastorders; int age; - struct ursprung *ursprung; + struct origin *origin; const struct race *race; magic_t magiegebiet; int newbies; diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index d2931c669..6c2ed3aa6 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -119,7 +119,7 @@ static void test_addfaction(CuTest *tc) { CuAssertPtrEquals(tc, NULL, (void *)f->next); CuAssertPtrEquals(tc, NULL, (void *)f->banner); CuAssertPtrEquals(tc, NULL, (void *)f->spellbook); - CuAssertPtrEquals(tc, NULL, (void *)f->ursprung); + CuAssertPtrEquals(tc, NULL, (void *)f->origin); CuAssertPtrEquals(tc, (void *)factions, (void *)f); CuAssertStrEquals(tc, "test@eressea.de", f->email); CuAssertTrue(tc, checkpasswd(f, "hurrdurr")); @@ -162,11 +162,11 @@ static void test_set_origin(CuTest *tc) { test_setup(); pl = create_new_plane(0, "", 0, 19, 0, 19, 0); f = test_create_faction(NULL); - CuAssertPtrEquals(tc, NULL, f->ursprung); + CuAssertPtrEquals(tc, NULL, f->origin); faction_setorigin(f, 0, 1, 1); - CuAssertIntEquals(tc, 0, f->ursprung->id); - CuAssertIntEquals(tc, 1, f->ursprung->x); - CuAssertIntEquals(tc, 1, f->ursprung->y); + CuAssertIntEquals(tc, 0, f->origin->id); + CuAssertIntEquals(tc, 1, f->origin->x); + CuAssertIntEquals(tc, 1, f->origin->y); faction_getorigin(f, 0, &x, &y); CuAssertIntEquals(tc, 1, x); CuAssertIntEquals(tc, 1, y); @@ -190,7 +190,7 @@ static void test_set_origin_bug(CuTest *tc) { faction_setorigin(f, 0, -10, 3); faction_setorigin(f, 0, -13, -4); adjust_coordinates(f, &x, &y, pl); - CuAssertIntEquals(tc, 0, f->ursprung->id); + CuAssertIntEquals(tc, 0, f->origin->id); CuAssertIntEquals(tc, -9, x); CuAssertIntEquals(tc, 2, y); test_teardown(); diff --git a/src/kernel/plane.c b/src/kernel/plane.c index 71c0dbd86..89de42618 100644 --- a/src/kernel/plane.c +++ b/src/kernel/plane.c @@ -122,7 +122,7 @@ int getplaneid(const region * r) static int ursprung_x(const faction * f, const plane * pl, const region * rdefault) { - ursprung *ur; + origin *ur; int id = 0; if (!f) @@ -131,7 +131,7 @@ ursprung_x(const faction * f, const plane * pl, const region * rdefault) if (pl) id = pl->id; - for (ur = f->ursprung; ur; ur = ur->next) { + for (ur = f->origin; ur; ur = ur->next) { if (ur->id == id) return ur->x; } @@ -145,7 +145,7 @@ ursprung_x(const faction * f, const plane * pl, const region * rdefault) static int ursprung_y(const faction * f, const plane * pl, const region * rdefault) { - ursprung *ur; + origin *ur; int id = 0; if (!f) @@ -154,7 +154,7 @@ ursprung_y(const faction * f, const plane * pl, const region * rdefault) if (pl) id = pl->id; - for (ur = f->ursprung; ur; ur = ur->next) { + for (ur = f->origin; ur; ur = ur->next) { if (ur->id == id) return ur->y; } diff --git a/src/kernel/region.c b/src/kernel/region.c index 377bf1e22..fee51d13c 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -774,7 +774,7 @@ region *new_region(int x, int y, struct plane *pl, int uid) log_error("duplicate region contains units\n"); return r; } - r = calloc(1, sizeof(region)); + r = (region *)calloc(sizeof(region), 1); assert_alloc(r); r->x = x; r->y = y; diff --git a/src/kernel/save.c b/src/kernel/save.c index cdc951fad..a61e4348f 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -357,14 +357,14 @@ static void read_skills(gamedata *data, unit *u) size_t sz = u->skill_size * sizeof(skill); qsort(skills, u->skill_size, sizeof(skill), skill_cmp); - u->skills = malloc(sz); + u->skills = (skill *)malloc(sz); memcpy(u->skills, skills, sz); } } else { int i; READ_INT(data->store, &u->skill_size); - u->skills = malloc(sizeof(skill)*u->skill_size); + u->skills = (skill *)malloc(sizeof(skill)*u->skill_size); for (i = 0; i != u->skill_size; ++i) { skill *sv = u->skills + i; read_skill(data, sv); @@ -420,7 +420,7 @@ unit *read_unit(gamedata *data) u_setfaction(u, NULL); } else { - u = calloc(sizeof(unit), 1); + u = (unit *)calloc(1, sizeof(unit)); assert_alloc(u); u->no = n; uhash(u); @@ -1095,7 +1095,7 @@ faction *read_faction(gamedata * data) void write_faction(gamedata *data, const faction * f) { ally *sf; - ursprung *ur; + origin *ur; assert(f->_alive); assert(f->no > 0 && f->no <= MAX_UNIT_NR); @@ -1134,8 +1134,8 @@ void write_faction(gamedata *data, const faction * f) WRITE_SECTION(data->store); WRITE_TOK(data->store, "end"); WRITE_SECTION(data->store); - WRITE_INT(data->store, listlen(f->ursprung)); - for (ur = f->ursprung; ur; ur = ur->next) { + WRITE_INT(data->store, listlen(f->origin)); + for (ur = f->origin; ur; ur = ur->next) { WRITE_INT(data->store, ur->id); WRITE_INT(data->store, ur->x); WRITE_INT(data->store, ur->y); diff --git a/src/kernel/types.h b/src/kernel/types.h index d4f46ae8d..34c50e0a1 100644 --- a/src/kernel/types.h +++ b/src/kernel/types.h @@ -55,12 +55,6 @@ struct terrain_type; struct unit; struct weapon_type; -typedef struct ursprung { - struct ursprung *next; - int id; - int x, y; -} ursprung; - /* seen_mode: visibility in the report */ typedef enum { seen_none, diff --git a/src/listbox.c b/src/listbox.c index 4aa00adaf..542ab13e8 100644 --- a/src/listbox.c +++ b/src/listbox.c @@ -28,7 +28,7 @@ void insert_selection(list_selection ** p_sel, list_selection * prev, const char *str, void *payload) { - list_selection *sel = calloc(sizeof(list_selection), 1); + list_selection *sel = (list_selection *)calloc(1, sizeof(list_selection)); sel->str = str_strdup(str); sel->data = payload; if (*p_sel) { @@ -56,7 +56,7 @@ const char *str, void *payload) list_selection **push_selection(list_selection ** p_sel, char *str, void *payload) { - list_selection *sel = calloc(sizeof(list_selection), 1); + list_selection *sel = (list_selection *)calloc(1, sizeof(list_selection)); list_selection *prev = NULL; sel->str = str; sel->data = payload; diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index 65585441c..bb31d3f4e 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -133,7 +133,7 @@ newfaction *read_newfactions(const char *filename) if (nf) { continue; } - nf = calloc(sizeof(newfaction), 1); + nf = (newfaction *)calloc(1, sizeof(newfaction)); if (check_email(email) == 0) { nf->email = str_strdup(email); } else { diff --git a/src/spells/borders.c b/src/spells/borders.c index 5d99b1307..c0e51a9b1 100644 --- a/src/spells/borders.c +++ b/src/spells/borders.c @@ -79,7 +79,7 @@ const curse_type ct_firewall = { static void wall_init(connection * b) { - wall_data *fd = (wall_data *)calloc(sizeof(wall_data), 1); + wall_data *fd = (wall_data *)calloc(1, sizeof(wall_data)); fd->countdown = -1; /* infinite */ b->data.v = fd; } diff --git a/src/util/umlaut.c b/src/util/umlaut.c index 1f9e32b1f..00252fb02 100644 --- a/src/util/umlaut.c +++ b/src/util/umlaut.c @@ -117,7 +117,7 @@ char * transliterate(char * out, size_t size, const char * in) } tnode * mknode(void) { - tnode * node = calloc(1, sizeof(tnode)); + tnode * node = (tnode *)calloc(1, sizeof(tnode)); node->refcount = 1; return node; }