Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
FixedSizeFreeList.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#include <Jolt/Core/Mutex.h>
9#include <Jolt/Core/Atomics.h>
10
12
16template <typename Object>
18{
19private:
21 struct ObjectStorage
22 {
24 Object mObject;
25
28 atomic<uint32> mNextFreeObject;
29 };
30
31 static_assert(alignof(ObjectStorage) == alignof(Object), "Object not properly aligned");
32
34 const ObjectStorage & GetStorage(uint32 inObjectIndex) const { return mPages[inObjectIndex >> mPageShift][inObjectIndex & mObjectMask]; }
35 ObjectStorage & GetStorage(uint32 inObjectIndex) { return mPages[inObjectIndex >> mPageShift][inObjectIndex & mObjectMask]; }
36
38#ifdef JPH_ENABLE_ASSERTS
39 atomic<uint32> mNumFreeObjects;
40#endif // JPH_ENABLE_ASSERTS
41
43 atomic<uint32> mAllocationTag;
44
46 atomic<uint64> mFirstFreeObjectAndTag;
47
49 uint32 mPageSize;
50
52 uint32 mPageShift;
53
55 uint32 mObjectMask;
56
58 uint32 mNumPages;
59
61 uint32 mNumObjectsAllocated;
62
64 atomic<uint32> mFirstFreeObjectInNewPage;
65
67 ObjectStorage ** mPages = nullptr;
68
70 Mutex mPageMutex;
71
72public:
74 static const uint32 cInvalidObjectIndex = 0xffffffff;
75
77 static const int ObjectStorageSize = sizeof(ObjectStorage);
78
80 inline ~FixedSizeFreeList();
81
83 inline void Init(uint inMaxObjects, uint inPageSize);
84
86 template <typename... Parameters>
87 inline uint32 ConstructObject(Parameters &&... inParameters);
88
90 inline void DestructObject(uint32 inObjectIndex);
91
93 inline void DestructObject(Object *inObject);
94
96 struct Batch
97 {
101 };
102
106 inline void AddObjectToBatch(Batch &ioBatch, uint32 inObjectIndex);
107
109 inline void DestructObjectBatch(Batch &ioBatch);
110
112 inline Object & Get(uint32 inObjectIndex) { return GetStorage(inObjectIndex).mObject; }
113
115 inline const Object & Get(uint32 inObjectIndex) const { return GetStorage(inObjectIndex).mObject; }
116};
117
119
120#include "FixedSizeFreeList.inl"
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
@ Object
Start of a new object.
Definition: FixedSizeFreeList.h:18
void Init(uint inMaxObjects, uint inPageSize)
Initialize the free list, up to inMaxObjects can be allocated.
Definition: FixedSizeFreeList.inl:25
void DestructObjectBatch(Batch &ioBatch)
Lockless destruct batch of objects.
Definition: FixedSizeFreeList.inl:124
uint32 ConstructObject(Parameters &&... inParameters)
Lockless construct a new object, inParameters are passed on to the constructor.
Definition: FixedSizeFreeList.inl:54
static const int ObjectStorageSize
Size of an object + bookkeeping for the freelist.
Definition: FixedSizeFreeList.h:77
~FixedSizeFreeList()
Destructor.
Definition: FixedSizeFreeList.inl:8
void DestructObject(uint32 inObjectIndex)
Lockless destruct an object and return it to the free pool.
Definition: FixedSizeFreeList.inl:172
static const uint32 cInvalidObjectIndex
Invalid index.
Definition: FixedSizeFreeList.h:74
Object & Get(uint32 inObjectIndex)
Access an object by index.
Definition: FixedSizeFreeList.h:112
const Object & Get(uint32 inObjectIndex) const
Access an object by index.
Definition: FixedSizeFreeList.h:115
void AddObjectToBatch(Batch &ioBatch, uint32 inObjectIndex)
Definition: FixedSizeFreeList.inl:109
Definition: Mutex.h:122
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
A batch of objects that can be destructed.
Definition: FixedSizeFreeList.h:97
uint32 mFirstObjectIndex
Definition: FixedSizeFreeList.h:98
uint32 mNumObjects
Definition: FixedSizeFreeList.h:100
uint32 mLastObjectIndex
Definition: FixedSizeFreeList.h:99