Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/STLAllocator.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 JPH_NAMESPACE_BEGIN
8
9 /// Default implementation of AllocatorHasReallocate which tells if an allocator has a reallocate function
10 template <class T> struct AllocatorHasReallocate { static constexpr bool sValue = false; };
11
12 #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
13
14 /// STL allocator that forwards to our allocation functions
15 template <typename T>
16 class STLAllocator
17 {
18 public:
19 using value_type = T;
20
21 /// Pointer to type
22 using pointer = T *;
23 using const_pointer = const T *;
24
25 /// Reference to type.
26 /// Can be removed in C++20.
27 using reference = T &;
28 using const_reference = const T &;
29
30 using size_type = size_t;
31 using difference_type = ptrdiff_t;
32
33 /// The allocator is stateless
34 using is_always_equal = std::true_type;
35
36 /// Allocator supports moving
37 using propagate_on_container_move_assignment = std::true_type;
38
39 /// Constructor
40 inline STLAllocator() = default;
41
42 /// Constructor from other allocator
43 template <typename T2>
44 inline STLAllocator(const STLAllocator<T2> &) { }
45
46 /// If this allocator needs to fall back to aligned allocations because the type requires it
47 static constexpr bool needs_aligned_allocate = alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
48
49 /// Allocate memory
50 inline pointer allocate(size_type inN)
51 {
52 if constexpr (needs_aligned_allocate)
53 return pointer(AlignedAllocate(inN * sizeof(value_type), alignof(T)));
54 else
55 return pointer(Allocate(inN * sizeof(value_type)));
56 }
57
58 /// Should we expose a reallocate function?
59 static constexpr bool has_reallocate = std::is_trivially_copyable<T>() && !needs_aligned_allocate;
60
61 /// Reallocate memory
62 template <bool has_reallocate_v = has_reallocate, typename = std::enable_if_t<has_reallocate_v>>
63 inline pointer reallocate(pointer inOldPointer, size_type inOldSize, size_type inNewSize)
64 {
65 JPH_ASSERT(inNewSize > 0); // Reallocating to zero size is implementation dependent, so we don't allow it
66 return pointer(Reallocate(inOldPointer, inOldSize * sizeof(value_type), inNewSize * sizeof(value_type)));
67 }
68
69 /// Free memory
70 inline void deallocate(pointer inPointer, size_type)
71 {
72 if constexpr (needs_aligned_allocate)
73 AlignedFree(inPointer);
74 else
75 Free(inPointer);
76 }
77
78 /// Allocators are stateless so assumed to be equal
79 inline bool operator == (const STLAllocator<T> &) const
80 {
81 return true;
82 }
83
84 inline bool operator != (const STLAllocator<T> &) const
85 {
86 return false;
87 }
88
89 /// Converting to allocator for other type
90 template <typename T2>
91 struct rebind
92 {
93 using other = STLAllocator<T2>;
94 };
95 };
96
97 /// The STLAllocator implements the reallocate function if the alignment of the class is smaller or equal to the default alignment for the platform
98 template <class T> struct AllocatorHasReallocate<STLAllocator<T>> { static constexpr bool sValue = STLAllocator<T>::has_reallocate; };
99
100 #else
101
102 template <typename T> using STLAllocator = std::allocator<T>;
103
104 #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
105
106 // Declare STL containers that use our allocator
107 using String = std::basic_string<char, std::char_traits<char>, STLAllocator<char>>;
108 using IStringStream = std::basic_istringstream<char, std::char_traits<char>, STLAllocator<char>>;
109
110 JPH_NAMESPACE_END
111
112 #if (!defined(JPH_PLATFORM_WINDOWS) || defined(JPH_COMPILER_MINGW)) && !defined(JPH_DISABLE_CUSTOM_ALLOCATOR)
113
114 namespace std
115 {
116 /// Declare std::hash for String, for some reason on Linux based platforms template deduction takes the wrong variant
117 template <>
118 struct hash<JPH::String>
119 {
120 inline size_t operator () (const JPH::String &inRHS) const
121 {
122 return hash<string_view> { } (inRHS);
123 }
124 };
125 }
126
127 #endif // (!JPH_PLATFORM_WINDOWS || JPH_COMPILER_MINGW) && !JPH_DISABLE_CUSTOM_ALLOCATOR