2
0

FixedSizeFreeList.h 4.2 KB

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