Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
TempAllocator.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
10
16{
17public:
19
21 static constexpr bool needs_aligned_allocate = JPH_RVECTOR_ALIGNMENT > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
22
24 virtual ~TempAllocator() = default;
25
27 virtual void * Allocate(uint inSize) = 0;
28
30 virtual void Free(void *inAddress, uint inSize) = 0;
31};
32
35{
36public:
38
40 explicit TempAllocatorImpl(size_t inSize) : mSize(inSize)
41 {
42 if constexpr (needs_aligned_allocate)
43 mBase = static_cast<uint8 *>(AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT));
44 else
45 mBase = static_cast<uint8 *>(JPH::Allocate(inSize));
46 }
47
49 virtual ~TempAllocatorImpl() override
50 {
51 JPH_ASSERT(mTop == 0);
52 if constexpr (needs_aligned_allocate)
53 AlignedFree(mBase);
54 else
55 JPH::Free(mBase);
56 }
57
58 // See: TempAllocator
59 virtual void * Allocate(uint inSize) override
60 {
61 if (inSize == 0)
62 {
63 return nullptr;
64 }
65 else
66 {
67 size_t new_top = mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT);
68 if (new_top > mSize)
69 {
70 Trace("TempAllocator: Out of memory trying to allocate %u bytes", inSize);
71 std::abort();
72 }
73 void *address = mBase + mTop;
74 mTop = new_top;
75 return address;
76 }
77 }
78
79 // See: TempAllocator
80 virtual void Free(void *inAddress, uint inSize) override
81 {
82 if (inAddress == nullptr)
83 {
84 JPH_ASSERT(inSize == 0);
85 }
86 else
87 {
88 mTop -= AlignUp(inSize, JPH_RVECTOR_ALIGNMENT);
89 if (mBase + mTop != inAddress)
90 {
91 Trace("TempAllocator: Freeing in the wrong order");
92 std::abort();
93 }
94 }
95 }
96
98 bool IsEmpty() const
99 {
100 return mTop == 0;
101 }
102
104 size_t GetSize() const
105 {
106 return mSize;
107 }
108
110 size_t GetUsage() const
111 {
112 return mTop;
113 }
114
116 bool CanAllocate(uint inSize) const
117 {
118 return mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT) <= mSize;
119 }
120
122 bool OwnsMemory(const void *inAddress) const
123 {
124 return inAddress >= mBase && inAddress < mBase + mSize;
125 }
126
127private:
128 uint8 * mBase;
129 size_t mSize;
130 size_t mTop = 0;
131};
132
136{
137public:
139
140 // See: TempAllocator
141 virtual void * Allocate(uint inSize) override
142 {
143 if (inSize > 0)
144 {
145 if constexpr (needs_aligned_allocate)
147 else
148 return JPH::Allocate(inSize);
149 }
150 else
151 return nullptr;
152 }
153
154 // See: TempAllocator
155 virtual void Free(void *inAddress, [[maybe_unused]] uint inSize) override
156 {
157 if (inAddress != nullptr)
158 {
159 if constexpr (needs_aligned_allocate)
160 AlignedFree(inAddress);
161 else
162 JPH::Free(inAddress);
163 }
164 }
165};
166
169{
170public:
172
175 mAllocator(inSize)
176 {
177 }
178
179 // See: TempAllocator
180 virtual void * Allocate(uint inSize) override
181 {
182 if (mAllocator.CanAllocate(inSize))
183 return mAllocator.Allocate(inSize);
184 else
185 return mFallbackAllocator.Allocate(inSize);
186 }
187
188 // See: TempAllocator
189 virtual void Free(void *inAddress, uint inSize) override
190 {
191 if (inAddress == nullptr)
192 {
193 JPH_ASSERT(inSize == 0);
194 }
195 else
196 {
197 if (mAllocator.OwnsMemory(inAddress))
198 mAllocator.Free(inAddress, inSize);
199 else
200 mFallbackAllocator.Free(inAddress, inSize);
201 }
202 }
203
204private:
205 TempAllocatorImpl mAllocator;
206 TempAllocatorMalloc mFallbackAllocator;
207};
208
std::uint8_t uint8
Definition Core.h:501
#define JPH_EXPORT
Definition Core.h:275
unsigned int uint
Definition Core.h:500
#define JPH_NAMESPACE_END
Definition Core.h:425
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
TraceFunction Trace
Definition IssueReporting.cpp:14
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
T AlignUp(T inV, uint64 inAlignment)
Align inV up to the next inAlignment bytes.
Definition Math.h:83
AlignedFreeFunction AlignedFree
Definition Memory.cpp:72
AlignedAllocateFunction AlignedAllocate
Definition Memory.cpp:71
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:49
#define JPH_RVECTOR_ALIGNMENT
Definition Real.h:34
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Definition TempAllocator.h:16
virtual void Free(void *inAddress, uint inSize)=0
Frees inSize bytes of memory located at inAddress.
virtual void * Allocate(uint inSize)=0
Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned.
virtual ~TempAllocator()=default
Destructor.
Default implementation of the temp allocator that allocates a large block through malloc upfront.
Definition TempAllocator.h:35
size_t GetUsage() const
Get current usage in bytes of the buffer.
Definition TempAllocator.h:110
size_t GetSize() const
Get the total size of the fixed buffer.
Definition TempAllocator.h:104
bool OwnsMemory(const void *inAddress) const
Check if memory block at inAddress is owned by this allocator.
Definition TempAllocator.h:122
virtual ~TempAllocatorImpl() override
Destructor, frees the block.
Definition TempAllocator.h:49
JPH_OVERRIDE_NEW_DELETE TempAllocatorImpl(size_t inSize)
Constructs the allocator with a maximum allocatable size of inSize.
Definition TempAllocator.h:40
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:80
bool CanAllocate(uint inSize) const
Check if an allocation of inSize can be made in this fixed buffer allocator.
Definition TempAllocator.h:116
bool IsEmpty() const
Check if no allocations have been made.
Definition TempAllocator.h:98
virtual void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned.
Definition TempAllocator.h:59
Implementation of the TempAllocator that tries to allocate from a large preallocated block,...
Definition TempAllocator.h:169
JPH_OVERRIDE_NEW_DELETE TempAllocatorImplWithMallocFallback(uint inSize)
Constructs the allocator with an initial fixed block if inSize.
Definition TempAllocator.h:174
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:189
virtual void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned.
Definition TempAllocator.h:180
Definition TempAllocator.h:136
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:155
virtual JPH_OVERRIDE_NEW_DELETE void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned.
Definition TempAllocator.h:141