]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: Free some memory in scripting subsystem at shutdown
authorDonald Sharp <sharpd@nvidia.com>
Tue, 11 Oct 2022 17:21:03 +0000 (13:21 -0400)
committerMergify <37929162+mergify[bot]@users.noreply.github.com>
Thu, 13 Oct 2022 05:09:25 +0000 (05:09 +0000)
Pre:
staticd: showing active allocations in memory group libfrr
staticd: memstats:  Scripting                     :     16 * (variably sized)
staticd: memstats:  Hash                          :      2 * (variably sized)
staticd: memstats:  Hash Bucket                   :      8 *         32
staticd: memstats:  Hash Index                    :      1 * (variably sized)
staticd: memstats:  Link List                     :      1 *         40
staticd: memstats:  Link Node                     :      1 *         24
staticd: showing active allocations in memory group logging subsystem
staticd: memstats:  log file target               :      1 *         88
staticd: showing active allocations in memory group staticd

Post:
staticd: showing active allocations in memory group libfrr
staticd: showing active allocations in memory group logging subsystem
staticd: memstats:  log file target               :      1 *         88
staticd: showing active allocations in memory group staticd

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
(cherry picked from commit ca28a0f6faef1e0b547966fef49d6987903684b0)

lib/frrscript.c
lib/frrscript.h
lib/libfrr.c

index a19bd0c3dba54ac81c5e2db051f687173c48579c..2e56932613261fe69e7245965f34ecf950978557 100644 (file)
@@ -184,13 +184,14 @@ static void *codec_alloc(void *arg)
        return e;
 }
 
-#if 0
-static void codec_free(struct codec *c)
+static void codec_free(void *data)
 {
-       XFREE(MTYPE_TMP, c->typename);
-       XFREE(MTYPE_TMP, c);
+       struct frrscript_codec *c = data;
+       char *constworkaroundandihateit = (char *)c->typename;
+
+       XFREE(MTYPE_SCRIPT, constworkaroundandihateit);
+       XFREE(MTYPE_SCRIPT, c);
 }
-#endif
 
 /* Lua function hash utils */
 
@@ -212,17 +213,18 @@ bool lua_function_hash_cmp(const void *d1, const void *d2)
 void *lua_function_alloc(void *arg)
 {
        struct lua_function_state *tmp = arg;
-
        struct lua_function_state *lfs =
                XCALLOC(MTYPE_SCRIPT, sizeof(struct lua_function_state));
+
        lfs->name = tmp->name;
        lfs->L = tmp->L;
        return lfs;
 }
 
-static void lua_function_free(struct hash_bucket *b, void *data)
+static void lua_function_free(void *data)
 {
-       struct lua_function_state *lfs = (struct lua_function_state *)b->data;
+       struct lua_function_state *lfs = data;
+
        lua_close(lfs->L);
        XFREE(MTYPE_SCRIPT, lfs);
 }
@@ -409,7 +411,8 @@ fail:
 
 void frrscript_delete(struct frrscript *fs)
 {
-       hash_iterate(fs->lua_function_hash, lua_function_free, NULL);
+       hash_clean(fs->lua_function_hash, lua_function_free);
+       hash_free(fs->lua_function_hash);
        XFREE(MTYPE_SCRIPT, fs->name);
        XFREE(MTYPE_SCRIPT, fs);
 }
@@ -425,4 +428,11 @@ void frrscript_init(const char *sd)
        frrscript_register_type_codecs(frrscript_codecs_lib);
 }
 
+void frrscript_fini(void)
+{
+       hash_clean(codec_hash, codec_free);
+       hash_free(codec_hash);
+
+       frrscript_names_destroy();
+}
 #endif /* HAVE_SCRIPTING */
index 4db3e6f1b2260907a001196ee6877d4546df9d9c..7fa01f70d1baf861c0a3b819c6e37f84f2b4f5ee 100644 (file)
@@ -161,6 +161,11 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs);
  */
 void frrscript_init(const char *scriptdir);
 
+/*
+ * On shutdown clean up memory associated with the scripting subsystem
+ */
+void frrscript_fini(void);
+
 /*
  * This macro is mapped to every (name, value) in frrscript_call,
  * so this in turn maps them onto their encoders
index f5aecd9f75a3ec4c14260661a94cb56a26f5cdf0..aee698185432c7a3c3fe55718d7f7ba23dce315a 100644 (file)
@@ -1219,6 +1219,10 @@ void frr_fini(void)
        db_close();
 #endif
        log_ref_fini();
+
+#ifdef HAVE_SCRIPTING
+       frrscript_fini();
+#endif
        frr_pthread_finish();
        zprivs_terminate(di->privs);
        /* signal_init -> nothing needed */