From 033e21e6dc4b004c1fb44bbb618e5e8e3eda1b8b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Vavru=C5=A1a?= Date: Mon, 22 Jun 2015 02:43:52 +0200 Subject: [PATCH] =?utf8?q?lib/utils:=20packet=20recycling=20(keeps=20heade?= =?utf8?q?r,=20doesn=E2=80=99t=20free=20memory)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- daemon/bindings/kres.c | 7 +------ lib/layer/rrcache.c | 5 +---- lib/utils.h | 6 ++++++ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/daemon/bindings/kres.c b/daemon/bindings/kres.c index 8d2befd1a..f77a304e3 100644 --- a/daemon/bindings/kres.c +++ b/daemon/bindings/kres.c @@ -150,14 +150,9 @@ static int pkt_question(lua_State *L) uint8_t dname[KNOT_DNAME_MAXLEN]; knot_dname_from_str(dname, lua_tostring(L, 2), sizeof(dname)); if (!knot_dname_is_equal(knot_pkt_qname(pkt), dname)) { - uint8_t header[KNOT_WIRE_HEADER_SIZE]; - memcpy(header, pkt->wire, sizeof(header)); - knot_pkt_clear(pkt); - memcpy(pkt->wire, header, sizeof(header)); - size_t max_size = pkt->max_size; + KR_PKT_RECYCLE(pkt); knot_pkt_put_question(pkt, dname, lua_tointeger(L, 3), lua_tointeger(L, 4)); pkt->parsed = pkt->size; - pkt->max_size = max_size; } return 0; } diff --git a/lib/layer/rrcache.c b/lib/layer/rrcache.c index 312470018..8af6e9b49 100644 --- a/lib/layer/rrcache.c +++ b/lib/layer/rrcache.c @@ -46,10 +46,7 @@ static int loot_rr(struct kr_cache_txn *txn, knot_pkt_t *pkt, const knot_dname_t /* Update packet question */ if (!knot_dname_is_equal(knot_pkt_qname(pkt), name)) { - uint8_t header[KNOT_WIRE_HEADER_SIZE]; - memcpy(header, pkt->wire, sizeof(header)); - knot_pkt_clear(pkt); - memcpy(pkt->wire, header, sizeof(header)); + KR_PKT_RECYCLE(pkt); knot_pkt_put_question(pkt, qry->sname, qry->sclass, qry->stype); } diff --git a/lib/utils.h b/lib/utils.h index 0d2bcba8d..e26fedd76 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -34,6 +34,12 @@ extern void _cleanup_fclose(FILE **p); * Defines. */ +/** @internal Fast packet reset. */ +#define KR_PKT_RECYCLE(pkt) do { \ + (pkt)->parsed = (pkt)->size = KNOT_WIRE_HEADER_SIZE; \ + knot_pkt_parse_question((pkt)); \ +} while (0) + /** Concatenate N strings. */ char* kr_strcatdup(unsigned n, ...); -- 2.47.3