Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DoubleRewindableAllocators.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine.Assertions;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. namespace Unity.Collections
  6. {
  7. /// <summary>
  8. /// A double rewindable allocators <see cref="RewindableAllocator"/>.
  9. /// </summary>
  10. unsafe public struct DoubleRewindableAllocators : IDisposable
  11. {
  12. RewindableAllocator* Pointer;
  13. AllocatorHelper<RewindableAllocator> UpdateAllocatorHelper0;
  14. AllocatorHelper<RewindableAllocator> UpdateAllocatorHelper1;
  15. /// <summary>
  16. /// Update the double rewindable allocators, switch Pointer to another allocator and rewind the newly switched allocator.
  17. /// </summary>
  18. public void Update()
  19. {
  20. var UpdateAllocator0 = (RewindableAllocator*)UnsafeUtility.AddressOf(ref UpdateAllocatorHelper0.Allocator);
  21. var UpdateAllocator1 = (RewindableAllocator*)UnsafeUtility.AddressOf(ref UpdateAllocatorHelper1.Allocator);
  22. Pointer = (Pointer == UpdateAllocator0) ? UpdateAllocator1 : UpdateAllocator0;
  23. CheckIsCreated();
  24. Allocator.Rewind();
  25. }
  26. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  27. void CheckIsCreated()
  28. {
  29. if (!IsCreated)
  30. {
  31. throw new InvalidOperationException($"DoubleRewindableAllocators is not created.");
  32. }
  33. }
  34. /// <summary>
  35. /// Retrieve the current rewindable allocator.
  36. /// </summary>
  37. /// <value>The Allocator retrieved.</value>
  38. public ref RewindableAllocator Allocator
  39. {
  40. get
  41. {
  42. CheckIsCreated();
  43. return ref UnsafeUtility.AsRef<RewindableAllocator>(Pointer);
  44. }
  45. }
  46. /// <summary>
  47. /// Check whether the double rewindable allocators is created.
  48. /// </summary>
  49. /// <value>True if current allocator is not null, otherwise false.</value>
  50. public bool IsCreated => Pointer != null;
  51. /// <summary>
  52. /// Construct a double rewindable allocators by allocating the allocators from backingAllocator and registering them.
  53. /// </summary>
  54. /// <param name="backingAllocator">Allocator used to allocate the double rewindable allocators.</param>
  55. /// <param name="initialSizeInBytes">The initial capacity of the allocators, in bytes</param>
  56. public DoubleRewindableAllocators(AllocatorManager.AllocatorHandle backingAllocator, int initialSizeInBytes)
  57. {
  58. this = default;
  59. Initialize(backingAllocator, initialSizeInBytes);
  60. }
  61. /// <summary>
  62. /// Initialize a double rewindable allocators by allocating the allocators from backingAllocator and registering them.
  63. /// </summary>
  64. /// <param name="backingAllocator">Allocator used to allocate the double rewindable allocators.</param>
  65. /// <param name="initialSizeInBytes">The initial capacity of the allocators, in bytes</param>
  66. public void Initialize(AllocatorManager.AllocatorHandle backingAllocator, int initialSizeInBytes)
  67. {
  68. UpdateAllocatorHelper0 = new AllocatorHelper<RewindableAllocator>(backingAllocator);
  69. UpdateAllocatorHelper1 = new AllocatorHelper<RewindableAllocator>(backingAllocator);
  70. UpdateAllocatorHelper0.Allocator.Initialize(initialSizeInBytes);
  71. UpdateAllocatorHelper1.Allocator.Initialize(initialSizeInBytes);
  72. Pointer = null;
  73. Update();
  74. }
  75. /// <summary>
  76. /// the double rewindable allocators and unregister it.
  77. /// </summary>
  78. public void Dispose()
  79. {
  80. if (!IsCreated)
  81. {
  82. return;
  83. }
  84. UpdateAllocatorHelper0.Allocator.Dispose();
  85. UpdateAllocatorHelper1.Allocator.Dispose();
  86. UpdateAllocatorHelper0.Dispose();
  87. UpdateAllocatorHelper1.Dispose();
  88. }
  89. internal bool EnableBlockFree
  90. {
  91. get
  92. {
  93. Assert.IsTrue(UpdateAllocatorHelper0.Allocator.EnableBlockFree == UpdateAllocatorHelper1.Allocator.EnableBlockFree);
  94. return UpdateAllocatorHelper0.Allocator.EnableBlockFree;
  95. }
  96. set
  97. {
  98. UpdateAllocatorHelper0.Allocator.EnableBlockFree = value;
  99. UpdateAllocatorHelper1.Allocator.EnableBlockFree = value;
  100. }
  101. }
  102. }
  103. }