Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/TempAllocator.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/NonCopyable.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 /// Allocator for temporary allocations.
12 /// This allocator works as a stack: The blocks must always be freed in the reverse order as they are allocated.
13 /// Note that allocations and frees can take place from different threads, but the order is guaranteed though
14 /// job dependencies, so it is not needed to use any form of locking.
15 class JPH_EXPORT TempAllocator : public NonCopyable
16 {
17 public:
18 JPH_OVERRIDE_NEW_DELETE
19
20 /// Destructor
21 virtual ~TempAllocator() = default;
22
23 /// Allocates inSize bytes of memory, returned memory address must be JPH_RVECTOR_ALIGNMENT byte aligned
24 virtual void * Allocate(uint inSize) = 0;
25
26 /// Frees inSize bytes of memory located at inAddress
27 virtual void Free(void *inAddress, uint inSize) = 0;
28 };
29
30 /// Default implementation of the temp allocator that allocates a large block through malloc upfront
31 class JPH_EXPORT TempAllocatorImpl final : public TempAllocator
32 {
33 public:
34 JPH_OVERRIDE_NEW_DELETE
35
36 /// Constructs the allocator with a maximum allocatable size of inSize
37 explicit TempAllocatorImpl(size_t inSize) :
38 mBase(static_cast<uint8 *>(AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT))),
39 mSize(inSize)
40 {
41 }
42
43 /// Destructor, frees the block
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 size_t new_top = mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT);
60 if (new_top > mSize)
61 {
62 Trace("TempAllocator: Out of memory trying to allocate %u bytes", inSize);
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
89 /// Check if no allocations have been made
90 bool IsEmpty() const
91 {
92 return mTop == 0;
93 }
94
95 /// Get the total size of the fixed buffer
96 size_t GetSize() const
97 {
98 return mSize;
99 }
100
101 /// Get current usage in bytes of the buffer
102 size_t GetUsage() const
103 {
104 return mTop;
105 }
106
107 /// Check if an allocation of inSize can be made in this fixed buffer allocator
108 bool CanAllocate(uint inSize) const
109 {
110 return mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT) <= mSize;
111 }
112
113 /// Check if memory block at inAddress is owned by this allocator
114 bool OwnsMemory(const void *inAddress) const
115 {
116 return inAddress >= mBase && inAddress < mBase + mSize;
117 }
118
119 private:
120 uint8 * mBase; ///< Base address of the memory block
121 size_t mSize; ///< Size of the memory block
122 size_t mTop = 0; ///< End of currently allocated area
123 };
124
125 /// Implementation of the TempAllocator that just falls back to malloc/free
126 /// Note: This can be quite slow when running in the debugger as large memory blocks need to be initialized with 0xcd
127 class JPH_EXPORT TempAllocatorMalloc final : public TempAllocator
128 {
129 public:
130 JPH_OVERRIDE_NEW_DELETE
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
146 /// Implementation of the TempAllocator that tries to allocate from a large preallocated block, but falls back to malloc when it is exhausted
147 class JPH_EXPORT TempAllocatorImplWithMallocFallback final : public TempAllocator
148 {
149 public:
150 JPH_OVERRIDE_NEW_DELETE
151
152 /// Constructs the allocator with an initial fixed block if inSize
153 explicit TempAllocatorImplWithMallocFallback(uint inSize) :
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
183 private:
184 TempAllocatorImpl mAllocator;
185 TempAllocatorMalloc mFallbackAllocator;
186 };
187
188 JPH_NAMESPACE_END