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 uint alignment_size = max(JPH_VECTOR_ALIGNMENT, JPH_RVECTOR_ALIGNMENT);
22
24 static constexpr bool needs_aligned_allocate = alignment_size > JPH_DEFAULT_ALLOCATE_ALIGNMENT;
25
27 virtual ~TempAllocator() = default;
28
30 virtual void * Allocate(uint inSize) = 0;
31
33 virtual void Free(void *inAddress, uint inSize) = 0;
34};
35
38{
39public:
41
43 explicit TempAllocatorImpl(size_t inSize) : mSize(inSize)
44 {
45 if constexpr (needs_aligned_allocate)
46 mBase = static_cast<uint8 *>(AlignedAllocate(inSize, alignment_size));
47 else
48 mBase = static_cast<uint8 *>(JPH::Allocate(inSize));
49 }
50
52 virtual ~TempAllocatorImpl() override
53 {
54 JPH_ASSERT(mTop == 0);
55 if constexpr (needs_aligned_allocate)
56 AlignedFree(mBase);
57 else
58 JPH::Free(mBase);
59 }
60
61 // See: TempAllocator
62 virtual void * Allocate(uint inSize) override
63 {
64 if (inSize == 0)
65 {
66 return nullptr;
67 }
68 else
69 {
70 size_t new_top = mTop + AlignUp(inSize, alignment_size);
71 if (new_top > mSize)
72 {
73 Trace("TempAllocator: Out of memory trying to allocate %u bytes", inSize);
74 std::abort();
75 }
76 void *address = mBase + mTop;
77 mTop = new_top;
78 return address;
79 }
80 }
81
82 // See: TempAllocator
83 virtual void Free(void *inAddress, uint inSize) override
84 {
85 if (inAddress == nullptr)
86 {
87 JPH_ASSERT(inSize == 0);
88 }
89 else
90 {
91 mTop -= AlignUp(inSize, alignment_size);
92 if (mBase + mTop != inAddress)
93 {
94 Trace("TempAllocator: Freeing in the wrong order");
95 std::abort();
96 }
97 }
98 }
99
101 bool IsEmpty() const
102 {
103 return mTop == 0;
104 }
105
107 size_t GetSize() const
108 {
109 return mSize;
110 }
111
113 size_t GetUsage() const
114 {
115 return mTop;
116 }
117
119 bool CanAllocate(uint inSize) const
120 {
121 return mTop + AlignUp(inSize, alignment_size) <= mSize;
122 }
123
125 bool OwnsMemory(const void *inAddress) const
126 {
127 return inAddress >= mBase && inAddress < mBase + mSize;
128 }
129
130private:
131 uint8 * mBase;
132 size_t mSize;
133 size_t mTop = 0;
134};
135
139{
140public:
142
143 // See: TempAllocator
144 virtual void * Allocate(uint inSize) override
145 {
146 if (inSize > 0)
147 {
148 if constexpr (needs_aligned_allocate)
149 return AlignedAllocate(inSize, alignment_size);
150 else
151 return JPH::Allocate(inSize);
152 }
153 else
154 return nullptr;
155 }
156
157 // See: TempAllocator
158 virtual void Free(void *inAddress, [[maybe_unused]] uint inSize) override
159 {
160 if (inAddress != nullptr)
161 {
162 if constexpr (needs_aligned_allocate)
163 AlignedFree(inAddress);
164 else
165 JPH::Free(inAddress);
166 }
167 }
168};
169
172{
173public:
175
178 mAllocator(inSize)
179 {
180 }
181
182 // See: TempAllocator
183 virtual void * Allocate(uint inSize) override
184 {
185 if (mAllocator.CanAllocate(inSize))
186 return mAllocator.Allocate(inSize);
187 else
188 return mFallbackAllocator.Allocate(inSize);
189 }
190
191 // See: TempAllocator
192 virtual void Free(void *inAddress, uint inSize) override
193 {
194 if (inAddress == nullptr)
195 {
196 JPH_ASSERT(inSize == 0);
197 }
198 else
199 {
200 if (mAllocator.OwnsMemory(inAddress))
201 mAllocator.Free(inAddress, inSize);
202 else
203 mFallbackAllocator.Free(inAddress, inSize);
204 }
205 }
206
207private:
208 TempAllocatorImpl mAllocator;
209 TempAllocatorMalloc mFallbackAllocator;
210};
211
std::uint8_t uint8
Definition Core.h:578
#define JPH_DEFAULT_ALLOCATE_ALIGNMENT
Definition Core.h:620
#define JPH_EXPORT
Definition Core.h:310
unsigned int uint
Definition Core.h:577
#define JPH_NAMESPACE_END
Definition Core.h:494
#define JPH_NAMESPACE_BEGIN
Definition Core.h:488
TraceFunction Trace
Definition IssueReporting.cpp:16
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
T AlignUp(T inV, uint64 inAlignment)
Align inV up to the next inAlignment bytes.
Definition Math.h:168
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:51
#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 alignment_size byte aligned.
virtual ~TempAllocator()=default
Destructor.
Default implementation of the temp allocator that allocates a large block through malloc upfront.
Definition TempAllocator.h:38
size_t GetUsage() const
Get current usage in bytes of the buffer.
Definition TempAllocator.h:113
size_t GetSize() const
Get the total size of the fixed buffer.
Definition TempAllocator.h:107
bool OwnsMemory(const void *inAddress) const
Check if memory block at inAddress is owned by this allocator.
Definition TempAllocator.h:125
virtual ~TempAllocatorImpl() override
Destructor, frees the block.
Definition TempAllocator.h:52
JPH_OVERRIDE_NEW_DELETE TempAllocatorImpl(size_t inSize)
Constructs the allocator with a maximum allocatable size of inSize.
Definition TempAllocator.h:43
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:83
bool CanAllocate(uint inSize) const
Check if an allocation of inSize can be made in this fixed buffer allocator.
Definition TempAllocator.h:119
bool IsEmpty() const
Check if no allocations have been made.
Definition TempAllocator.h:101
virtual void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be alignment_size byte aligned.
Definition TempAllocator.h:62
Implementation of the TempAllocator that tries to allocate from a large preallocated block,...
Definition TempAllocator.h:172
JPH_OVERRIDE_NEW_DELETE TempAllocatorImplWithMallocFallback(uint inSize)
Constructs the allocator with an initial fixed block if inSize.
Definition TempAllocator.h:177
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:192
virtual void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be alignment_size byte aligned.
Definition TempAllocator.h:183
Definition TempAllocator.h:139
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:158
virtual JPH_OVERRIDE_NEW_DELETE void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be alignment_size byte aligned.
Definition TempAllocator.h:144