mirror of
https://github.com/isc-projects/bind9.git
synced 2026-05-26 03:12:16 -04:00
chg: dev: Use malloc_usable_size()/malloc_size() for memory accounting
Restore usage of malloc_usable_size()/malloc_size(), but this time only for memory accounting and statistics purposes. This should reduce the memory footprint in case of compilation without jemalloc as we don't have to keep track of the allocated memory size ourselves. Merge branch 'ondrej/use-malloc_usable_size-when-available' into 'main' See merge request isc-projects/bind9!11271
This commit is contained in:
commit
d8410f93d2
3 changed files with 102 additions and 0 deletions
|
|
@ -32,6 +32,66 @@ const char *malloc_conf = NULL;
|
|||
#define MALLOCX_ZERO ((int)0x40)
|
||||
#define MALLOCX_ZERO_GET(flags) ((bool)(flags & MALLOCX_ZERO))
|
||||
|
||||
#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
|
||||
|
||||
#ifdef HAVE_MALLOC_SIZE
|
||||
|
||||
#include <malloc/malloc.h>
|
||||
|
||||
static inline size_t
|
||||
sallocx(void *ptr, int flags ISC_ATTR_UNUSED) {
|
||||
return malloc_size(ptr);
|
||||
}
|
||||
|
||||
#elif HAVE_MALLOC_USABLE_SIZE
|
||||
|
||||
#ifdef __DragonFly__
|
||||
/*
|
||||
* On DragonFly BSD 'man 3 malloc' advises us to include the following
|
||||
* header to have access to malloc_usable_size().
|
||||
*/
|
||||
#include <malloc_np.h>
|
||||
#else
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
static inline size_t
|
||||
sallocx(void *ptr, int flags ISC_ATTR_UNUSED) {
|
||||
return malloc_usable_size(ptr);
|
||||
}
|
||||
|
||||
#endif /* HAVE_MALLOC_SIZE */
|
||||
|
||||
static inline void *
|
||||
mallocx(size_t size, int flags) {
|
||||
void *ptr = malloc(size);
|
||||
INSIST(ptr != NULL);
|
||||
|
||||
if ((flags & MALLOCX_ZERO) != 0) {
|
||||
memset(ptr, 0, size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static inline void
|
||||
sdallocx(void *ptr, size_t size ISC_ATTR_UNUSED, int flags ISC_ATTR_UNUSED) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static inline void *
|
||||
rallocx(void *ptr, size_t size, int flags) {
|
||||
REQUIRE(size != 0);
|
||||
REQUIRE((flags & MALLOCX_ZERO) == 0);
|
||||
|
||||
ptr = realloc(ptr, size);
|
||||
INSIST(ptr != NULL);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
typedef union {
|
||||
size_t size;
|
||||
max_align_t __alignment;
|
||||
|
|
@ -85,4 +145,6 @@ rallocx(void *ptr, size_t size, int flags) {
|
|||
return ptr;
|
||||
}
|
||||
|
||||
#endif /* defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE) */
|
||||
|
||||
#endif /* !defined(HAVE_JEMALLOC) */
|
||||
|
|
|
|||
|
|
@ -724,7 +724,21 @@ isc__mem_reget(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size,
|
|||
DELETE_TRACE(ctx, old_ptr, old_size, func, file, line);
|
||||
mem_putstats(ctx, old_size);
|
||||
|
||||
ADJUST_ZERO_ALLOCATION_SIZE(new_size);
|
||||
|
||||
#ifdef HAVE_JEMALLOC
|
||||
new_ptr = mem_realloc(ctx, old_ptr, new_size, flags);
|
||||
#else
|
||||
new_ptr = mem_realloc(ctx, old_ptr, new_size,
|
||||
flags & ~ISC__MEM_ZERO);
|
||||
|
||||
if ((flags & ISC__MEM_ZERO) != 0) {
|
||||
if (new_size > old_size) {
|
||||
memset((uint8_t *)new_ptr + old_size, 0,
|
||||
new_size - old_size);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
mem_getstats(ctx, new_size);
|
||||
ADD_TRACE(ctx, new_ptr, new_size, func, file, line);
|
||||
|
|
|
|||
26
meson.build
26
meson.build
|
|
@ -718,6 +718,32 @@ if jemalloc_opt.allowed()
|
|||
endif
|
||||
endif
|
||||
|
||||
foreach fn : [
|
||||
'malloc_usable_size',
|
||||
]
|
||||
if cc.has_function(
|
||||
fn,
|
||||
prefix: '#include <malloc.h>',
|
||||
args: sys_defines,
|
||||
dependencies: thread_dep,
|
||||
)
|
||||
config.set('HAVE_@0@'.format(fn.to_upper()), 1)
|
||||
endif
|
||||
endforeach
|
||||
|
||||
foreach fn : [
|
||||
'malloc_size',
|
||||
]
|
||||
if cc.has_function(
|
||||
fn,
|
||||
prefix: '#include <malloc/malloc.h>',
|
||||
args: sys_defines,
|
||||
dependencies: thread_dep,
|
||||
)
|
||||
config.set('HAVE_@0@'.format(fn.to_upper()), 1)
|
||||
endif
|
||||
endforeach
|
||||
|
||||
## dnstap
|
||||
dnstap_dep = null_dep # Will be filled later
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue