2021-07-21 23:56:17 +00:00
|
|
|
#pragma once
|
2022-03-11 10:04:35 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2021-07-21 23:56:17 +00:00
|
|
|
|
|
|
|
#include <new>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/defines.h>
|
2021-07-21 23:56:17 +00:00
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
#include <Common/Concepts.h>
|
2021-07-21 23:56:17 +00:00
|
|
|
#include <Common/CurrentMemoryTracker.h>
|
2022-01-18 06:51:13 +00:00
|
|
|
#include <Common/config.h>
|
2021-07-21 23:56:17 +00:00
|
|
|
|
|
|
|
#if USE_JEMALLOC
|
|
|
|
# include <jemalloc/jemalloc.h>
|
|
|
|
#endif
|
|
|
|
|
2022-01-18 06:51:13 +00:00
|
|
|
#if !USE_JEMALLOC
|
2021-07-21 23:56:17 +00:00
|
|
|
# include <cstdlib>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Memory
|
|
|
|
{
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
inline ALWAYS_INLINE size_t alignToSizeT(std::align_val_t align) noexcept
|
|
|
|
{
|
|
|
|
return static_cast<size_t>(align);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::same_as<std::align_val_t>... TAlign>
|
|
|
|
requires DB::OptionalArgument<TAlign...>
|
|
|
|
inline ALWAYS_INLINE void * newImpl(std::size_t size, TAlign... align)
|
2021-07-21 23:56:17 +00:00
|
|
|
{
|
2022-03-11 10:04:35 +00:00
|
|
|
void * ptr = nullptr;
|
|
|
|
if constexpr (sizeof...(TAlign) == 1)
|
|
|
|
ptr = aligned_alloc(alignToSizeT(align...), size);
|
|
|
|
else
|
|
|
|
ptr = malloc(size);
|
|
|
|
|
2021-07-21 23:56:17 +00:00
|
|
|
if (likely(ptr != nullptr))
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
/// @note no std::get_new_handler logic implemented
|
|
|
|
throw std::bad_alloc{};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ALWAYS_INLINE void * newNoExept(std::size_t size) noexcept
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
inline ALWAYS_INLINE void * newNoExept(std::size_t size, std::align_val_t align) noexcept
|
|
|
|
{
|
|
|
|
return aligned_alloc(static_cast<size_t>(align), size);
|
|
|
|
}
|
|
|
|
|
2021-07-21 23:56:17 +00:00
|
|
|
inline ALWAYS_INLINE void deleteImpl(void * ptr) noexcept
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2022-01-18 06:51:13 +00:00
|
|
|
#if USE_JEMALLOC
|
2021-07-21 23:56:17 +00:00
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
template <std::same_as<std::align_val_t>... TAlign>
|
|
|
|
requires DB::OptionalArgument<TAlign...>
|
|
|
|
inline ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size, TAlign... align) noexcept
|
2021-07-21 23:56:17 +00:00
|
|
|
{
|
|
|
|
if (unlikely(ptr == nullptr))
|
|
|
|
return;
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
if constexpr (sizeof...(TAlign) == 1)
|
|
|
|
sdallocx(ptr, size, MALLOCX_ALIGN(alignToSizeT(align...)));
|
|
|
|
else
|
|
|
|
sdallocx(ptr, size, 0);
|
2021-07-21 23:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
template <std::same_as<std::align_val_t>... TAlign>
|
|
|
|
requires DB::OptionalArgument<TAlign...>
|
|
|
|
inline ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size [[maybe_unused]], TAlign... /* align */) noexcept
|
2021-07-21 23:56:17 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_LINUX)
|
2022-03-11 10:04:35 +00:00
|
|
|
# include <malloc.h>
|
2021-07-21 23:56:17 +00:00
|
|
|
#elif defined(OS_DARWIN)
|
2022-03-11 10:04:35 +00:00
|
|
|
# include <malloc/malloc.h>
|
2021-07-21 23:56:17 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
template <std::same_as<std::align_val_t>... TAlign>
|
|
|
|
requires DB::OptionalArgument<TAlign...>
|
|
|
|
inline ALWAYS_INLINE size_t getActualAllocationSize(size_t size, TAlign... align)
|
2021-07-21 23:56:17 +00:00
|
|
|
{
|
|
|
|
size_t actual_size = size;
|
|
|
|
|
2022-01-18 06:51:13 +00:00
|
|
|
#if USE_JEMALLOC
|
2021-07-21 23:56:17 +00:00
|
|
|
/// The nallocx() function allocates no memory, but it performs the same size computation as the mallocx() function
|
|
|
|
/// @note je_mallocx() != je_malloc(). It's expected they don't differ much in allocation logic.
|
|
|
|
if (likely(size != 0))
|
2022-03-11 10:04:35 +00:00
|
|
|
{
|
|
|
|
if constexpr (sizeof...(TAlign) == 1)
|
|
|
|
actual_size = nallocx(size, MALLOCX_ALIGN(alignToSizeT(align...)));
|
|
|
|
else
|
|
|
|
actual_size = nallocx(size, 0);
|
|
|
|
}
|
2021-07-21 23:56:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return actual_size;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
template <std::same_as<std::align_val_t>... TAlign>
|
|
|
|
requires DB::OptionalArgument<TAlign...>
|
|
|
|
inline ALWAYS_INLINE void trackMemory(std::size_t size, TAlign... align)
|
2021-07-21 23:56:17 +00:00
|
|
|
{
|
2022-03-11 10:04:35 +00:00
|
|
|
std::size_t actual_size = getActualAllocationSize(size, align...);
|
2021-07-21 23:56:17 +00:00
|
|
|
CurrentMemoryTracker::allocNoThrow(actual_size);
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:04:35 +00:00
|
|
|
template <std::same_as<std::align_val_t>... TAlign>
|
|
|
|
requires DB::OptionalArgument<TAlign...>
|
|
|
|
inline ALWAYS_INLINE void untrackMemory(void * ptr [[maybe_unused]], std::size_t size [[maybe_unused]] = 0, TAlign... align) noexcept
|
2021-07-21 23:56:17 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-01-18 06:51:13 +00:00
|
|
|
#if USE_JEMALLOC
|
2022-03-11 10:04:35 +00:00
|
|
|
|
2021-07-21 23:56:17 +00:00
|
|
|
/// @note It's also possible to use je_malloc_usable_size() here.
|
|
|
|
if (likely(ptr != nullptr))
|
2022-03-11 10:04:35 +00:00
|
|
|
{
|
|
|
|
if constexpr (sizeof...(TAlign) == 1)
|
|
|
|
CurrentMemoryTracker::free(sallocx(ptr, MALLOCX_ALIGN(alignToSizeT(align...))));
|
|
|
|
else
|
|
|
|
CurrentMemoryTracker::free(sallocx(ptr, 0));
|
|
|
|
}
|
2021-07-21 23:56:17 +00:00
|
|
|
#else
|
|
|
|
if (size)
|
|
|
|
CurrentMemoryTracker::free(size);
|
|
|
|
# if defined(_GNU_SOURCE)
|
|
|
|
/// It's innaccurate resource free for sanitizers. malloc_usable_size() result is greater or equal to allocated size.
|
|
|
|
else
|
|
|
|
CurrentMemoryTracker::free(malloc_usable_size(ptr));
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
catch (...)
|
2022-03-11 10:04:35 +00:00
|
|
|
{
|
|
|
|
}
|
2021-07-21 23:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-03-11 10:04:35 +00:00
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|