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 virtual ~TempAllocator() = default;
22
24 virtual void * Allocate(uint inSize) = 0;
25
27 virtual void Free(void *inAddress, uint inSize) = 0;
28};
29
32{
33public:
35
37 explicit TempAllocatorImpl(uint inSize) :
38 mBase(static_cast<uint8 *>(AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT))),
39 mSize(inSize)
40 {
41 }
42
44 virtual ~TempAllocatorImpl() override
45 {
46 JPH_ASSERT(mTop == 0);
47 AlignedFree(mBase);
48 }
49
50 // See: TempAllocator
51 virtual void * Allocate(uint inSize) override
52 {
53 if (inSize == 0)
54 {
55 return nullptr;
56 }
57 else
58 {
59 uint new_top = mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT);
60 if (new_top > mSize)
61 {
62 Trace("TempAllocator: Out of memory");
63 std::abort();
64 }
65 void *address = mBase + mTop;
66 mTop = new_top;
67 return address;
68 }
69 }
70
71 // See: TempAllocator
72 virtual void Free(void *inAddress, uint inSize) override
73 {
74 if (inAddress == nullptr)
75 {
76 JPH_ASSERT(inSize == 0);
77 }
78 else
79 {
80 mTop -= AlignUp(inSize, JPH_RVECTOR_ALIGNMENT);
81 if (mBase + mTop != inAddress)
82 {
83 Trace("TempAllocator: Freeing in the wrong order");
84 std::abort();
85 }
86 }
87 }
88
90 bool IsEmpty() const
91 {
92 return mTop == 0;
93 }
94
96 uint GetSize() const
97 {
98 return mSize;
99 }
100
103 {
104 return mTop;
105 }
106
108 bool CanAllocate(uint inSize) const
109 {
110 return mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT) <= mSize;
111 }
112
114 bool OwnsMemory(const void *inAddress) const
115 {
116 return inAddress >= mBase && inAddress < mBase + mSize;
117 }
118
119private:
120 uint8 * mBase;
121 uint mSize;
122 uint mTop = 0;
123};
124
128{
129public:
131
132 // See: TempAllocator
133 virtual void * Allocate(uint inSize) override
134 {
135 return inSize > 0? AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT) : nullptr;
136 }
137
138 // See: TempAllocator
139 virtual void Free(void *inAddress, [[maybe_unused]] uint inSize) override
140 {
141 if (inAddress != nullptr)
142 AlignedFree(inAddress);
143 }
144};
145
148{
149public:
151
154 mAllocator(inSize)
155 {
156 }
157
158 // See: TempAllocator
159 virtual void * Allocate(uint inSize) override
160 {
161 if (mAllocator.CanAllocate(inSize))
162 return mAllocator.Allocate(inSize);
163 else
164 return mFallbackAllocator.Allocate(inSize);
165 }
166
167 // See: TempAllocator
168 virtual void Free(void *inAddress, uint inSize) override
169 {
170 if (inAddress == nullptr)
171 {
172 JPH_ASSERT(inSize == 0);
173 }
174 else
175 {
176 if (mAllocator.OwnsMemory(inAddress))
177 mAllocator.Free(inAddress, inSize);
178 else
179 mFallbackAllocator.Free(inAddress, inSize);
180 }
181 }
182
183private:
184 TempAllocatorImpl mAllocator;
185 TempAllocatorMalloc mFallbackAllocator;
186};
187
std::uint8_t uint8
Definition Core.h:482
#define JPH_EXPORT
Definition Core.h:271
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
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:31
#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 JPH_OVERRIDE_NEW_DELETE ~TempAllocator()=default
Destructor.
Default implementation of the temp allocator that allocates a large block through malloc upfront.
Definition TempAllocator.h:32
bool OwnsMemory(const void *inAddress) const
Check if memory block at inAddress is owned by this allocator.
Definition TempAllocator.h:114
virtual ~TempAllocatorImpl() override
Destructor, frees the block.
Definition TempAllocator.h:44
JPH_OVERRIDE_NEW_DELETE TempAllocatorImpl(uint inSize)
Constructs the allocator with a maximum allocatable size of inSize.
Definition TempAllocator.h:37
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:72
bool CanAllocate(uint inSize) const
Check if an allocation of inSize can be made in this fixed buffer allocator.
Definition TempAllocator.h:108
uint GetSize() const
Get the total size of the fixed buffer.
Definition TempAllocator.h:96
bool IsEmpty() const
Check if no allocations have been made.
Definition TempAllocator.h:90
virtual void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned.
Definition TempAllocator.h:51
uint GetUsage() const
Get current usage in bytes of the buffer.
Definition TempAllocator.h:102
Implementation of the TempAllocator that tries to allocate from a large preallocated block,...
Definition TempAllocator.h:148
JPH_OVERRIDE_NEW_DELETE TempAllocatorImplWithMallocFallback(uint inSize)
Constructs the allocator with an initial fixed block if inSize.
Definition TempAllocator.h:153
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:168
virtual void * Allocate(uint inSize) override
Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned.
Definition TempAllocator.h:159
Definition TempAllocator.h:128
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition TempAllocator.h:139
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:133