Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/STLTempAllocator.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 #include <Jolt/Core/TempAllocator.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 /// STL allocator that wraps around TempAllocator
12 template <typename T>
13 class STLTempAllocator
14 {
15 public:
16 using value_type = T;
17
18 /// Pointer to type
19 using pointer = T *;
20 using const_pointer = const T *;
21
22 /// Reference to type.
23 /// Can be removed in C++20.
24 using reference = T &;
25 using const_reference = const T &;
26
27 using size_type = size_t;
28 using difference_type = ptrdiff_t;
29
30 /// The allocator is not stateless (depends on the temp allocator)
31 using is_always_equal = std::false_type;
32
33 /// Constructor
34 inline STLTempAllocator(TempAllocator &inAllocator) : mAllocator(inAllocator) { }
35
36 /// Constructor from other allocator
37 template <typename T2>
38 inline explicit STLTempAllocator(const STLTempAllocator<T2> &inRHS) : mAllocator(inRHS.GetAllocator()) { }
39
40 /// Allocate memory
41 inline pointer allocate(size_type inN)
42 {
43 return pointer(mAllocator.Allocate(uint(inN * sizeof(value_type))));
44 }
45
46 /// Free memory
47 inline void deallocate(pointer inPointer, size_type inN)
48 {
49 mAllocator.Free(inPointer, uint(inN * sizeof(value_type)));
50 }
51
52 /// Allocators are not-stateless, assume if allocator address matches that the allocators are the same
53 inline bool operator == (const STLTempAllocator<T> &inRHS) const
54 {
55 return &mAllocator == &inRHS.mAllocator;
56 }
57
58 inline bool operator != (const STLTempAllocator<T> &inRHS) const
59 {
60 return &mAllocator != &inRHS.mAllocator;
61 }
62
63 /// Converting to allocator for other type
64 template <typename T2>
65 struct rebind
66 {
67 using other = STLTempAllocator<T2>;
68 };
69
70 /// Get our temp allocator
71 TempAllocator & GetAllocator() const
72 {
73 return mAllocator;
74 }
75
76 private:
77 TempAllocator & mAllocator;
78 };
79
80 JPH_NAMESPACE_END