]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: Add new MTYPE for script results
authorDonald Lee <dlqs@gmx.com>
Mon, 26 Jul 2021 15:27:43 +0000 (23:27 +0800)
committerDonald Lee <dlqs@gmx.com>
Mon, 26 Jul 2021 15:27:56 +0000 (23:27 +0800)
Signed-off-by: Donald Lee <dlqs@gmx.com>
bgpd/bgp_routemap.c
doc/developer/scripting.rst
lib/frrlua.c
lib/frrlua.h
tests/lib/test_frrscript.c

index b01186e6573ce3054aedfd167c6bfaba224c5cde..4b71ba30ce58d997c483a4920e89130ba7c8c718 100644 (file)
@@ -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);
 
index e40029c2b4e064f3a797822c7a736b1347b60c96..fa1c521fc6b32a4cae5fb0e0ade3ce1c091dff5b 100644 (file)
@@ -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.
 
index 5d0732eac0c5853fcc92e9a7c0049127b0920d4b..96d72694409ec4bc4b2b83736773b9142af164fc 100644 (file)
@@ -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;
 }
index 98bff00fd60af9d9366f812abfcd8e6aef806bd1..3e16c82e2255eba59ac0a4eb2d147cda505fc2d0 100644 (file)
@@ -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
index 44e644c52a00025f3b72b7384eaaf2e5ec84af12..7b23045978d91351c562212a9da051cd8bc31f39 100644 (file)
@@ -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 */