mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 06:25:31 +00:00
Use custom isc_mem based allocator for libuv
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.
This commit is contained in:
@@ -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);
|
||||
|
@@ -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();
|
||||
|
72
lib/isc/uv.c
72
lib/isc/uv.c
@@ -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) */
|
||||
}
|
||||
|
Reference in New Issue
Block a user