1#pragma once
2
3#include <new>
4#include "likely.h"
5
6#if __has_include(<common/config_common.h>)
7#include <common/config_common.h>
8#endif
9
10#if USE_JEMALLOC
11#include <jemalloc/jemalloc.h>
12
13#if JEMALLOC_VERSION_MAJOR < 4
14 #undef USE_JEMALLOC
15 #define USE_JEMALLOC 0
16 #include <cstdlib>
17#endif
18#else
19#include <cstdlib>
20#endif
21
22// Also defined in Core/Defines.h
23#if !defined(ALWAYS_INLINE)
24#if defined(_MSC_VER)
25 #define ALWAYS_INLINE inline __forceinline
26#else
27 #define ALWAYS_INLINE inline __attribute__((__always_inline__))
28#endif
29#endif
30
31#if !defined(NO_INLINE)
32#if defined(_MSC_VER)
33 #define NO_INLINE static __declspec(noinline)
34#else
35 #define NO_INLINE __attribute__((__noinline__))
36#endif
37#endif
38
39namespace Memory
40{
41
42ALWAYS_INLINE void * newImpl(std::size_t size)
43{
44 auto * ptr = malloc(size);
45 if (likely(ptr != nullptr))
46 return ptr;
47
48 /// @note no std::get_new_handler logic implemented
49 throw std::bad_alloc{};
50}
51
52ALWAYS_INLINE void * newNoExept(std::size_t size) noexcept
53{
54 return malloc(size);
55}
56
57ALWAYS_INLINE void deleteImpl(void * ptr) noexcept
58{
59 free(ptr);
60}
61
62#if USE_JEMALLOC
63
64ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size) noexcept
65{
66 if (unlikely(ptr == nullptr))
67 return;
68
69 sdallocx(ptr, size, 0);
70}
71
72#else
73
74ALWAYS_INLINE void deleteSized(void * ptr, std::size_t size [[maybe_unused]]) noexcept
75{
76 free(ptr);
77}
78
79#endif
80
81}
82