From cef068fe9bf3e19a5135e647ce00d8c26c84a8cd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 16 Aug 2014 23:23:37 +0200 Subject: [PATCH] cut the number of allocations for order-data in half. started writing a test file for orders. --- src/kernel/CMakeLists.txt | 1 + src/kernel/order.c | 57 +++++++++++++++++++-------------------- src/kernel/order.test.c | 48 +++++++++++++++++++++++++++++++++ src/test_eressea.c | 31 +-------------------- 4 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 src/kernel/order.test.c diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 8b97848bf..ac524bfbc 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -14,6 +14,7 @@ equipment.test.c curse.test.c item.test.c move.test.c +order.test.c pool.test.c reports.test.c spellbook.test.c diff --git a/src/kernel/order.c b/src/kernel/order.c index c10a256fd..5550ca9eb 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -2,7 +2,7 @@ +-------------------+ | | Christian Schlittchen | Eressea PBEM host | Enno Rehling - | (c) 1998 - 2004 | Katja Zedel + | (c) 1998 - 2014 | Katja Zedel | | +-------------------+ @@ -44,7 +44,7 @@ static struct locale_data *locale_array[16]; static int nlocales = 0; typedef struct order_data { - char *_str; + const char *_str; # ifdef LOMEM int _refcount:20; int _lindex:4; @@ -59,8 +59,6 @@ static void release_data(order_data * data) { if (data) { if (--data->_refcount == 0) { - if (data->_str) - free(data->_str); free(data); } } @@ -182,6 +180,22 @@ void free_orders(order ** olist) } } +static char *mkdata(order_data **pdata, size_t len, keyword_t kwd, int lindex, const char *str) +{ + order_data *data; + char *result; + data = malloc(sizeof(order_data) + len +1); + result = (char *)(data + 1); + data->_keyword = kwd; + data->_lindex = lindex; + data->_refcount = 0; + data->_str = 0; + data->_str = (len > 0) ? result : 0; + if (str) strcpy(result, str); + if (pdata) *pdata = data; + return result; +} + static order_data *create_data(keyword_t kwd, const char *sptr, int lindex) { const char *s = sptr; @@ -204,21 +218,15 @@ static order_data *create_data(keyword_t kwd, const char *sptr, int lindex) data = locale_array[lindex]->study_orders[sk]; if (data == NULL) { const char *skname = skillname(sk, lang); - data = (order_data *)malloc(sizeof(order_data)); + const char *spc = strchr(skname, ' '); + size_t len = strlen(skname); + char *dst = mkdata(&data, len + (spc ? 3 : 0), kwd, lindex, spc ? 0 : skname); locale_array[lindex]->study_orders[sk] = data; - data->_keyword = kwd; - data->_lindex = lindex; - assert(data->_lindex >= 0); - if (strchr(skname, ' ') != NULL) { - size_t len = strlen(skname); - data->_str = malloc(len + 3); - data->_str[0] = '\"'; - memcpy(data->_str + 1, skname, len); - data->_str[len + 1] = '\"'; - data->_str[len + 2] = '\0'; - } - else { - data->_str = _strdup(skname); + if (spc) { + dst[0] = '\"'; + memcpy(dst + 1, skname, len); + dst[len + 1] = '\"'; + dst[len + 2] = '\0'; } data->_refcount = 1; } @@ -231,22 +239,13 @@ static order_data *create_data(keyword_t kwd, const char *sptr, int lindex) else if (kwd != NOKEYWORD && *sptr == 0) { data = locale_array[lindex]->short_orders[kwd]; if (data == NULL) { - data = (order_data *)malloc(sizeof(order_data)); - locale_array[lindex]->short_orders[kwd] = data; - data->_keyword = kwd; - data->_lindex = lindex; - assert(data->_lindex >= 0); - data->_str = NULL; + mkdata(&data, 0, kwd, lindex, 0); data->_refcount = 1; } ++data->_refcount; return data; } - data = (order_data *)malloc(sizeof(order_data)); - data->_keyword = kwd; - data->_lindex = lindex; - assert(data->_lindex >= 0); - data->_str = s ? _strdup(s) : NULL; + mkdata(&data, s ? strlen(s) : 0, kwd, lindex, s); data->_refcount = 1; return data; } diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c new file mode 100644 index 000000000..0d769825c --- /dev/null +++ b/src/kernel/order.test.c @@ -0,0 +1,48 @@ +#include +#include "order.h" + +#include +#include + +#include +#include + +static void test_create_order(CuTest *tc) { + char cmd[32]; + order *ord; + struct locale * lang = get_or_create_locale("en"); + + locale_setstring(lang, "keyword::move", "MOVE"); + ord = create_order(K_MOVE, lang, "NORTH"); + CuAssertPtrNotNull(tc, ord); + CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); + init_tokens(ord); + CuAssertStrEquals(tc, "MOVE NORTH", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MOVE", getstrtoken()); + CuAssertStrEquals(tc, "NORTH", getstrtoken()); + free_order(ord); +} + +static void test_parse_order(CuTest *tc) { + char cmd[32]; + order *ord; + struct locale * lang = get_or_create_locale("en"); + + locale_setstring(lang, "keyword::move", "MOVE"); + ord = parse_order("MOVE NORTH", lang); + CuAssertPtrNotNull(tc, ord); + CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); + init_tokens(ord); + CuAssertStrEquals(tc, "MOVE NORTH", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MOVE", getstrtoken()); + CuAssertStrEquals(tc, "NORTH", getstrtoken()); + free_order(ord); +} + +CuSuite *get_order_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_create_order); + SUITE_ADD_TEST(suite, test_parse_order); + return suite; +} diff --git a/src/test_eressea.c b/src/test_eressea.c index 0cc26348c..3f3061490 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -9,36 +9,6 @@ CuSuite *get_##name##_suite(void); \ CuSuiteAddSuite(suite, get_##name##_suite()) -CuSuite *get_tests_suite(void); -CuSuite *get_callback_suite(void); -CuSuite *get_jsonconf_suite(void); -CuSuite *get_json_suite(void); -CuSuite *get_economy_suite(void); -CuSuite *get_laws_suite(void); -CuSuite *get_market_suite(void); -CuSuite *get_battle_suite(void); -CuSuite *get_building_suite(void); -CuSuite *get_curse_suite(void); -CuSuite *get_equipment_suite(void); -CuSuite *get_item_suite(void); -CuSuite *get_magic_suite(void); -CuSuite *get_move_suite(void); -CuSuite *get_pool_suite(void); -CuSuite *get_build_suite(void); -CuSuite *get_reports_suite(void); -CuSuite *get_ship_suite(void); -CuSuite *get_spellbook_suite(void); -CuSuite *get_spell_suite(void); -CuSuite *get_base36_suite(void); -CuSuite *get_config_suite(void); -CuSuite *get_bsdstring_suite(void); -CuSuite *get_functions_suite(void); -CuSuite *get_umlaut_suite(void); -CuSuite *get_ally_suite(void); -CuSuite *get_direction_suite(void); -CuSuite *get_skill_suite(void); -CuSuite *get_keyword_suite(void); - int RunAllTests(void) { CuString *output = CuStringNew(); @@ -56,6 +26,7 @@ int RunAllTests(void) ADD_TESTS(suite, direction); ADD_TESTS(suite, skill); ADD_TESTS(suite, keyword); + ADD_TESTS(suite, order); /* util */ ADD_TESTS(suite, config); ADD_TESTS(suite, base36);