5 #ifdef DIARKIS_ENABLE_UE_BUILD
7 #include "CoreMinimal.h"
10 #include "Windows/WindowsSystemIncludes.h"
11 #include "Windows/AllowWindowsPlatformTypes.h"
14 THIRD_PARTY_INCLUDES_START
16 #endif // DIARKIS_ENABLE_UE_BUILD
18 #include "diarkis/config.h"
19 #include "diarkis/diarkis_export.h"
20 #include "system/stringHash.h"
22 #include <type_traits>
30 #include <unordered_map>
34 #ifdef DIARKIS_ENABLE_UE_BUILD
36 THIRD_PARTY_INCLUDES_END
39 #include "Windows/HideWindowsPlatformTypes.h"
42 #endif // DIARKIS_ENABLE_UE_BUILD
44 #ifndef DIARKIS_ENABLE_UE_BUILD
51 #define DIARKIS_UNUSED(var) (void)var
53 #define DIARKIS_UNUSED_IN_RELEASE(var) DIARKIS_UNUSED(var)
55 #define DIARKIS_ASSERT(x, msg) assert(x && msg)
57 #define DIARKIS_AUTHKEY_LEN 16
73 static const char* diarkisVersion =
"v1.0.0";
77 return Diarkis::diarkisVersion;
98 virtual void* Allocate(
size_t size,
int flag) = 0;
99 virtual void* AlignedAllocate(
size_t size,
size_t align,
int flag) = 0;
100 virtual void Deallocate(
void* ptr) = 0;
115 virtual void* Allocate(
size_t size,
int flag)
override;
116 virtual void* AlignedAllocate(
size_t size,
size_t align,
int flag)
override;
117 virtual void Deallocate(
void* ptr)
override;
119 size_t GetAllocatedSize()
const;
122 std::atomic<size_t>* allocatedSize_;
123 char buff_[
sizeof(std::atomic<size_t>)];
132 virtual int Initialize(
void* startAddr, std::size_t mainAreaSize,
size_t manageAreaSize, std::size_t align) = 0;
133 virtual void DeInitialize() = 0;
135 virtual std::size_t GetAllocatedSize()
const = 0;
136 virtual void PrintStatistics()
const = 0;
138 static std::size_t GetManageAreaMemorySize(std::size_t size, std::size_t align);
169 DIARKIS_EXPORT
void*
DiarkisAlloc(
size_t size,
int flag = 0);
175 template<
class T,
class... _Types>
179 return new(pMemory) T(std::forward<_Types>(_Args)...);
182 template <class T, typename std::enable_if<std::is_array<T>::value,
int>::type = 0>
183 inline typename std::remove_extent<T>::type*
DiarkisNew(
const size_t N)
185 using _Elem =
typename std::remove_extent<T>::type;
187 return new(pMemory) _Elem[N];
208 return reinterpret_cast<T*
>(
DiarkisAlloc(
sizeof(T) * n));
213 static_cast<void>(n);
218 template <
class T,
class U>
224 template <
class T,
class U>
238 typename std::enable_if<std::is_convertible<U*, T*>::value, std::nullptr_t>::type =
nullptr
257 typename std::enable_if<std::is_convertible<U(*)[], T(*)[]>::value, std::nullptr_t>::type =
nullptr
266 #if defined(DIARKIS_DISABLE_CUSTOM_ALLOCATOR_FOR_STD_LIBS)
270 template <
class T,
class... _Types,
typename std::enable_if<!std::is_array<T>::value,
int>::type = 0>
273 return std::move(StdUniquePtr<T>(DiarkisNew<T>(std::forward<_Types>(_Args)...)));
276 template <class T, typename std::enable_if<std::is_array<T>::value,
int>::type = 0>
279 return std::move(StdUniquePtr<T>(DiarkisNew<T>(N)));
282 template<
class T,
class... _Types>
285 std::allocator<T> alloc;
286 return std::allocate_shared<T>(alloc, std::forward<_Types>(_Args)...);
290 using StdVector = std::vector<T>;
293 using StdBasicString = std::basic_string<T, std::char_traits<T>>;
295 using StdString = StdBasicString<char>;
314 template <
typename T>
317 std::size_t
operator()(
const T& obj)
const noexcept
320 auto hashfn = std::hash<T> {};
326 struct StdStringHash<StdString>
328 std::size_t
operator()(
const StdString& s)
const noexcept
330 return hash_bytes(s.data(), s.size());
338 using StdQueue = std::queue<T, StdDeque<T>>;
340 template<
class Key,
class Value>
341 using StdMap = std::map<Key, Value, std::less<Key>>;
343 template<
class Key,
class Value>
344 using StdUnorderedMap = std::unordered_map<Key, Value, StdStringHash<Key>, std::equal_to<Key>>;
354 inline StdString
to_string(
long long value)
358 inline StdString
to_string(
unsigned value)
362 inline StdString
to_string(
unsigned long value)
366 inline StdString
to_string(
unsigned long long value)
378 inline StdString
to_string(
long double value)
384 using StdUniquePtr = std::unique_ptr<T, Diarkis::DiarkisAllocatorDeleter<T>>;
386 template <
class T,
class... _Types,
typename std::enable_if<!std::is_array<T>::value,
int>::type = 0>
389 return std::move(
StdUniquePtr<T>(DiarkisNew<T>(std::forward<_Types>(_Args)...)));
392 template <class T, typename std::enable_if<std::is_array<T>::value,
int>::type = 0>
398 template<
class T,
class... _Types>
402 return std::allocate_shared<T>(alloc, std::forward<_Types>(_Args)...);
406 class StdVector :
public std::vector<T, DiarkisStdAllocator<T>>
413 template <
class _Iter>
420 std::vector<T, DiarkisStdAllocator<T>>::operator=(data);
425 inline operator std::vector<T>()
const
432 class StdString :
public std::basic_string<char, std::char_traits<char>, DiarkisStdAllocator<char>>
443 template <
class _Iter>
459 inline operator std::string()
const
505 template <
typename T>
511 auto hashfn = std::hash<T> {};
521 return hash_bytes(s.data(), s.size());
526 using StdDeque = std::deque<T, Diarkis::DiarkisStdAllocator<T>>;
531 template<
class Key,
class Value>
534 template<
class Key,
class Value>
539 char buf[
sizeof(int) * 4];
540 snprintf(buf,
sizeof(buf),
"%d", value);
545 char buf[
sizeof(long) * 4];
546 snprintf(buf,
sizeof(buf),
"%ld", value);
551 char buf[
sizeof(
long long) * 4];
552 snprintf(buf,
sizeof(buf),
"%lld", value);
557 char buf[
sizeof(unsigned) * 4];
558 snprintf(buf,
sizeof(buf),
"%u", value);
563 char buf[
sizeof(
unsigned long) * 4];
564 snprintf(buf,
sizeof(buf),
"%lu", value);
569 char buf[
sizeof(
unsigned long long) * 4];
570 snprintf(buf,
sizeof(buf),
"%llu", value);
575 char buf[
sizeof(float) * 4];
576 snprintf(buf,
sizeof(buf),
"%f", value);
581 char buf[
sizeof(double) * 4];
582 snprintf(buf,
sizeof(buf),
"%f", value);
587 char buf[
sizeof(
long double) * 4];
588 snprintf(buf,
sizeof(buf),
"%Lf", value);
591 #endif // ENABLE_CUSTOM_ALLOCATOR
603 #ifndef DIARKIS_DISABLE_CUSTOM_ALLOCATOR_FOR_STD_LIBS
612 return hash_bytes(str.data(), str.length());;