]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use isc_mem_reget() when growing buffer dynamically
authorOndřej Surý <ondrej@isc.org>
Wed, 22 Sep 2021 18:01:20 +0000 (20:01 +0200)
committerOndřej Surý <ondrej@sury.org>
Thu, 23 Sep 2021 20:17:15 +0000 (22:17 +0200)
Previously, we cannot use isc_mem_reallocate() for growing the buffer
dynamically, because the memory was allocated using the
isc_mem_get()/isc_mem_put() API.  With the introduction of the
isc_mem_reget() function, we can use grow/shrink the memory directly
without always moving the memory around as the allocator might have
reserved some extra space after the initial allocation.

lib/isc/buffer.c

index 1db0eb026e0ab986e190d3b103e333da73e74cc3..05bf4143b4e948ed3b7e7cd6de6c2ee927591ab3 100644 (file)
@@ -553,8 +553,7 @@ isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
 
 isc_result_t
 isc_buffer_reserve(isc_buffer_t **dynbuffer, unsigned int size) {
-       unsigned char *bdata;
-       uint64_t len;
+       size_t len;
 
        REQUIRE(dynbuffer != NULL);
        REQUIRE(ISC_BUFFER_VALID(*dynbuffer));
@@ -581,18 +580,9 @@ isc_buffer_reserve(isc_buffer_t **dynbuffer, unsigned int size) {
                return (ISC_R_NOMEMORY);
        }
 
-       /*
-        * XXXMUKS: This is far more expensive than plain realloc() as
-        * it doesn't remap pages, but does ordinary copy. So is
-        * isc_mem_reallocate(), which has additional issues.
-        */
-       bdata = isc_mem_get((*dynbuffer)->mctx, (unsigned int)len);
-
-       memmove(bdata, (*dynbuffer)->base, (*dynbuffer)->length);
-       isc_mem_put((*dynbuffer)->mctx, (*dynbuffer)->base,
-                   (*dynbuffer)->length);
-
-       (*dynbuffer)->base = bdata;
+       (*dynbuffer)->base = isc_mem_reget((*dynbuffer)->mctx,
+                                          (*dynbuffer)->base,
+                                          (*dynbuffer)->length, len);
        (*dynbuffer)->length = (unsigned int)len;
 
        return (ISC_R_SUCCESS);