diff --git a/clibs b/clibs index 12139c7ce..ed5c4fee3 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit 12139c7ce25e6731393f863e248ab16aa0578a3a +Subproject commit ed5c4fee3afbc3d8be79d64857f30702aed522f8 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/build.c b/src/kernel/build.c index 71d5543fd..2b7251f1d 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -54,16 +54,17 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* from libutil */ -#include #include #include #include #include #include #include +#include /* from libc */ #include +#include #include #include #include @@ -180,8 +181,9 @@ int destroy_cmd(unit * u, struct order *ord) s = gettoken(token, sizeof(token)); if (s && *s) { - n = atoi10((const char *)s); - + ERRNO_CHECK(); + n = atoi(s); + errno = 0; if (n <= 0) { n = INT_MAX; } 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/item.c b/src/kernel/item.c index aab734759..58702be07 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -723,7 +723,7 @@ int change_money(unit * u, int v) return 0; } -static int add_resourcename_cb(const void * match, const void * key, +static int add_resourcename_cb(void * match, const void * key, size_t keylen, void *data) { struct locale * lang = (struct locale *)data; @@ -773,7 +773,7 @@ attrib_type at_showitem = { "showitem" }; -static int add_itemname_cb(const void * match, const void * key, +static int add_itemname_cb(void * match, const void * key, size_t keylen, void *data) { struct locale * lang = (struct locale *)data; @@ -919,7 +919,7 @@ void free_rtype(resource_type *rtype) { free(rtype); } -static int free_rtype_cb(const void * match, const void * key, +static int free_rtype_cb(void * match, const void * key, size_t keylen, void *cbdata) { resource_type *rtype = ((rt_entry *)match)->value;; 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/magic.c b/src/magic.c index 99a9ecf86..342eb0831 100644 --- a/src/magic.c +++ b/src/magic.c @@ -2950,7 +2950,7 @@ void free_spellbook(spellbook *sb) { free(sb); } -static int free_spellbook_cb(const void *match, const void *key, size_t keylen, void *data) { +static int free_spellbook_cb(void *match, const void *key, size_t keylen, void *data) { const sb_entry *ent = (const sb_entry *)match; UNUSED_ARG(data); UNUSED_ARG(keylen); diff --git a/src/main.c b/src/main.c index 5bb4ad4ee..118f30afd 100644 --- a/src/main.c +++ b/src/main.c @@ -335,6 +335,8 @@ int main(int argc, char **argv) game_done(); lua_done(L); log_close(); + stats_write(stdout, ""); + stats_close(); if (d) { iniparser_freedict(d); } 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/monsters.c b/src/monsters.c index 42ab8938a..9f17db54d 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -608,6 +608,7 @@ static void recruit_dracoids(unit * dragon, int size) region *r = dragon->region; const struct item *weapon = NULL; unit *un = create_unit(r, f, size, get_race(RC_DRACOID), 0, NULL, NULL); + stats_count("monsters.create.dracoid", 1); fset(un, UFL_ISNEW | UFL_MOVED); @@ -868,6 +869,7 @@ static int nrand(int handle_start, int sub) unit *spawn_seaserpent(region *r, faction *f) { unit *u = create_unit(r, f, 1, get_race(RC_SEASERPENT), 0, NULL, NULL); + stats_count("monsters.create.seaserpent", 1); fset(u, UFL_ISNEW | UFL_MOVED); equip_unit(u, "seed_seaserpent"); return u; @@ -908,6 +910,7 @@ void spawn_dragons(void) else { u = create_unit(r, monsters, nrand(30, 20) + 1, get_race(RC_DRAGON), 0, NULL, NULL); } + stats_count("monsters.create.dragon", 1); fset(u, UFL_ISNEW | UFL_MOVED); equip_unit(u, "seed_dragon"); @@ -940,7 +943,7 @@ void spawn_undead(void) } if (r->land && unburied > rpeasants(r) / 20 - && rng_int() % 10000 < 200) { + && rng_int() % 10000 < 100) { message *msg; unit *u; /* es ist sinnfrei, wenn irgendwo im Wald 3er-Einheiten Untote entstehen. @@ -968,6 +971,7 @@ void spawn_undead(void) } u = create_unit(r, monsters, undead, rc, 0, NULL, NULL); + stats_count("monsters.create.undead", 1); fset(u, UFL_ISNEW | UFL_MOVED); if ((rc == get_race(RC_SKELETON) || rc == get_race(RC_ZOMBIE)) && rng_int() % 10 < 4) { 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/teleport.c b/src/teleport.c index 36634c5cd..2d8fafe72 100644 --- a/src/teleport.c +++ b/src/teleport.c @@ -182,6 +182,7 @@ void spawn_braineaters(float chance) u = create_unit(r, f, 1 + rng_int() % 10 + rng_int() % 10, rc_brain, 0, NULL, NULL); equip_unit(u, "seed_braineater"); + stats_count("monsters.create.braineater", 1); next = rng_int() % (int)(chance * 100); } diff --git a/src/test_eressea.c b/src/test_eressea.c index 9897c4bf0..2b36d4e03 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -175,6 +175,8 @@ int RunAllTests(int argc, char *argv[]) fail_count = summary->failCount; CuSuiteDelete(summary); game_done(); + log_close(); + stats_close(); return fail_count; } return 0; diff --git a/src/tests.c b/src/tests.c index cad296e57..3a8c9e095 100644 --- a/src/tests.c +++ b/src/tests.c @@ -234,6 +234,8 @@ static void test_reset(void) { default_locale = 0; calendar_cleanup(); close_orders(); + log_close(); + stats_close(); free_special_directions(); free_locales(); free_spells(); diff --git a/src/util/base36.c b/src/util/base36.c index e68fd76f6..3b5543e88 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -23,14 +23,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include - -int atoi10(const char *str) -{ - int i = atoi(str); - errno = 0; - return i; -} int atoi36(const char *str) { diff --git a/src/util/crypto/crypt_blowfish/crypt_blowfish.c b/src/util/crypto/crypt_blowfish/crypt_blowfish.c index b0b11d3ad..701ddddf0 100644 --- a/src/util/crypto/crypt_blowfish/crypt_blowfish.c +++ b/src/util/crypto/crypt_blowfish/crypt_blowfish.c @@ -53,10 +53,7 @@ /* Just to make sure the prototypes match the actual definitions */ #include "crypt_blowfish.h" -#ifdef __i386__ -#define BF_ASM 1 -#define BF_SCALE 1 -#elif defined(__x86_64__) || defined(__alpha__) || defined(__hppa__) +#if defined(__x86_64__) || defined(__alpha__) || defined(__hppa__) #define BF_ASM 0 #define BF_SCALE 1 #else diff --git a/src/util/log.c b/src/util/log.c index b8549d60e..fd50ec700 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -16,6 +16,8 @@ without prior permission by the authors of Eressea. #include "strings.h" #include "unicode.h" +#include + #include #include #include @@ -318,3 +320,77 @@ int log_level(log_t * log, int flags) log->flags = flags; return old; } + +static critbit_tree stats = CRITBIT_TREE(); + +int stats_count(const char *stat, int delta) { + size_t len; + char data[128]; + void * match; + if (cb_find_prefix_str(&stats, stat, &match, 1, 0) == 0) { + len = cb_new_kv(stat, strlen(stat), &delta, sizeof(delta), data); + cb_insert(&stats, data, len); + return delta; + } + else { + int *num; + cb_get_kv_ex(match, (void **)&num); + return *num += delta; + } +} + +#if 0 +#define STATS_BATCH 8 +void stats_walk(const char *prefix, void(*callback)(const char *, int, void *), void *udata) { + void *match[STATS_BATCH]; + int n, off = 0; + do { + int i; + n = cb_find_prefix_str(&stats, prefix, match, STATS_BATCH, off); + if (n == 0) { + break; + } + off += n; + for (i = 0; i != n; ++i) { + const void *kv = match[i]; + int *num; + cb_get_kv_ex(kv, &(void *)num); + callback(kv, *num, udata); + } + } while (n == STATS_BATCH); +} +#else + +struct walk_data { + int (*callback)(const char *, int, void *); + void *udata; +}; + +static int walk_cb(void * match, const void * key, size_t keylen, void *udata) { + struct walk_data *data = (struct walk_data *)udata; + int *num; + cb_get_kv_ex(match, (void **)&num); + return data->callback((const char*)match, *num, data->udata); +} + +int stats_walk(const char *prefix, int (*callback)(const char *, int, void *), void *udata) { + struct walk_data data; + data.callback = callback; + data.udata = udata; + return cb_foreach(&stats, prefix, strlen(prefix), walk_cb, &data); +} +#endif + +static int write_cb(const char *key, int val, void *udata) { + FILE * F = (FILE *)udata; + fprintf(F, "%s: %d\n", (const char *)key, val); + return 0; +} + +void stats_write(FILE *F, const char *prefix) { + stats_walk(prefix, write_cb, F); +} + +void stats_close(void) { + cb_clear(&stats); +} diff --git a/src/util/log.h b/src/util/log.h index 15872d6dc..98a0d3af6 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -36,6 +36,12 @@ extern "C" { void log_printf(FILE * ios, const char *format, ...); void errno_check(const char *file, int line); + + int stats_count(const char *stat, int delta); + void stats_write(FILE *F, const char *prefix); + int stats_walk(const char *prefix, int (*callback)(const char *key, int val, void * udata), void *udata); + void stats_close(void); + #define ERRNO_CHECK() errno_check(__FILE__, __LINE__) diff --git a/src/util/log.test.c b/src/util/log.test.c index 86f13ea9c..928ba5660 100644 --- a/src/util/log.test.c +++ b/src/util/log.test.c @@ -1,11 +1,13 @@ #ifdef _MSC_VER #include #endif -#include #include "log.h" #include "macros.h" +#include +#include + #include #include @@ -32,9 +34,47 @@ static void test_logging(CuTest * tc) CuAssertStrEquals(tc, "World", str2); } +static int stats_cb(const char *stat, int num, void *udata) { + int *counter = (int *)udata; + if (counter) { + *counter += num; + } + return 0; +} + +static void test_stats(CuTest * tc) +{ + int n = 0; + test_setup(); + CuAssertIntEquals(tc, 1, stats_count("foobar", 1)); + CuAssertIntEquals(tc, 2, stats_count("test.one", 2)); + CuAssertIntEquals(tc, 1, stats_count("test.two", 1)); + CuAssertIntEquals(tc, 4, stats_count("test.one", 2)); + CuAssertIntEquals(tc, 1, stats_count("test.two", 0)); + + n = 0; + CuAssertIntEquals(tc, 0, stats_walk("", stats_cb, &n)); + CuAssertIntEquals(tc, 6, n); + + n = 0; + CuAssertIntEquals(tc, 0, stats_walk("test", stats_cb, &n)); + CuAssertIntEquals(tc, 5, n); + + n = 0; + CuAssertIntEquals(tc, 0, stats_walk("test.one", stats_cb, &n)); + CuAssertIntEquals(tc, 4, n); + + n = 0; + CuAssertIntEquals(tc, 0, stats_walk("foobar", stats_cb, &n)); + CuAssertIntEquals(tc, 1, n); + + test_teardown(); +} + CuSuite *get_log_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_logging); + SUITE_ADD_TEST(suite, test_stats); return suite; } diff --git a/src/util/strings.c b/src/util/strings.c index ef351ab1f..e9434fb25 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -22,10 +22,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "strings.h" /* libc includes */ +#include #include #include #include -#include #include #ifdef HAVE_LIBBSD diff --git a/src/util/strings.h b/src/util/strings.h index bd6f0de58..bd58c0eb2 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #define STRINGS_H #include +#include #ifdef __cplusplus extern "C" { 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; }