From 78f1ac25744f3db6d64570037066380f9b347abe Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Mon, 26 Jul 2021 23:27:43 +0800 Subject: [PATCH] lib: Add new MTYPE for script results Signed-off-by: Donald Lee --- bgpd/bgp_routemap.c | 2 +- doc/developer/scripting.rst | 6 +++--- lib/frrlua.c | 22 ++++++++++++++-------- lib/frrlua.h | 2 ++ tests/lib/test_frrscript.c | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index b01186e657..4b71ba30ce 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -426,7 +426,7 @@ route_match_script(void *rule, const struct prefix *prefix, void *object) break; } - XFREE(MTYPE_TMP, action); + XFREE(MTYPE_SCRIPT_RES, action); frrscript_delete(fs); diff --git a/doc/developer/scripting.rst b/doc/developer/scripting.rst index e40029c2b4..fa1c521fc6 100644 --- a/doc/developer/scripting.rst +++ b/doc/developer/scripting.rst @@ -274,7 +274,7 @@ So, a typical execution call, with error checking, looks something like this: goto DONE; // "d" might not have been in returned table assert(*d == 800); - XFREE(MTYPE_TMP, d); // caller responsible for free + XFREE(MTYPE_SCRIPT_RES, d); // caller responsible for free DONE: frrscript_delete(fs); @@ -430,7 +430,7 @@ This function can and should be implemented using ``lua_decode_*``: void *lua_toprefix(lua_State *L, int idx) { - struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + struct prefix *p = XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct prefix)); lua_decode_prefix(L, idx, p); return p; @@ -438,7 +438,7 @@ This function can and should be implemented using ``lua_decode_*``: The returned data must always be copied off the stack and the copy must be -allocated with ``MTYPE_TMP``. This way it is possible to unload the script +allocated with ``MTYPE_SCRIPT_RES``. This way it is possible to unload the script (destroy the state) without invalidating any references to values stored in it. Note that it is the caller's responsibility to free the data. diff --git a/lib/frrlua.c b/lib/frrlua.c index 5d0732eac0..96d7269440 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -29,6 +29,8 @@ #include "log.h" #include "buffer.h" +DEFINE_MTYPE(LIB, SCRIPT_RES, "Scripting results"); + /* Lua stuff */ /* @@ -81,7 +83,7 @@ void lua_decode_prefix(lua_State *L, int idx, struct prefix *prefix) void *lua_toprefix(lua_State *L, int idx) { - struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + struct prefix *p = XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct prefix)); lua_decode_prefix(L, idx, p); return p; } @@ -153,7 +155,8 @@ void lua_decode_interface(lua_State *L, int idx, struct interface *ifp) } void *lua_tointerface(lua_State *L, int idx) { - struct interface *ifp = XCALLOC(MTYPE_TMP, sizeof(struct interface)); + struct interface *ifp = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct interface)); lua_decode_interface(L, idx, ifp); return ifp; @@ -183,7 +186,8 @@ void lua_decode_inaddr(lua_State *L, int idx, struct in_addr *inaddr) void *lua_toinaddr(lua_State *L, int idx) { - struct in_addr *inaddr = XCALLOC(MTYPE_TMP, sizeof(struct in_addr)); + struct in_addr *inaddr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in_addr)); lua_decode_inaddr(L, idx, inaddr); return inaddr; } @@ -213,7 +217,8 @@ void lua_decode_in6addr(lua_State *L, int idx, struct in6_addr *in6addr) void *lua_toin6addr(lua_State *L, int idx) { - struct in6_addr *in6addr = XCALLOC(MTYPE_TMP, sizeof(struct in6_addr)); + struct in6_addr *in6addr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in6_addr)); lua_decode_in6addr(L, idx, in6addr); return in6addr; } @@ -243,7 +248,8 @@ void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su) void *lua_tosockunion(lua_State *L, int idx) { - union sockunion *su = XCALLOC(MTYPE_TMP, sizeof(union sockunion)); + union sockunion *su = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(union sockunion)); lua_decode_sockunion(L, idx, su); return su; @@ -262,7 +268,7 @@ void lua_decode_timet(lua_State *L, int idx, time_t *t) void *lua_totimet(lua_State *L, int idx) { - time_t *t = XCALLOC(MTYPE_TMP, sizeof(time_t)); + time_t *t = XCALLOC(MTYPE_SCRIPT_RES, sizeof(time_t)); lua_decode_timet(L, idx, t); return t; @@ -283,7 +289,7 @@ void lua_decode_integerp(lua_State *L, int idx, long long *num) void *lua_tointegerp(lua_State *L, int idx) { - long long *num = XCALLOC(MTYPE_TMP, sizeof(long long)); + long long *num = XCALLOC(MTYPE_SCRIPT_RES, sizeof(long long)); lua_decode_integerp(L, idx, num); return num; @@ -297,7 +303,7 @@ void lua_decode_stringp(lua_State *L, int idx, char *str) void *lua_tostringp(lua_State *L, int idx) { - char *string = XSTRDUP(MTYPE_TMP, lua_tostring(L, idx)); + char *string = XSTRDUP(MTYPE_SCRIPT_RES, lua_tostring(L, idx)); return string; } diff --git a/lib/frrlua.h b/lib/frrlua.h index 98bff00fd6..3e16c82e22 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -34,6 +34,8 @@ extern "C" { #endif +DECLARE_MTYPE(SCRIPT_RES); + /* * gcc-10 is complaining about the wrapper function * not being compatible with lua_pushstring returning diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index 44e644c52a..7b23045978 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -51,7 +51,7 @@ int main(int argc, char **argv) assert(a == 100); assert(b == 201); assert(*cptr == 303); - XFREE(MTYPE_TMP, cptr); + XFREE(MTYPE_SCRIPT_RES, cptr); long long n = 5; @@ -62,7 +62,7 @@ int main(int argc, char **argv) long long *ansptr = frrscript_get_result(fs, "fact", "ans", lua_tointegerp); assert(*ansptr == 120); - XFREE(MTYPE_TMP, ansptr); + XFREE(MTYPE_SCRIPT_RES, ansptr); /* Negative testing */ -- 2.39.5