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?
2021-05-25 12:46:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02: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?
2021-05-25 12:46:00 +02:00
|
|
|
* 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)
|
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
#include <stddef.h>
|
2022-06-03 12:23:49 +02:00
|
|
|
#include <string.h>
|
2021-07-13 12:35:52 +02: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?
2021-05-25 12:46:00 +02:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
const char *malloc_conf = NULL;
|
|
|
|
|
2023-08-10 17:02:43 +03:00
|
|
|
#define MALLOCX_ZERO ((int)0x40)
|
|
|
|
#define MALLOCX_TCACHE_NONE (0)
|
|
|
|
#define MALLOCX_ARENA(a) (0)
|
2021-12-14 21:50:23 +01: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?
2021-05-25 12:46:00 +02:00
|
|
|
#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2023-01-16 18:31:08 +02:00
|
|
|
#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
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
#include <malloc.h>
|
2023-01-16 18:31:08 +02:00
|
|
|
#endif
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
|
|
|
|
static inline size_t
|
|
|
|
sallocx(void *ptr, int flags) {
|
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
return (malloc_usable_size(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_MALLOC_SIZE */
|
|
|
|
|
2022-06-03 12:23:49 +02:00
|
|
|
static inline void *
|
|
|
|
mallocx(size_t size, int flags) {
|
|
|
|
void *ptr = malloc(size);
|
|
|
|
INSIST(ptr != NULL);
|
|
|
|
|
|
|
|
if ((flags & MALLOCX_ZERO) != 0) {
|
2023-02-03 12:29:00 +00:00
|
|
|
memset(ptr, 0, sallocx(ptr, flags));
|
2022-06-03 12:23:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
void *new_ptr;
|
2023-02-03 12:29:00 +00:00
|
|
|
size_t old_size, new_size;
|
2022-06-03 12:23:49 +02:00
|
|
|
|
|
|
|
REQUIRE(size != 0);
|
|
|
|
|
|
|
|
if ((flags & MALLOCX_ZERO) != 0) {
|
|
|
|
old_size = sallocx(ptr, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_ptr = realloc(ptr, size);
|
|
|
|
INSIST(new_ptr != NULL);
|
|
|
|
|
2023-02-03 12:29:00 +00:00
|
|
|
if ((flags & MALLOCX_ZERO) != 0) {
|
|
|
|
new_size = sallocx(new_ptr, flags);
|
|
|
|
if (new_size > old_size) {
|
|
|
|
memset((uint8_t *)new_ptr + old_size, 0,
|
|
|
|
new_size - old_size);
|
|
|
|
}
|
2022-06-03 12:23:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (new_ptr);
|
|
|
|
}
|
|
|
|
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
#else /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
typedef union {
|
|
|
|
size_t size;
|
|
|
|
max_align_t __alignment;
|
|
|
|
} size_info;
|
|
|
|
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
static inline void *
|
|
|
|
mallocx(size_t size, int flags) {
|
2021-07-13 12:35:52 +02:00
|
|
|
void *ptr = NULL;
|
|
|
|
|
2023-06-06 15:20:44 +01:00
|
|
|
size_t bytes = ISC_CHECKED_ADD(size, sizeof(size_info));
|
|
|
|
size_info *si = malloc(bytes);
|
2021-09-29 10:58:58 +02:00
|
|
|
INSIST(si != NULL);
|
2021-07-13 12:35:52 +02:00
|
|
|
|
|
|
|
si->size = size;
|
|
|
|
ptr = &si[1];
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
|
2022-06-03 12:23:49 +02:00
|
|
|
if ((flags & MALLOCX_ZERO) != 0) {
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
return (ptr);
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
sdallocx(void *ptr, size_t size, int flags) {
|
2021-07-13 12:35:52 +02:00
|
|
|
size_info *si = &(((size_info *)ptr)[-1]);
|
|
|
|
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
UNUSED(size);
|
|
|
|
UNUSED(flags);
|
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
free(si);
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t
|
|
|
|
sallocx(void *ptr, int flags) {
|
2021-07-13 12:35:52 +02:00
|
|
|
size_info *si = &(((size_info *)ptr)[-1]);
|
|
|
|
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
UNUSED(flags);
|
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
return (si[0].size);
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *
|
|
|
|
rallocx(void *ptr, size_t size, int flags) {
|
2022-06-03 12:23:49 +02:00
|
|
|
size_info *si = realloc(&(((size_info *)ptr)[-1]), size + sizeof(*si));
|
2021-09-29 10:58:58 +02:00
|
|
|
INSIST(si != NULL);
|
2021-07-13 12:35:52 +02:00
|
|
|
|
2022-06-03 12:23:49 +02:00
|
|
|
if ((flags & MALLOCX_ZERO) != 0 && size > si->size) {
|
|
|
|
memset((uint8_t *)si + sizeof(*si) + si->size, 0,
|
|
|
|
size - si->size);
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
si->size = size;
|
|
|
|
ptr = &si[1];
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
|
2021-07-13 12:35:52 +02:00
|
|
|
return (ptr);
|
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?
2021-05-25 12:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
|
|
|
|
|
|
|
|
#endif /* !defined(HAVE_JEMALLOC) */
|