]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use custom isc_mem based allocator for libuv
authorOndřej Surý <ondrej@isc.org>
Fri, 23 Sep 2022 11:54:33 +0000 (13:54 +0200)
committerOndřej Surý <ondrej@isc.org>
Tue, 27 Sep 2022 15:10:42 +0000 (17:10 +0200)
The libuv library provides a way to replace the default allocator with
user supplied allocator (malloc, realloc, calloc and free).

Create a memory context specifically for libuv to allow tracking the
memory usage that has originated from within libuv.  This requires
libuv >= 1.38.0 which provides uv_library_shutdown() function that
assures no more allocations will be made.

lib/isc/include/isc/uv.h
lib/isc/lib.c
lib/isc/uv.c

index 476d8010c7547356514ae72b9b1edfac7bfc4593..8f80b65f82e89049ee301b0a66fa8e03f91b35b3 100644 (file)
@@ -107,3 +107,14 @@ isc__uverr2result(int uverr, bool dolog, const char *file, unsigned int line,
        })
 
 #endif
+
+/*
+ * Internal
+ */
+
+void
+isc__uv_initialize(void);
+void
+isc__uv_shutdown(void);
+void
+isc__uv_setdestroycheck(bool check);
index dd42ea792bc60add69c14c9d1adfaf82d11d1ac5..5a9515dd8d3a8635aa82c41bb08b9a098ad53baf 100644 (file)
@@ -18,6 +18,7 @@
 #include <isc/os.h>
 #include <isc/tls.h>
 #include <isc/util.h>
+#include <isc/uv.h>
 
 #include "config.h"
 #include "mem_p.h"
@@ -46,11 +47,13 @@ isc__initialize(void) {
        isc__mem_initialize();
        isc__tls_initialize();
        isc__trampoline_initialize();
+       isc__uv_initialize();
        (void)isc_os_ncpus();
 }
 
 void
 isc__shutdown(void) {
+       isc__uv_shutdown();
        isc__trampoline_shutdown();
        isc__tls_shutdown();
        isc__mem_shutdown();
index 62c34415a5fb9d816df9284cb504674613ec3444..deb525287423e7111c661e128f67baed712d0e97 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <unistd.h>
 
+#include <isc/mem.h>
 #include <isc/util.h>
 #include <isc/uv.h>
 
@@ -97,3 +98,74 @@ isc__uverr2result(int uverr, bool dolog, const char *file, unsigned int line,
                return (ISC_R_UNEXPECTED);
        }
 }
+
+#if UV_VERSION_HEX >= UV_VERSION(1, 38, 0)
+static isc_mem_t *isc__uv_mctx = NULL;
+
+static void *
+isc__uv_malloc(size_t size) {
+       return (isc_mem_allocate(isc__uv_mctx, size));
+}
+
+static void *
+isc__uv_realloc(void *ptr, size_t size) {
+       return (isc_mem_reallocate(isc__uv_mctx, ptr, size));
+}
+
+static void *
+isc__uv_calloc(size_t count, size_t size) {
+       void *ptr;
+       size_t res;
+#if HAVE_BUILTIN_MUL_OVERFLOW
+       bool overflow = __builtin_mul_overflow(count, size, &res);
+       RUNTIME_CHECK(!overflow);
+#else
+       res = count * size;
+       REQUIRE(count == 0 || res / count == size);
+#endif
+
+       ptr = isc_mem_allocate(isc__uv_mctx, res);
+       memset(ptr, 0, res);
+
+       return (ptr);
+}
+
+static void
+isc__uv_free(void *ptr) {
+       if (ptr == NULL) {
+               return;
+       }
+       isc_mem_free(isc__uv_mctx, ptr);
+}
+#endif /* UV_VERSION_HEX >= UV_VERSION(1, 38, 0) */
+
+void
+isc__uv_initialize(void) {
+#if UV_VERSION_HEX >= UV_VERSION(1, 38, 0)
+       int r;
+       isc_mem_create(&isc__uv_mctx);
+       isc_mem_setname(isc__uv_mctx, "uv");
+       isc_mem_setdestroycheck(isc__uv_mctx, false);
+
+       r = uv_replace_allocator(isc__uv_malloc, isc__uv_realloc,
+                                isc__uv_calloc, isc__uv_free);
+       UV_RUNTIME_CHECK(uv_replace_allocator, r);
+#endif /* UV_VERSION_HEX >= UV_VERSION(1, 38, 0) */
+}
+
+void
+isc__uv_shutdown(void) {
+#if UV_VERSION_HEX >= UV_VERSION(1, 38, 0)
+       uv_library_shutdown();
+       isc_mem_destroy(&isc__uv_mctx);
+#endif /* UV_VERSION_HEX < UV_VERSION(1, 38, 0) */
+}
+
+void
+isc__uv_setdestroycheck(bool check) {
+#if UV_VERSION_HEX >= UV_VERSION(1, 38, 0)
+       isc_mem_setdestroycheck(isc__uv_mctx, check);
+#else
+       UNUSED(check);
+#endif /* UV_VERSION_HEX >= UV_VERSION(1, 6, 0) */
+}