FixedSizeFreeList.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/NonCopyable.h>
  6. #include <Jolt/Core/Mutex.h>
  7. #include <Jolt/Core/Atomics.h>
  8. JPH_NAMESPACE_BEGIN
  9. /// Class that allows lock free creation / destruction of objects (unless a new page of objects needs to be allocated)
  10. /// It contains a fixed pool of objects and also allows batching up a lot of objects to be destroyed
  11. /// and doing the actual free in a single atomic operation
  12. template <typename Object>
  13. class FixedSizeFreeList : public NonCopyable
  14. {
  15. private:
  16. /// Storage type for an Object
  17. struct ObjectStorage
  18. {
  19. /// The object we're storing
  20. Object mObject;
  21. /// When the object is freed (or in the process of being freed as a batch) this will contain the next free object
  22. /// When an object is in use it will contain the object's index in the free list
  23. atomic<uint32> mNextFreeObject;
  24. };
  25. static_assert(alignof(ObjectStorage) == alignof(Object), "Object not properly aligned");
  26. /// Access the object storage given the object index
  27. const ObjectStorage & GetStorage(uint32 inObjectIndex) const { return mPages[inObjectIndex >> mPageShift][inObjectIndex & mObjectMask]; }
  28. ObjectStorage & GetStorage(uint32 inObjectIndex) { return mPages[inObjectIndex >> mPageShift][inObjectIndex & mObjectMask]; }
  29. /// Number of objects that we currently have in the free list / new pages
  30. #ifdef JPH_ENABLE_ASSERTS
  31. atomic<uint32> mNumFreeObjects;
  32. #endif // JPH_ENABLE_ASSERTS
  33. /// Simple counter that makes the first free object pointer update with every CAS so that we don't suffer from the ABA problem
  34. atomic<uint32> mAllocationTag;
  35. /// Index of first free object, the first 32 bits of an object are used to point to the next free object
  36. atomic<uint64> mFirstFreeObjectAndTag;
  37. /// Size (in objects) of a single page
  38. uint32 mPageSize;
  39. /// Number of bits to shift an object index to the right to get the page number
  40. uint32 mPageShift;
  41. /// Mask to and an object index with to get the page number
  42. uint32 mObjectMask;
  43. /// Total number of pages that are usable
  44. uint32 mNumPages;
  45. /// Total number of objects that have been allocated
  46. uint32 mNumObjectsAllocated;
  47. /// The first free object to use when the free list is empty (may need to allocate a new page)
  48. atomic<uint32> mFirstFreeObjectInNewPage;
  49. /// Array of pages of objects
  50. ObjectStorage ** mPages = nullptr;
  51. /// Mutex that is used to allocate a new page if the storage runs out
  52. Mutex mPageMutex;
  53. public:
  54. /// Invalid index
  55. static const uint32 cInvalidObjectIndex = 0xffffffff;
  56. /// Size of an object + bookkeeping for the freelist
  57. static const int ObjectStorageSize = sizeof(ObjectStorage);
  58. /// Destructor
  59. inline ~FixedSizeFreeList();
  60. /// Initialize the free list, up to inMaxObjects can be allocated
  61. inline void Init(uint inMaxObjects, uint inPageSize);
  62. /// Lockless construct a new object, inParameters are passed on to the constructor
  63. template <typename... Parameters>
  64. inline uint32 ConstructObject(Parameters &&... inParameters);
  65. /// Lockless destruct an object and return it to the free pool
  66. inline void DestructObject(uint32 inObjectIndex);
  67. /// Lockless destruct an object and return it to the free pool
  68. inline void DestructObject(Object *inObject);
  69. /// A batch of objects that can be destructed
  70. struct Batch
  71. {
  72. uint32 mFirstObjectIndex = cInvalidObjectIndex;
  73. uint32 mLastObjectIndex = cInvalidObjectIndex;
  74. uint32 mNumObjects = 0;
  75. };
  76. /// Add a object to an existing batch to be destructed.
  77. /// Adding objects to a batch does not destroy or modify the objects, this will merely link them
  78. /// so that the entire batch can be returned to the free list in a single atomic operation
  79. inline void AddObjectToBatch(Batch &ioBatch, uint32 inObjectIndex);
  80. /// Lockless destruct batch of objects
  81. inline void DestructObjectBatch(Batch &ioBatch);
  82. /// Access an object by index.
  83. inline Object & Get(uint32 inObjectIndex) { return GetStorage(inObjectIndex).mObject; }
  84. /// Access an object by index.
  85. inline const Object & Get(uint32 inObjectIndex) const { return GetStorage(inObjectIndex).mObject; }
  86. };
  87. JPH_NAMESPACE_END
  88. #include "FixedSizeFreeList.inl"