From df88386cd185682091a92e2ccee5a3696510ce3f Mon Sep 17 00:00:00 2001 From: onefang Date: Fri, 30 Jul 2021 20:53:37 +1000 Subject: Dump and free Lua trees. --- src/sledjchisl/sledjchisl.c | 109 +++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 47 deletions(-) (limited to 'src/sledjchisl') diff --git a/src/sledjchisl/sledjchisl.c b/src/sledjchisl/sledjchisl.c index 5c79138..c158a61 100644 --- a/src/sledjchisl/sledjchisl.c +++ b/src/sledjchisl/sledjchisl.c @@ -670,6 +670,34 @@ qLua *qLuaGet(qtreetbl_t *tree, char *key) return (qLua *) tree->get(tree, key, NULL, false); } +void dumpLuaTree(qtreetbl_t *tree, char *name, int depth) +{ + qtreetbl_obj_t obj0; + + if (0 == depth) + fprintf(stderr, "DUMP Lua table %s -\n%s =\n{\n", name, name); + else + fprintf(stderr, "%*s[%s] =\n%*s{\n", depth * 2, "", name, depth * 2, ""); + memset((void*)&obj0, 0, sizeof(obj0)); // must be cleared before call + tree->lock(tree); // lock it when thread condition is expected + while(tree->getnext(tree, &obj0, false) == true) + { + qLua *q0 = obj0.data; + + switch (q0->type) + { + case LUA_TBOOLEAN : {fprintf(stderr, "%*s[%s] = %d,\n", (depth + 1) * 2, "", obj0.name, q0->v.b); break;} + case LUA_TINTEGER : {fprintf(stderr, "%*s[%s] = %d,\n", (depth + 1) * 2, "", obj0.name, q0->v.i); break;} + case LUA_TNUMBER : {fprintf(stderr, "%*s[%s] = %f,\n", (depth + 1) * 2, "", obj0.name, q0->v.f); break;} + case LUA_TSTRING : {fprintf(stderr, "%*s[%s] = %s,\n", (depth + 1) * 2, "", obj0.name, q0->v.s); break;} + case LUA_TTABLE : {dumpLuaTree(q0->v.t, obj0.name, depth + 1); break;} + default : {fprintf(stderr, "%*s[%s] ? %s,\n", (depth + 1) * 2, "", obj0.name, q0->v.s); break;} + } + } + tree->unlock(tree); + fprintf(stderr, "%*s},\n", depth * 2, ""); +} + qtreetbl_t *lua2tree() { qtreetbl_t *ret = qtreetbl(0); @@ -686,9 +714,19 @@ qtreetbl_t *lua2tree() lua_pushvalue(L, -2); // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table char *n = (char *) lua_tostring(L, -1); // 0, modifies key copy + int t = lua_type(L, -3); + + // Correct the sort order. Assuming we will never have more than 999,999 sims. Think that's a safe assumption. + /* https://www.hypergridbusiness.com/2021/07/opensim-land-growth/ + "Total area increased by the equivalent of 2,252 standard regions, for a new record high of 103,625 regions. + That’s nearly four times the area of Second Life," + */ + if ((LUA_TINTEGER == t) || (LUA_TNUMBER == t)) + { + char *tmp = xmprintf("%6s", n); + n = tmp; + } q.type = lua_type(L, -2); // 0 - // Numbers can convert to strings, so check for numbers before checking for strings. - // On the other hand, strings that can be converted to numbers also pass lua_isnumber(). sigh switch(q.type) { case LUA_TBOOLEAN : {q.v.b = lua_toboolean(L, -2); break;} @@ -705,6 +743,8 @@ qtreetbl_t *lua2tree() } } ret->put(ret, n, &q, sizeof(qLua)); + if ((LUA_TINTEGER == t) || (LUA_TNUMBER == t)) + free(n); // pop value + copy of key, leaving original key lua_pop(L, 2); // -2 value and copy of key // stack now contains: -1 => key; -2 => table @@ -752,6 +792,23 @@ qtreetbl_t *Lua2tree(char *file, char *var) return ret; } +void freeLuaTree(qtreetbl_t *tree) +{ + qtreetbl_obj_t obj0; + + memset((void*)&obj0, 0, sizeof(obj0)); // must be cleared before call + tree->lock(tree); // lock it when thread condition is expected + while(tree->getnext(tree, &obj0, false) == true) + { + qLua *q0 = obj0.data; + + if (LUA_TTABLE == q0->type) + freeLuaTree(q0->v.t); + } + tree->unlock(tree); + tree->free(tree); +} + int Lua2hashtbl(char *file, qhashtbl_t *hash, char *var) { @@ -815,6 +872,7 @@ int Lua2hashtbl(char *file, qhashtbl_t *hash, char *var) return ret; } + int LuaToHash(reqData *Rd, char *file, char *var, qhashtbl_t *tnm, int ret, struct stat *st, struct timespec *now, char *type) { struct timespec then; @@ -1154,51 +1212,7 @@ simList *getSims() size_t l = strlen(tnm); ourSims->simsLua = Lua2tree(file, "sims"); - -fprintf(stderr, "DEBUG simsLua table\n"); - qtreetbl_obj_t obj0; - - memset((void*)&obj0, 0, sizeof(obj0)); // must be cleared before call - ourSims->simsLua->lock(ourSims->simsLua); // lock it when thread condition is expected - while(ourSims->simsLua->getnext(ourSims->simsLua, &obj0, false) == true) - { - fprintf(stderr, "%s\n", obj0.name); - qLua *q0 = obj0.data; - - switch (q0->type) - { - case LUA_TBOOLEAN : {fprintf(stderr, " %s = %d\n", obj0.name, q0->v.b); break;} - case LUA_TINTEGER : {fprintf(stderr, " %s = %d\n", obj0.name, q0->v.i); break;} - case LUA_TNUMBER : {fprintf(stderr, " %s = %f\n", obj0.name, q0->v.f); break;} - case LUA_TSTRING : {fprintf(stderr, " %s = %s\n", obj0.name, q0->v.s); break;} - case LUA_TTABLE : {fprintf(stderr, " %s TABLE\n", obj0.name); break;} - default : {fprintf(stderr, " %s ? %s\n", obj0.name, q0->v.s); break;} - } - if (LUA_TTABLE == q0->type) - { - qtreetbl_t *t = q0->v.t; - qtreetbl_obj_t obj1; - - memset((void*)&obj1, 0, sizeof(obj1)); // must be cleared before call - t->lock(t); // lock it when thread condition is expected - while(t->getnext(t, &obj1, false) == true) - { - qLua *q1 = obj1.data; - switch (q1->type) - { - case LUA_TBOOLEAN : {fprintf(stderr, " %s = %d\n", obj1.name, q1->v.b); break;} - case LUA_TINTEGER : {fprintf(stderr, " %s = %d\n", obj1.name, q1->v.i); break;} - case LUA_TNUMBER : {fprintf(stderr, " %s = %f\n", obj1.name, q1->v.f); break;} - case LUA_TSTRING : {fprintf(stderr, " %s = %s\n", obj1.name, q1->v.s); break;} - case LUA_TTABLE : {fprintf(stderr, " %s TABLE\n", obj1.name); break;} - default : {fprintf(stderr, " %s ? %s\n", obj1.name, q1->v.s); break;} - } - } - t->unlock(t); - } - } - ourSims->simsLua->unlock(ourSims->simsLua); - + dumpLuaTree(ourSims->simsLua, "simsLua", 0); if (s) { @@ -1362,6 +1376,7 @@ void freeSimList(simList *sims) } sims->byTab->unlock(sims->byTab); sims->byTab->free(sims->byTab); + freeLuaTree(sims->simsLua); free(sims); } -- cgit v1.1