summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2019-03-02 18:15:12 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2019-03-08 00:11:03 +0100
commit6d7d9412f68c64380a96a7cba962f370f18fe3fe (patch)
treed21bec9c41d5a468cf617548cefa7603ed0fae09
parent33b39f23a964a5b8fb56b818d23c9d9fce856e8c (diff)
lua: Fix memory handling
-rw-r--r--lua/filter.c38
1 files changed, 35 insertions, 3 deletions
diff --git a/lua/filter.c b/lua/filter.c
index f4de8f1f..220bdcf1 100644
--- a/lua/filter.c
+++ b/lua/filter.c
@@ -95,7 +95,41 @@ static const char *lua_interpret_reader(lua_State *L UNUSED, void *ud, size_t *s
return out;
}
-struct f_val lua_interpret(struct lua_filter_chunk *chunk, struct rte **e, struct rta **a UNUSED, struct ea_list **ea UNUSED, struct linpool *lp, int flags UNUSED) {
+static inline void f_rte_cow(struct rte **e, struct rta **a)
+{
+ if (!((*e)->flags & REF_COW))
+ return;
+
+ *e = rte_do_cow(*e);
+}
+
+static void
+f_rta_cow(struct rte **e, struct rta **old_rta, struct linpool *lp)
+{
+ if (!rta_is_cached((*e)->attrs))
+ return;
+
+ /* Prepare to modify rte */
+ f_rte_cow(e, old_rta);
+
+ /* Store old rta to free it later, it stores reference from rte_cow() */
+ *old_rta = (*e)->attrs;
+
+ /*
+ * Get shallow copy of rta. Fields eattrs and nexthops of rta are shared
+ * with f_old_rta (they will be copied when the cached rta will be obtained
+ * at the end of f_run()), also the lock of hostentry is inherited (we
+ * suppose hostentry is not changed by filters).
+ */
+ (*e)->attrs = rta_do_cow((*e)->attrs, lp);
+
+ /* Re-cache the ea_list */
+ /* f_cache_eattrs(); */
+}
+
+struct f_val lua_interpret(struct lua_filter_chunk *chunk, struct rte **e, struct rta **a, struct ea_list **ea UNUSED, struct linpool *lp, int flags UNUSED) {
+ f_rta_cow(e, a, lp);
+
lua_State *L = luaB_getstate();
lua_bird_state *lbs = luaB_init(L, lp);
@@ -118,8 +152,6 @@ struct f_val lua_interpret(struct lua_filter_chunk *chunk, struct rte **e, struc
out = F_VAL(T_RETURN, i, F_ERROR);
}
- *ea = (*e)->attrs->eattrs;
-
luaB_close(L);
return out;
}