2019-07-11 15:40:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-15 18:57:00 +00:00
|
|
|
#include <new>
|
|
|
|
#include <common/likely.h>
|
|
|
|
|
2019-07-11 15:40:55 +00:00
|
|
|
#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__))
|
2019-07-11 15:40:55 +00:00
|
|
|
#define NO_INLINE __attribute__((__noinline__))
|
|
|
|
#endif
|
|
|
|
|
2019-07-15 18:57:00 +00:00
|
|
|
#if USE_JEMALLOC
|
|
|
|
|
|
|
|
#include <jemalloc/jemalloc.h>
|
|
|
|
|
2019-07-11 15:40:55 +00:00
|
|
|
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)
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
2019-07-15 18:57:00 +00:00
|
|
|
void * ptr = malloc(size);
|
2019-07-11 15:40:55 +00:00
|
|
|
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
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
2019-07-15 18:57:00 +00:00
|
|
|
void * ptr = malloc(size);
|
2019-07-11 15:40:55 +00:00
|
|
|
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
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
2019-07-15 18:57:00 +00:00
|
|
|
free(ptr);
|
2019-07-11 15:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 14:41:59 +00:00
|
|
|
ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size) noexcept
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
if (unlikely(ptr == nullptr))
|
|
|
|
return;
|
|
|
|
|
2019-07-15 18:57:00 +00:00
|
|
|
sdallocx(ptr, size, 0);
|
2019-07-11 15:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Memory
|
|
|
|
{
|
|
|
|
using namespace JeMalloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace Memory
|
|
|
|
{
|
|
|
|
|
2019-07-12 14:41:59 +00:00
|
|
|
ALWAYS_INLINE void * newImpl(std::size_t size)
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
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-11 15:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 14:41:59 +00:00
|
|
|
ALWAYS_INLINE void * newNoExept(std::size_t size) noexcept
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:41:59 +00:00
|
|
|
ALWAYS_INLINE void deleteImpl(void * ptr) noexcept
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:57:00 +00:00
|
|
|
ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size [[maybe_unused]]) noexcept
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|