Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AllocatorRewindableTests.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #region allocator-rewindable-example
  2. using System;
  3. using NUnit.Framework;
  4. using Unity.Collections;
  5. // This is the example code used in
  6. // Packages/com.unity.collections/Documentation~/allocator/allocator-rewindable.md
  7. // Example user structure
  8. internal struct ExampleStruct
  9. {
  10. // Use AllocatorHelper to help creating a rewindable alloctor
  11. AllocatorHelper<RewindableAllocator> rwdAllocatorHelper;
  12. // Rewindable allocator property for accessibility
  13. public ref RewindableAllocator RwdAllocator => ref rwdAllocatorHelper.Allocator;
  14. // Create the rewindable allocator
  15. void CreateRewindableAllocator(AllocatorManager.AllocatorHandle backgroundAllocator, int initialBlockSize, bool enableBlockFree = false)
  16. {
  17. // Allocate the rewindable allocator from backgroundAllocator and register the allocator
  18. rwdAllocatorHelper = new AllocatorHelper<RewindableAllocator>(backgroundAllocator);
  19. // Allocate the first memory block with initialBlockSize in bytes, and indicate whether
  20. // to enable the rewindable allocator with individual block free through enableBlockFree
  21. RwdAllocator.Initialize(initialBlockSize, enableBlockFree);
  22. }
  23. // Constructor of user structure
  24. public ExampleStruct(int initialBlockSize)
  25. {
  26. this = default;
  27. CreateRewindableAllocator(Allocator.Persistent, initialBlockSize, false);
  28. }
  29. // Dispose the user structure
  30. public void Dispose()
  31. {
  32. DisposeRewindableAllocator();
  33. }
  34. #region allocator-rewindable-use
  35. // Sample code to use rewindable allocator to allocate containers
  36. public unsafe void UseRewindableAllocator(out NativeArray<int> nativeArray, out NativeList<int> nativeList, out byte* bytePtr)
  37. {
  38. // Use rewindable allocator to allocate a native array, no need to dispose the array manually
  39. // CollectionHelper is required to create/allocate native array from a custom allocator.
  40. nativeArray = CollectionHelper.CreateNativeArray<int, RewindableAllocator>(100, ref RwdAllocator);
  41. nativeArray[0] = 0xFE;
  42. // Use rewindable allocator to allocate a native list, do not need to dispose the list manually
  43. nativeList = new NativeList<int>(RwdAllocator.Handle);
  44. for (int i = 0; i < 50; i++)
  45. {
  46. nativeList.Add(i);
  47. }
  48. // Use custom allocator to allocate a byte buffer.
  49. bytePtr = (byte*)AllocatorManager.Allocate(ref RwdAllocator, sizeof(byte), sizeof(byte), 10);
  50. bytePtr[0] = 0xAB;
  51. }
  52. #endregion // allocator-rewindable-use
  53. #region allocator-rewindable-free
  54. // Free all allocations from the rewindable allocator
  55. public void FreeRewindableAllocator()
  56. {
  57. RwdAllocator.Rewind();
  58. }
  59. #endregion // allocator-rewindable-free
  60. #region allocator-rewindable-dispose
  61. // Dispose the rewindable allocator
  62. void DisposeRewindableAllocator()
  63. {
  64. // Dispose all the memory blocks in the rewindable allocator
  65. RwdAllocator.Dispose();
  66. // Unregister the rewindable allocator and dispose it
  67. rwdAllocatorHelper.Dispose();
  68. }
  69. #endregion // allocator-rewindable-dispose
  70. }
  71. internal class ExampleStructSampleUsage
  72. {
  73. // Initial block size of the rewindable allocator.
  74. const int IntialBlockSize = 128 * 1024;
  75. [Test]
  76. public unsafe void UseRewindableAllocator_Works()
  77. {
  78. ExampleStruct exampleStruct = new ExampleStruct(IntialBlockSize);
  79. // Allocate native array and native list from rewindable allocator
  80. exampleStruct.UseRewindableAllocator(out NativeArray<int> nativeArray, out NativeList<int> nativeList, out byte* bytePtr);
  81. // Still able to access the native array, native list and byte buffer
  82. Assert.AreEqual(nativeArray[0], 0xFE);
  83. Assert.AreEqual(nativeList[10], 10);
  84. Assert.AreEqual(bytePtr[0], 0xAB);
  85. // Free all memories allocated from the rewindable allocator
  86. // No need to dispose the native array and native list
  87. exampleStruct.FreeRewindableAllocator();
  88. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  89. // Object disposed exception throws because nativeArray is already disposed
  90. Assert.Throws<ObjectDisposedException>(() =>
  91. {
  92. nativeArray[0] = 0xEF;
  93. });
  94. // Object disposed exception throws because nativeList is already disposed
  95. Assert.Throws<ObjectDisposedException>(() =>
  96. {
  97. nativeList[10] = 0x10;
  98. });
  99. #endif
  100. // Dispose the user structure
  101. exampleStruct.Dispose();
  102. }
  103. }
  104. #endregion // allocator-rewindable-example