resolved: grow DnsAnswer exponentially
authorLennart Poettering <lennart@poettering.net>
Wed, 9 Dec 2015 17:05:53 +0000 (18:05 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 10 Dec 2015 10:35:52 +0000 (11:35 +0100)
When increasing the DnsAnswer array, don't operate piecemeal, grow the
array exponentially.

This way, the default logic for DnsAnswer allocations matches the
behaviour for GREEDY_REALLOC and suchlike, and we can reduce the number
of necessary allocations.

src/resolve/resolved-dns-answer.c

index f1f4d90..92b9871 100644 (file)
@@ -271,6 +271,8 @@ void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) {
 int dns_answer_reserve(DnsAnswer **a, unsigned n_free) {
         DnsAnswer *n;
 
+        assert(a);
+
         if (n_free <= 0)
                 return 0;
 
@@ -285,6 +287,9 @@ int dns_answer_reserve(DnsAnswer **a, unsigned n_free) {
                 if ((*a)->n_allocated >= ns)
                         return 0;
 
+                /* Allocate more than we need */
+                ns *= 2;
+
                 n = realloc(*a, offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * ns);
                 if (!n)
                         return -ENOMEM;