diff --git a/src/kernel/building.c b/src/kernel/building.c index 1053a7748..948797b94 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -98,7 +98,7 @@ bool bt_changed(int *cache) return false; } -void bt_register(building_type * btype) +static void bt_register(building_type * btype) { size_t len; char data[64]; diff --git a/src/kernel/building.h b/src/kernel/building.h index edb46edfd..a2019e4a2 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -83,7 +83,6 @@ extern "C" { bool bt_changed(int *cache); const building_type *bt_find(const char *name); void free_buildingtypes(void); - void bt_register(struct building_type *type); int bt_effsize(const struct building_type *btype, const struct building *b, int bsize); diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 3d25cd9a1..39b145d24 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -21,14 +21,13 @@ static void test_register_building(CuTest * tc) test_cleanup(); - btype = (building_type *)calloc(sizeof(building_type), 1); - btype->_name = strdup("herp"); CuAssertIntEquals(tc, true, bt_changed(&cache)); CuAssertIntEquals(tc, false, bt_changed(&cache)); - bt_register(btype); - CuAssertIntEquals(tc, true, bt_changed(&cache)); - CuAssertPtrNotNull(tc, bt_find("herp")); + btype = bt_get_or_create("herp"); + CuAssertIntEquals(tc, true, bt_changed(&cache)); + CuAssertPtrEquals(tc, btype, (void *)bt_find("herp")); + free_buildingtypes(); CuAssertIntEquals(tc, true, bt_changed(&cache)); test_cleanup(); diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 88c0f31e7..27db2e5c2 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -107,21 +107,25 @@ const ship_type *st_find(const char *name) { return st_find_i(name); } +static void st_register(ship_type *stype) { + size_t len; + char data[64]; + + selist_push(&shiptypes, (void *)stype); + + len = cb_new_kv(stype->_name, strlen(stype->_name), &stype, sizeof(stype), data); + assert(len <= sizeof(data)); + cb_insert(&cb_shiptypes, data, len); +} + ship_type *st_get_or_create(const char * name) { ship_type * st = st_find_i(name); assert(!snames); if (!st) { - size_t len; - char data[64]; - st = (ship_type *)calloc(sizeof(ship_type), 1); st->_name = strdup(name); st->storm = 1.0; - selist_push(&shiptypes, (void *)st); - - len = cb_new_kv(name, strlen(name), &st, sizeof(st), data); - assert(len <= sizeof(data)); - cb_insert(&cb_shiptypes, data, len); + st_register(st); } return st; } diff --git a/src/market.test.c b/src/market.test.c index a631dbcb7..2beeaea6b 100644 --- a/src/market.test.c +++ b/src/market.test.c @@ -43,9 +43,7 @@ static void test_market_curse(CuTest * tc) config_set("rules.region_owners", "1"); - btype = (building_type *)calloc(1, sizeof(building_type)); - btype->_name = strdup("market"); - bt_register(btype); + btype = bt_get_or_create("market"); terrain = get_terrain("plain");