#include <unistd.h>
+#include <isc/mem.h>
#include <isc/util.h>
#include <isc/uv.h>
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) */
+}