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>
|
2025-06-25 08:25:41 +02:00
|
|
|
#include <stdlib.h>
|
2022-06-03 12:23:49 +02:00
|
|
|
#include <string.h>
|
2021-07-13 12:35:52 +02:00
|
|
|
|
2025-06-25 08:25:41 +02:00
|
|
|
#include <isc/overflow.h>
|
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;
|
|
|
|
|
2025-06-04 10:19:35 +02:00
|
|
|
/*
|
|
|
|
* The MALLOCX_ZERO and MALLOCX_ZERO_GET macros were taken literal from
|
|
|
|
* jemalloc_macros.h and jemalloc_internal_types.h headers respectively.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MALLOCX_ZERO ((int)0x40)
|
|
|
|
#define MALLOCX_ZERO_GET(flags) ((bool)(flags & MALLOCX_ZERO))
|
2021-12-14 21:50:23 +01:00
|
|
|
|
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
|
|
|
|
2025-06-04 10:19:35 +02:00
|
|
|
if (MALLOCX_ZERO_GET(flags)) {
|
2022-06-03 12:23:49 +02:00
|
|
|
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
|
2024-08-23 06:02:00 +02:00
|
|
|
sdallocx(void *ptr, size_t size ISC_ATTR_UNUSED, int flags ISC_ATTR_UNUSED) {
|
2021-07-13 12:35:52 +02:00
|
|
|
size_info *si = &(((size_info *)ptr)[-1]);
|
|
|
|
|
|
|
|
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
|
2024-08-23 06:02:00 +02:00
|
|
|
sallocx(void *ptr, int flags ISC_ATTR_UNUSED) {
|
2021-07-13 12:35:52 +02:00
|
|
|
size_info *si = &(((size_info *)ptr)[-1]);
|
|
|
|
|
|
|
|
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
|
|
|
|
2025-06-04 10:19:35 +02:00
|
|
|
if (MALLOCX_ZERO_GET(flags) && size > si->size) {
|
2022-06-03 12:23:49 +02:00
|
|
|
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_JEMALLOC) */
|