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
9#ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
10
12template <typename T>
14{
15public:
16 using value_type = T;
17
19 using pointer = T *;
20 using const_pointer = const T *;
21
24 using reference = T &;
25 using const_reference = const T &;
26
27 using size_type = size_t;
28 using difference_type = ptrdiff_t;
29
31 inline STLAllocator() = default;
32
34 template <typename T2>
35 inline STLAllocator(const STLAllocator<T2> &) { }
36
39 {
40 if constexpr (alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16))
41 return pointer(AlignedAllocate(inN * sizeof(value_type), alignof(T)));
42 else
43 return pointer(Allocate(inN * sizeof(value_type)));
44 }
45
47 inline void deallocate(pointer inPointer, size_type)
48 {
49 if constexpr (alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16))
50 AlignedFree(inPointer);
51 else
52 Free(inPointer);
53 }
54
56 inline bool operator == (const STLAllocator<T> &) const
57 {
58 return true;
59 }
60
61 inline bool operator != (const STLAllocator<T> &) const
62 {
63 return false;
64 }
65
67 template <typename T2>
68 struct rebind
69 {
71 };
72};
73
74#else
75
76template <typename T> using STLAllocator = std::allocator<T>;
77
78#endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
79
80// Declare STL containers that use our allocator
81template <class T> using Array = std::vector<T, STLAllocator<T>>;
82using String = std::basic_string<char, std::char_traits<char>, STLAllocator<char>>;
83using IStringStream = std::basic_istringstream<char, std::char_traits<char>, STLAllocator<char>>;
84
86
87#if (!defined(JPH_PLATFORM_WINDOWS) || defined(JPH_COMPILER_MINGW)) && !defined(JPH_DISABLE_CUSTOM_ALLOCATOR)
88
89namespace std
90{
92 template <>
93 struct hash<JPH::String>
94 {
95 inline size_t operator () (const JPH::String &inRHS) const
96 {
97 return hash<string_view> { } (inRHS);
98 }
99 };
100}
101
102#endif // (!JPH_PLATFORM_WINDOWS || JPH_COMPILER_MINGW) && !JPH_DISABLE_CUSTOM_ALLOCATOR
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
AllocateFunction Allocate
Definition: Memory.cpp:59
FreeFunction Free
Definition: Memory.cpp:60
AlignedFreeFunction AlignedFree
Definition: Memory.cpp:62
AlignedAllocateFunction AlignedAllocate
Definition: Memory.cpp:61
std::basic_string< char, std::char_traits< char >, STLAllocator< char > > String
Definition: STLAllocator.h:82
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
std::basic_istringstream< char, std::char_traits< char >, STLAllocator< char > > IStringStream
Definition: STLAllocator.h:83
STL allocator that forwards to our allocation functions.
Definition: STLAllocator.h:14
STLAllocator()=default
Constructor.
pointer allocate(size_type inN)
Allocate memory.
Definition: STLAllocator.h:38
T * pointer
Pointer to type.
Definition: STLAllocator.h:19
const T & const_reference
Definition: STLAllocator.h:25
bool operator==(const STLAllocator< T > &) const
Allocators are stateless so assumed to be equal.
Definition: STLAllocator.h:56
bool operator!=(const STLAllocator< T > &) const
Definition: STLAllocator.h:61
T value_type
Definition: STLAllocator.h:16
void deallocate(pointer inPointer, size_type)
Free memory.
Definition: STLAllocator.h:47
size_t size_type
Definition: STLAllocator.h:27
STLAllocator(const STLAllocator< T2 > &)
Constructor from other allocator.
Definition: STLAllocator.h:35
ptrdiff_t difference_type
Definition: STLAllocator.h:28
const T * const_pointer
Definition: STLAllocator.h:20
T & reference
Definition: STLAllocator.h:24
Definition: Reference.h:204
Converting to allocator for other type.
Definition: STLAllocator.h:69