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 uint32 mPageSize;
39
41 uint32 mPageShift;
42
44 uint32 mObjectMask;
45
47 uint32 mNumPages;
48
51 atomic<uint32> mNumObjectsAllocated;
52
54 ObjectStorage ** mPages = nullptr;
55
59 alignas(JPH_CACHE_LINE_SIZE) Mutex mPageMutex;
60
62#ifdef JPH_ENABLE_ASSERTS
63 atomic<uint32> mNumFreeObjects;
64#endif // JPH_ENABLE_ASSERTS
65
67 atomic<uint32> mAllocationTag;
68
70 atomic<uint64> mFirstFreeObjectAndTag;
71
73 atomic<uint32> mFirstFreeObjectInNewPage;
74
75public:
77 static const uint32 cInvalidObjectIndex = 0xffffffff;
78
80 static const int ObjectStorageSize = sizeof(ObjectStorage);
81
84
86 inline void Init(uint inMaxObjects, uint inPageSize);
87
89 template <typename... Parameters>
90 inline uint32 ConstructObject(Parameters &&... inParameters);
91
93 inline void DestructObject(uint32 inObjectIndex);
94
96 inline void DestructObject(Object *inObject);
97
105
109 inline void AddObjectToBatch(Batch &ioBatch, uint32 inObjectIndex);
110
112 inline void DestructObjectBatch(Batch &ioBatch);
113
115 inline Object & Get(uint32 inObjectIndex) { return GetStorage(inObjectIndex).mObject; }
116
118 inline const Object & Get(uint32 inObjectIndex) const { return GetStorage(inObjectIndex).mObject; }
119
121 inline uint32 GetObjectIndex(const Object *inObject) const;
122};
123
125
126#include "FixedSizeFreeList.inl"
#define JPH_CACHE_LINE_SIZE
Definition Core.h:613
unsigned int uint
Definition Core.h:564
#define JPH_NAMESPACE_END
Definition Core.h:487
std::uint32_t uint32
Definition Core.h:567
#define JPH_NAMESPACE_BEGIN
Definition Core.h:481
@ Object
Start of a new object.
Definition SerializableAttribute.h:20
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:130
uint32 GetObjectIndex(const Object *inObject) const
Get the index of an object that was created by this free list.
Definition FixedSizeFreeList.inl:210
uint32 ConstructObject(Parameters &&... inParameters)
Lockless construct a new object, inParameters are passed on to the constructor.
Definition FixedSizeFreeList.inl:54
static const int ObjectStorageSize
Definition FixedSizeFreeList.h:80
~FixedSizeFreeList()
Destructor.
Definition FixedSizeFreeList.inl:8
void DestructObject(uint32 inObjectIndex)
Lockless destruct an object and return it to the free pool.
Definition FixedSizeFreeList.inl:178
void DestructObject(Object *inObject)
Lockless destruct an object and return it to the free pool.
Definition FixedSizeFreeList.inl:216
static const uint32 cInvalidObjectIndex
Definition FixedSizeFreeList.h:77
Object & Get(uint32 inObjectIndex)
Access an object by index.
Definition FixedSizeFreeList.h:115
const Object & Get(uint32 inObjectIndex) const
Access an object by index.
Definition FixedSizeFreeList.h:118
void AddObjectToBatch(Batch &ioBatch, uint32 inObjectIndex)
Definition FixedSizeFreeList.inl:111
Definition Mutex.h:124
NonCopyable()=default
A batch of objects that can be destructed.
Definition FixedSizeFreeList.h:100
uint32 mFirstObjectIndex
Definition FixedSizeFreeList.h:101
uint32 mNumObjects
Definition FixedSizeFreeList.h:103
uint32 mLastObjectIndex
Definition FixedSizeFreeList.h:102