From: Vladimír Čunát Date: Fri, 7 Jul 2017 12:59:13 +0000 (+0200) Subject: lua nitpicks: memory management X-Git-Tag: v1.3.2~12^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41b2544ac31f82a51738efad60ff2e606df55d3c;p=thirdparty%2Fknot-resolver.git lua nitpicks: memory management --- diff --git a/daemon/lua/kres.lua.in b/daemon/lua/kres.lua.in index f302a08a3..994c3796d 100644 --- a/daemon/lua/kres.lua.in +++ b/daemon/lua/kres.lua.in @@ -135,6 +135,8 @@ local rrset_buflen = (64 + 1) * 1024 local rrset_buf = ffi.new('char[?]', rrset_buflen) local knot_rrset_t = ffi.typeof('knot_rrset_t') ffi.metatype( knot_rrset_t, { + -- beware: `owner` and `rdata` are typed as a plain lua strings + -- and not the real types they represent. __index = { owner = function(rr) return ffi.string(rr._owner, knot.knot_dname_size(rr._owner)) end, ttl = function(rr) return tonumber(knot.knot_rrset_ttl(rr)) end, @@ -317,7 +319,7 @@ local function rr2str(rr, style) -- Construct a single-RR temporary set while minimizing copying. local rrs = knot_rrset_t() knot.knot_rrset_init_empty(rrs) - rrs._owner = ffi.cast('char *', rr.owner) -- explicit cast needed here + rrs._owner = ffi.cast('knot_dname_t *', rr.owner) -- explicit cast needed here rrs.type = rr.type rrs.rclass = kres.class.IN knot.knot_rrset_add_rdata(rrs, rr.rdata, #rr.rdata, rr.ttl, nil) diff --git a/modules/dns64/dns64.lua b/modules/dns64/dns64.lua index 206daa053..e9c830b26 100644 --- a/modules/dns64/dns64.lua +++ b/modules/dns64/dns64.lua @@ -30,7 +30,7 @@ mod.layer = { if orig.type == kres.type.A then local rrs = ffi.typeof('knot_rrset_t')() ffi.C.knot_rrset_init_empty(rrs) - rrs._owner = ffi.cast('char *', orig:owner()) -- explicit cast needed here + rrs._owner = ffi.cast('knot_dname_t *', orig:owner()) -- explicit cast needed here rrs.type = kres.type.AAAA rrs.rclass = orig.rclass for k = 1, orig.rrs.rr_count do @@ -39,6 +39,8 @@ mod.layer = { ffi.copy(addr_buf + 12, rdata, 4) ffi.C.knot_rrset_add_rdata(rrs, ffi.string(addr_buf, 16), 16, orig:ttl(), req.pool) end + -- All referred memory is copied within the function, + -- so it doesn't matter that lua GCs our variables. ffi.C.kr_ranked_rrarray_add( req.answ_selected, rrs,