Mirror of Svelto.ECS because we're a fan of it
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

169 Zeilen
4.1KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Threading;
  4. using Svelto.Common;
  5. namespace Svelto.ECS.DataStructures
  6. {
  7. public struct SharedNative<T> : IDisposable where T : struct, IDisposable
  8. {
  9. #if UNITY_COLLECTIONS || (UNITY_JOBS || UNITY_BURST)
  10. [global::Unity.Collections.LowLevel.Unsafe.NativeDisableUnsafePtrRestriction]
  11. #endif
  12. unsafe IntPtr ptr;
  13. public SharedNative(in T value)
  14. {
  15. unsafe
  16. {
  17. ptr = MemoryUtilities.Alloc<T>(1, Allocator.Persistent);
  18. Unsafe.Write((void*)ptr, value);
  19. }
  20. }
  21. public void Dispose()
  22. {
  23. unsafe
  24. {
  25. Unsafe.AsRef<T>((void*)ptr).Dispose();
  26. MemoryUtilities.Free((IntPtr)ptr, Allocator.Persistent);
  27. ptr = IntPtr.Zero;
  28. }
  29. }
  30. public ref T value
  31. {
  32. get
  33. {
  34. unsafe
  35. {
  36. DBC.ECS.Check.Require(ptr != null, "SharedNative has not been initialized");
  37. return ref Unsafe.AsRef<T>((void*)ptr);
  38. }
  39. }
  40. }
  41. }
  42. public struct SharedNativeInt: IDisposable
  43. {
  44. #if UNITY_COLLECTIONS || (UNITY_JOBS || UNITY_BURST)
  45. [global::Unity.Collections.LowLevel.Unsafe.NativeDisableUnsafePtrRestriction]
  46. #endif
  47. unsafe int* data;
  48. Allocator _allocator;
  49. public SharedNativeInt(Allocator allocator)
  50. {
  51. unsafe
  52. {
  53. _allocator = allocator;
  54. data = (int*) MemoryUtilities.Alloc(sizeof(int), allocator);
  55. }
  56. }
  57. public static SharedNativeInt Create(int t, Allocator allocator)
  58. {
  59. unsafe
  60. {
  61. var current = new SharedNativeInt();
  62. current._allocator = allocator;
  63. current.data = (int*) MemoryUtilities.Alloc(sizeof(int), allocator);
  64. *current.data = t;
  65. return current;
  66. }
  67. }
  68. public static implicit operator int(SharedNativeInt t)
  69. {
  70. unsafe
  71. {
  72. #if DEBUG && !PROFILE_SVELTO
  73. if (t.data == null)
  74. throw new Exception("using disposed SharedInt");
  75. #endif
  76. return *t.data;
  77. }
  78. }
  79. public void Dispose()
  80. {
  81. unsafe
  82. {
  83. if (data != null)
  84. {
  85. MemoryUtilities.Free((IntPtr) data, _allocator);
  86. data = null;
  87. }
  88. }
  89. }
  90. public int Decrement()
  91. {
  92. unsafe
  93. {
  94. #if DEBUG && !PROFILE_SVELTO
  95. if (data == null)
  96. throw new Exception("null-access");
  97. #endif
  98. return Interlocked.Decrement(ref *data);
  99. }
  100. }
  101. public int Increment()
  102. {
  103. unsafe
  104. {
  105. #if DEBUG && !PROFILE_SVELTO
  106. if (data == null)
  107. throw new Exception("null-access");
  108. #endif
  109. return Interlocked.Increment(ref *data);
  110. }
  111. }
  112. public int Add(int val)
  113. {
  114. unsafe
  115. {
  116. #if DEBUG && !PROFILE_SVELTO
  117. if (data == null)
  118. throw new Exception("null-access");
  119. #endif
  120. return Interlocked.Add(ref *data, val);
  121. }
  122. }
  123. public int CompareExchange(int value, int compare)
  124. {
  125. unsafe
  126. {
  127. #if DEBUG && !PROFILE_SVELTO
  128. if (data == null)
  129. throw new Exception("null-access");
  130. #endif
  131. return Interlocked.CompareExchange(ref *data, value, compare);
  132. }
  133. }
  134. public void Set(int val)
  135. {
  136. unsafe
  137. {
  138. #if DEBUG && !PROFILE_SVELTO
  139. if (data == null)
  140. throw new Exception("null-access");
  141. #endif
  142. Volatile.Write(ref *data, val);
  143. }
  144. }
  145. }
  146. }