From: Ondřej Surý Date: Wed, 22 Sep 2021 18:01:20 +0000 (+0200) Subject: Use isc_mem_reget() when growing buffer dynamically X-Git-Tag: v9.17.19~27^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=15d6249260bc7e9ded7890d95a31c568e7dac586;p=thirdparty%2Fbind9.git Use isc_mem_reget() when growing buffer dynamically 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. --- diff --git a/lib/isc/buffer.c b/lib/isc/buffer.c index 1db0eb026e0..05bf4143b4e 100644 --- a/lib/isc/buffer.c +++ b/lib/isc/buffer.c @@ -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);