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
89 // Check if no allocations have been made
90 bool IsEmpty() const
91 {
92 return mTop == 0;
93 }
94
95private:
96 uint8 * mBase;
97 uint mSize;
98 uint mTop = 0;
99};
100
104{
105public:
107
108 // See: TempAllocator
109 virtual void * Allocate(uint inSize) override
110 {
112 }
113
114 // See: TempAllocator
115 virtual void Free(void *inAddress, [[maybe_unused]] uint inSize) override
116 {
117 AlignedFree(inAddress);
118 }
119};
120
std::uint8_t uint8
Definition: Core.h:440
#define JPH_EXPORT
Definition: Core.h:227
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
TraceFunction Trace
Definition: IssueReporting.cpp:18
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
T AlignUp(T inV, uint64 inAlignment)
Align inV up to the next inAlignment bytes.
Definition: Math.h:80
AlignedFreeFunction AlignedFree
Definition: Memory.cpp:62
AlignedAllocateFunction AlignedAllocate
Definition: Memory.cpp:61
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
#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
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 IsEmpty() const
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
Definition: TempAllocator.h:104
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition: TempAllocator.h:115
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:109