Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
STLAllocator.h
Go to the documentation of this file.
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
8
10template <class T> struct AllocatorHasReallocate { static constexpr bool sValue = false; };
11
12#ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
13
15template <typename T>
17{
18public:
19 using value_type = T;
20
22 using pointer = T *;
23 using const_pointer = const T *;
24
27 using reference = T &;
28 using const_reference = const T &;
29
30 using size_type = size_t;
31 using difference_type = ptrdiff_t;
32
34 using is_always_equal = std::true_type;
35
38
40 inline STLAllocator() = default;
41
43 template <typename T2>
44 inline STLAllocator(const STLAllocator<T2> &) { }
45
47 static constexpr bool needs_aligned_allocate = alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
48
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
59 static constexpr bool has_reallocate = std::is_trivially_copyable<T>() && !needs_aligned_allocate;
60
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
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
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
90 template <typename T2>
91 struct rebind
92 {
94 };
95};
96
98template <class T> struct AllocatorHasReallocate<STLAllocator<T>> { static constexpr bool sValue = STLAllocator<T>::has_reallocate; };
99
100#else
101
102template <typename T> using STLAllocator = std::allocator<T>;
103
104#endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
105
106// Declare STL containers that use our allocator
107using String = std::basic_string<char, std::char_traits<char>, STLAllocator<char>>;
108using IStringStream = std::basic_istringstream<char, std::char_traits<char>, STLAllocator<char>>;
109
111
112#if (!defined(JPH_PLATFORM_WINDOWS) || defined(JPH_COMPILER_MINGW)) && !defined(JPH_DISABLE_CUSTOM_ALLOCATOR)
113
114namespace std
115{
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
#define JPH_NAMESPACE_END
Definition Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition Core.h:372
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
AllocateFunction Allocate
Definition Memory.cpp:68
ReallocateFunction Reallocate
Definition Memory.cpp:69
FreeFunction Free
Definition Memory.cpp:70
AlignedFreeFunction AlignedFree
Definition Memory.cpp:72
AlignedAllocateFunction AlignedAllocate
Definition Memory.cpp:71
std::basic_string< char, std::char_traits< char >, STLAllocator< char > > String
Definition STLAllocator.h:107
std::basic_istringstream< char, std::char_traits< char >, STLAllocator< char > > IStringStream
Definition STLAllocator.h:108
STL allocator that forwards to our allocation functions.
Definition STLAllocator.h:17
STLAllocator()=default
Constructor.
std::true_type propagate_on_container_move_assignment
Allocator supports moving.
Definition STLAllocator.h:37
pointer allocate(size_type inN)
Allocate memory.
Definition STLAllocator.h:50
static constexpr bool needs_aligned_allocate
If this allocator needs to fall back to aligned allocations because the type requires it.
Definition STLAllocator.h:47
static constexpr bool has_reallocate
Should we expose a reallocate function?
Definition STLAllocator.h:59
T * pointer
Pointer to type.
Definition STLAllocator.h:22
const T & const_reference
Definition STLAllocator.h:28
bool operator==(const STLAllocator< T > &) const
Allocators are stateless so assumed to be equal.
Definition STLAllocator.h:79
bool operator!=(const STLAllocator< T > &) const
Definition STLAllocator.h:84
pointer reallocate(pointer inOldPointer, size_type inOldSize, size_type inNewSize)
Reallocate memory.
Definition STLAllocator.h:63
std::true_type is_always_equal
The allocator is stateless.
Definition STLAllocator.h:34
T value_type
Definition STLAllocator.h:19
void deallocate(pointer inPointer, size_type)
Free memory.
Definition STLAllocator.h:70
size_t size_type
Definition STLAllocator.h:30
STLAllocator(const STLAllocator< T2 > &)
Constructor from other allocator.
Definition STLAllocator.h:44
ptrdiff_t difference_type
Definition STLAllocator.h:31
const T * const_pointer
Definition STLAllocator.h:23
T & reference
Definition STLAllocator.h:27
Definition Array.h:575
Default implementation of AllocatorHasReallocate which tells if an allocator has a reallocate functio...
Definition STLAllocator.h:10
static constexpr bool sValue
Definition STLAllocator.h:10
Converting to allocator for other type.
Definition STLAllocator.h:92