ClickHouse/libs/libcommon/include/common/memory.h

98 lines
1.6 KiB
C++
Raw Normal View History

#pragma once
#include <new>
#include <common/likely.h>
#if __has_include(<common/config_common.h>)
#include <common/config_common.h>
#endif
#if defined(_MSC_VER)
#define ALWAYS_INLINE __forceinline
#define NO_INLINE static __declspec(noinline)
#else
2019-07-12 14:41:59 +00:00
#define ALWAYS_INLINE inline __attribute__((__always_inline__))
#define NO_INLINE __attribute__((__noinline__))
#endif
#if USE_JEMALLOC
#include <jemalloc/jemalloc.h>
namespace JeMalloc
{
void * handleOOM(std::size_t size, bool nothrow);
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void * newImpl(std::size_t size)
{
void * ptr = malloc(size);
if (likely(ptr != nullptr))
return ptr;
return handleOOM(size, false);
}
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void * newNoExept(std::size_t size) noexcept
{
void * ptr = malloc(size);
if (likely(ptr != nullptr))
return ptr;
return handleOOM(size, true);
}
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void deleteImpl(void * ptr) noexcept
{
free(ptr);
}
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size) noexcept
{
if (unlikely(ptr == nullptr))
return;
sdallocx(ptr, size, 0);
}
}
namespace Memory
{
using namespace JeMalloc;
}
#else
namespace Memory
{
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void * newImpl(std::size_t size)
{
auto * ptr = malloc(size);
if (likely(ptr != nullptr))
return ptr;
/// @note no std::get_new_handler logic implemented
2019-07-12 14:41:59 +00:00
throw std::bad_alloc{};
}
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void * newNoExept(std::size_t size) noexcept
{
return malloc(size);
}
2019-07-12 14:41:59 +00:00
ALWAYS_INLINE void deleteImpl(void * ptr) noexcept
{
free(ptr);
}
ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size [[maybe_unused]]) noexcept
{
free(ptr);
}
}
#endif