Mirror of Svelto.ECS because we're a fan of it
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

47 lines
1.2KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. namespace Svelto.ECS.DataStructures
  5. {
  6. public struct SharedDisposableNative<T> : IDisposable where T : unmanaged, IDisposable
  7. {
  8. #if UNITY_COLLECTIONS || (UNITY_JOBS || UNITY_BURST)
  9. [global::Unity.Collections.LowLevel.Unsafe.NativeDisableUnsafePtrRestriction]
  10. #endif
  11. unsafe IntPtr ptr;
  12. public SharedDisposableNative(in T value)
  13. {
  14. unsafe
  15. {
  16. ptr = MemoryUtilities.Alloc<T>(1, Allocator.Persistent);
  17. Unsafe.Write((void*)ptr, value);
  18. }
  19. }
  20. public void Dispose()
  21. {
  22. unsafe
  23. {
  24. Unsafe.AsRef<T>((void*)ptr).Dispose();
  25. MemoryUtilities.Free((IntPtr)ptr, Allocator.Persistent);
  26. ptr = IntPtr.Zero;
  27. }
  28. }
  29. public ref T value
  30. {
  31. get
  32. {
  33. unsafe
  34. {
  35. DBC.ECS.Check.Require(ptr != null, "SharedNative has not been initialized");
  36. return ref Unsafe.AsRef<T>((void*)ptr);
  37. }
  38. }
  39. }
  40. }
  41. }