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 JPH_CRASH; // Out of memory
62 void *address = mBase + mTop;
63 mTop = new_top;
64 return address;
65 }
66 }
67
68 // See: TempAllocator
69 virtual void Free(void *inAddress, uint inSize) override
70 {
71 if (inAddress == nullptr)
72 {
73 JPH_ASSERT(inSize == 0);
74 }
75 else
76 {
77 mTop -= AlignUp(inSize, JPH_RVECTOR_ALIGNMENT);
78 if (mBase + mTop != inAddress)
79 JPH_CRASH; // Freeing in the wrong order
80 }
81 }
82
83 // Check if no allocations have been made
84 bool IsEmpty() const
85 {
86 return mTop == 0;
87 }
88
89private:
90 uint8 * mBase;
91 uint mSize;
92 uint mTop = 0;
93};
94
98{
99public:
101
102 // See: TempAllocator
103 virtual void * Allocate(uint inSize) override
104 {
106 }
107
108 // See: TempAllocator
109 virtual void Free(void *inAddress, [[maybe_unused]] uint inSize) override
110 {
111 AlignedFree(inAddress);
112 }
113};
114
std::uint8_t uint8
Definition: Core.h:427
#define JPH_EXPORT
Definition: Core.h:214
unsigned int uint
Definition: Core.h:426
#define JPH_NAMESPACE_END
Definition: Core.h:354
#define JPH_CRASH
Definition: Core.h:345
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
#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:61
AlignedAllocateFunction AlignedAllocate
Definition: Memory.cpp:60
#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:69
bool IsEmpty() const
Definition: TempAllocator.h:84
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:98
virtual void Free(void *inAddress, uint inSize) override
Frees inSize bytes of memory located at inAddress.
Definition: TempAllocator.h:109
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:103