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
50 uint32 mNumObjectsAllocated;
51
53 ObjectStorage ** mPages = nullptr;
54
58 alignas(JPH_CACHE_LINE_SIZE) Mutex mPageMutex;
59
61#ifdef JPH_ENABLE_ASSERTS
62 atomic<uint32> mNumFreeObjects;
63#endif // JPH_ENABLE_ASSERTS
64
66 atomic<uint32> mAllocationTag;
67
69 atomic<uint64> mFirstFreeObjectAndTag;
70
72 atomic<uint32> mFirstFreeObjectInNewPage;
73
74public:
76 static const uint32 cInvalidObjectIndex = 0xffffffff;
77
79 static const int ObjectStorageSize = sizeof(ObjectStorage);
80
82 inline ~FixedSizeFreeList();
83
85 inline void Init(uint inMaxObjects, uint inPageSize);
86
88 template <typename... Parameters>
89 inline uint32 ConstructObject(Parameters &&... inParameters);
90
92 inline void DestructObject(uint32 inObjectIndex);
93
95 inline void DestructObject(Object *inObject);
96
98 struct Batch
99 {
103 };
104
108 inline void AddObjectToBatch(Batch &ioBatch, uint32 inObjectIndex);
109
111 inline void DestructObjectBatch(Batch &ioBatch);
112
114 inline Object & Get(uint32 inObjectIndex) { return GetStorage(inObjectIndex).mObject; }
115
117 inline const Object & Get(uint32 inObjectIndex) const { return GetStorage(inObjectIndex).mObject; }
118};
119
121
122#include "FixedSizeFreeList.inl"
#define JPH_CACHE_LINE_SIZE
Definition: Core.h:492
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
@ 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:79
~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:76
Object & Get(uint32 inObjectIndex)
Access an object by index.
Definition: FixedSizeFreeList.h:114
const Object & Get(uint32 inObjectIndex) const
Access an object by index.
Definition: FixedSizeFreeList.h:117
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:99
uint32 mFirstObjectIndex
Definition: FixedSizeFreeList.h:100
uint32 mNumObjects
Definition: FixedSizeFreeList.h:102
uint32 mLastObjectIndex
Definition: FixedSizeFreeList.h:101