From 3c60f863a572ad5c04f2c9c0be69267a04000c78 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 26 Feb 2017 18:17:58 +0100 Subject: [PATCH] remove the special_resources trie. memory leak, bad performance. --- scripts/eressea/xmasitems.lua | 1 + src/bind_region.c | 93 +++++++++++++++-------------------- 2 files changed, 41 insertions(+), 53 deletions(-) diff --git a/scripts/eressea/xmasitems.lua b/scripts/eressea/xmasitems.lua index a05e023b8..27ea641ee 100644 --- a/scripts/eressea/xmasitems.lua +++ b/scripts/eressea/xmasitems.lua @@ -25,6 +25,7 @@ end function use_stardust(u, amount) local p = u.region:get_resource("peasant") + assert(p>0) p = math.ceil(1.5 * p) u.region:set_resource("peasant", p) local msg = usepotion_message(u, "stardust") diff --git a/src/bind_region.c b/src/bind_region.c index 51a5bedd4..9e4e2f778 100644 --- a/src/bind_region.c +++ b/src/bind_region.c @@ -341,20 +341,16 @@ static int tolua_region_get_resourcelevel(lua_State * L) #define LUA_ASSERT(c, s) if (!(c)) { log_error("%s(%d): %s\n", __FILE__, __LINE__, (s)); return 0; } -static critbit_tree * special_resources(void) -{ - static critbit_tree cb = CRITBIT_TREE(); - if (!cb.root) { - const char * special[] = { "seed", "sapling", "tree", "grave", "chaos", 0 }; - char buffer[32]; - int i; - for (i = 0; special[i]; ++i) { - size_t len = strlen(special[i]); - len = cb_new_kv(special[i], len, &i, sizeof(int), buffer); - cb_insert(&cb, buffer, len); +static int special_resource(const char *type) { + const char * special[] = { "seed", "sapling", "tree", "grave", "chaos", 0 }; + int i; + + for (i = 0; special[i]; ++i) { + if (strcmp(type, special[i]) == 0) { + return i; } } - return &cb; + return -1; } static int tolua_region_get_resource(lua_State * L) @@ -362,32 +358,27 @@ static int tolua_region_get_resource(lua_State * L) region *r; const char *type; const resource_type *rtype; - int result = 0; - void * match; - critbit_tree * cb = special_resources(); + int result; r = (region *)tolua_tousertype(L, 1, 0); LUA_ASSERT(r != NULL, "invalid parameter"); type = tolua_tostring(L, 2, 0); LUA_ASSERT(type != NULL, "invalid parameter"); - - if (cb_find_prefix(cb, type, strlen(type) + 1, &match, 1, 0)) { - cb_get_kv(match, &result, sizeof(result)); - switch (result) { - case 0: - case 1: - case 2: - result = rtrees(r, result); - break; - case 3: - result = deathcount(r); - break; - case 4: - result = get_chaoscount(r); - break; - } - } - else { + + result = special_resource(type); + switch (result) { + case 0: + case 1: + case 2: + result = rtrees(r, result); + break; + case 3: + result = deathcount(r); + break; + case 4: + result = get_chaoscount(r); + break; + default: rtype = rt_find(type); if (rtype) { result = region_getresource(r, rtype); @@ -406,27 +397,23 @@ static int tolua_region_set_resource(lua_State * L) region *r = (region *)tolua_tousertype(L, 1, 0); const char *type = tolua_tostring(L, 2, 0); int result, value = (int)tolua_tonumber(L, 3, 0); - critbit_tree * cb = special_resources(); - void * match; + const resource_type *rtype; - if (cb_find_prefix(cb, type, strlen(type) + 1, &match, 1, 0)) { - cb_get_kv(match, &result, sizeof(result)); - switch (result) { - case 0: - case 1: - case 2: - rsettrees(r, result, value); - break; - case 3: - deathcounts(r, value - deathcount(r)); - break; - case 4: - add_chaoscount(r, value - get_chaoscount(r)); - break; - } - } - else { - const resource_type *rtype = rt_find(type); + result = special_resource(type); + switch (result) { + case 0: + case 1: + case 2: + rsettrees(r, result, value); + break; + case 3: + deathcounts(r, value - deathcount(r)); + break; + case 4: + add_chaoscount(r, value - get_chaoscount(r)); + break; + default: + rtype = rt_find(type); if (rtype != NULL) { region_setresource(r, rtype, value); }