From: Marek Vavruša Date: Mon, 22 Jun 2015 00:43:52 +0000 (+0200) Subject: lib/utils: packet recycling (keeps header, doesn’t free memory) X-Git-Tag: v1.0.0-beta1~116^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=033e21e6dc4b004c1fb44bbb618e5e8e3eda1b8b;p=thirdparty%2Fknot-resolver.git lib/utils: packet recycling (keeps header, doesn’t free memory) --- 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, ...);