2019-07-11 15:40:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-15 18:57:00 +00:00
|
|
|
#include <new>
|
2020-03-19 10:38:34 +00:00
|
|
|
#include "defines.h"
|
2019-07-15 18:57:00 +00:00
|
|
|
|
2019-07-11 15:40:55 +00:00
|
|
|
#if __has_include(<common/config_common.h>)
|
|
|
|
#include <common/config_common.h>
|
|
|
|
#endif
|
|
|
|
|
2019-07-16 14:18:01 +00:00
|
|
|
#if USE_JEMALLOC
|
|
|
|
#include <jemalloc/jemalloc.h>
|
|
|
|
|
|
|
|
#if JEMALLOC_VERSION_MAJOR < 4
|
|
|
|
#undef USE_JEMALLOC
|
|
|
|
#define USE_JEMALLOC 0
|
|
|
|
#include <cstdlib>
|
|
|
|
#endif
|
2019-07-28 14:43:38 +00:00
|
|
|
#else
|
|
|
|
#include <cstdlib>
|
|
|
|
#endif
|
|
|
|
|
2019-07-15 18:57:00 +00:00
|
|
|
|
2019-07-16 16:36:10 +00:00
|
|
|
namespace Memory
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
inline ALWAYS_INLINE void * newImpl(std::size_t size)
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
2019-07-16 16:36:10 +00:00
|
|
|
auto * ptr = malloc(size);
|
2019-07-11 15:40:55 +00:00
|
|
|
if (likely(ptr != nullptr))
|
|
|
|
return ptr;
|
|
|
|
|
2019-07-16 16:36:10 +00:00
|
|
|
/// @note no std::get_new_handler logic implemented
|
|
|
|
throw std::bad_alloc{};
|
2019-07-11 15:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
inline ALWAYS_INLINE void * newNoExept(std::size_t size) noexcept
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
2019-07-16 16:36:10 +00:00
|
|
|
return malloc(size);
|
2019-07-11 15:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
inline 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-16 16:36:10 +00:00
|
|
|
#if USE_JEMALLOC
|
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
inline 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
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
inline ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size [[maybe_unused]]) noexcept
|
2019-07-11 15:40:55 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2019-07-16 16:36:10 +00:00
|
|
|
|
|
|
|
}
|