mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-05 09:05:40 +00:00
Use system allocator when jemalloc is unavailable
This commit adds support for systems where the jemalloc library is not available as a package, here's the quick summary: * On Linux - the jemalloc is usually available as a package, if configured --without-jemalloc, the shim would be used around malloc(), free(), realloc() and malloc_usable_size() * On macOS - the jemalloc is available from homebrew or macports, if configured --without-jemalloc, the shim would be used around malloc(), free(), realloc() and malloc_size() * On FreeBSD - the jemalloc is *the* system allocator, we just need to check for <malloc_np.h> header to get access to non-standard API * On NetBSD - the jemalloc is *the* system allocator, we just need to check for <jemalloc/jemalloc.h> header to get access to non-standard API * On a system hostile to users and developers (read OpenBSD) - the jemalloc API is emulated by using ((size_t *)ptr)[-1] field to hold the size information. The OpenBSD developers care only for themselves, so why should we care about speed on OpenBSD?
This commit is contained in:
@@ -167,6 +167,7 @@ libisc_la_SOURCES = \
|
||||
httpd.c \
|
||||
interfaceiter.c \
|
||||
iterated_hash.c \
|
||||
jemalloc_shim.h \
|
||||
lex.c \
|
||||
lib.c \
|
||||
log.c \
|
||||
|
118
lib/isc/jemalloc_shim.h
Normal file
118
lib/isc/jemalloc_shim.h
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(HAVE_JEMALLOC)
|
||||
|
||||
#include <isc/util.h>
|
||||
|
||||
const char *malloc_conf = NULL;
|
||||
|
||||
#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void *
|
||||
mallocx(size_t size, int flags) {
|
||||
UNUSED(flags);
|
||||
void *__ptr = malloc(size);
|
||||
REQUIRE(__ptr != NULL);
|
||||
return (__ptr);
|
||||
}
|
||||
|
||||
static inline void
|
||||
sdallocx(void *ptr, size_t size, int flags) {
|
||||
UNUSED(size);
|
||||
UNUSED(flags);
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static inline void *
|
||||
rallocx(void *ptr, size_t size, int flags) {
|
||||
UNUSED(flags);
|
||||
REQUIRE(size != 0);
|
||||
|
||||
void *__ptr = realloc(ptr, size);
|
||||
REQUIRE(__ptr != NULL);
|
||||
|
||||
return (__ptr);
|
||||
}
|
||||
|
||||
#ifdef HAVE_MALLOC_SIZE
|
||||
|
||||
#include <malloc/malloc.h>
|
||||
|
||||
static inline size_t
|
||||
sallocx(void *ptr, int flags) {
|
||||
UNUSED(flags);
|
||||
|
||||
return (malloc_size(ptr));
|
||||
}
|
||||
|
||||
#elif HAVE_MALLOC_USABLE_SIZE
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
static inline size_t
|
||||
sallocx(void *ptr, int flags) {
|
||||
UNUSED(flags);
|
||||
|
||||
return (malloc_usable_size(ptr));
|
||||
}
|
||||
|
||||
#endif /* HAVE_MALLOC_SIZE */
|
||||
|
||||
#else /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void *
|
||||
mallocx(size_t size, int flags) {
|
||||
UNUSED(flags);
|
||||
|
||||
size_t *__ptr = malloc(size + sizeof(size_t));
|
||||
REQUIRE(__ptr != NULL);
|
||||
__ptr[0] = size;
|
||||
|
||||
return (&__ptr[1]);
|
||||
}
|
||||
|
||||
static inline void
|
||||
sdallocx(void *ptr, size_t size, int flags) {
|
||||
UNUSED(size);
|
||||
UNUSED(flags);
|
||||
|
||||
free(&((size_t *)ptr)[-1]);
|
||||
}
|
||||
|
||||
static inline size_t
|
||||
sallocx(void *ptr, int flags) {
|
||||
UNUSED(flags);
|
||||
|
||||
return (((size_t *)ptr)[-1]);
|
||||
}
|
||||
|
||||
static inline void *
|
||||
rallocx(void *ptr, size_t size, int flags) {
|
||||
UNUSED(flags);
|
||||
|
||||
size_t *__ptr = realloc(&((size_t *)ptr)[-1], size);
|
||||
REQUIRE(__ptr != NULL);
|
||||
__ptr[0] = size;
|
||||
|
||||
return (&__ptr[1]);
|
||||
}
|
||||
|
||||
#endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
|
||||
|
||||
#endif /* !defined(HAVE_JEMALLOC) */
|
@@ -42,8 +42,19 @@
|
||||
#include <json_object.h>
|
||||
#endif /* HAVE_JSON_C */
|
||||
|
||||
#if defined(HAVE_MALLOC_NP_H)
|
||||
#include <malloc_np.h>
|
||||
#elif defined(HAVE_JEMALLOC)
|
||||
#include <jemalloc/jemalloc.h>
|
||||
|
||||
#if JEMALLOC_VERSION_MAJOR < 4
|
||||
#define sdallocx(ptr, size, flags) dallocx(ptr, flags)
|
||||
#endif /* JEMALLOC_VERSION_MAJOR < 4 */
|
||||
|
||||
#else
|
||||
#include "jemalloc_shim.h"
|
||||
#endif
|
||||
|
||||
#include "mem_p.h"
|
||||
|
||||
#define MCTXLOCK(m) LOCK(&m->lock)
|
||||
|
Reference in New Issue
Block a user