PinnedList.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. namespace UTJ.FbxExporter
  6. {
  7. public class PinnedList<T> : IDisposable, IEnumerable<T>, IEnumerable
  8. {
  9. public PinnedList(int size = 0)
  10. {
  11. this.m_data = new T[size];
  12. this.m_list = PinnedList<T>.ListCreateIntrusive(this.m_data);
  13. this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
  14. }
  15. public PinnedList(T[] data, bool clone = false)
  16. {
  17. this.m_data = ((!clone) ? data : ((T[])data.Clone()));
  18. this.m_list = PinnedList<T>.ListCreateIntrusive(this.m_data);
  19. this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
  20. }
  21. public PinnedList(List<T> data, bool clone = false)
  22. {
  23. this.m_list = ((!clone) ? data : new List<T>(data));
  24. this.m_data = PinnedList<T>.ListGetInternalArray(this.m_list);
  25. this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
  26. }
  27. public static T[] ListGetInternalArray(List<T> list)
  28. {
  29. PinnedList<T>.Caster caster = default(PinnedList<T>.Caster);
  30. caster.list = list;
  31. return caster.data.items;
  32. }
  33. public static List<T> ListCreateIntrusive(T[] data)
  34. {
  35. List<T> list = new List<T>();
  36. PinnedList<T>.Caster caster = default(PinnedList<T>.Caster);
  37. caster.list = list;
  38. caster.data.items = data;
  39. caster.data.size = data.Length;
  40. return list;
  41. }
  42. public static void ListSetCount(List<T> list, int count)
  43. {
  44. PinnedList<T>.Caster caster = default(PinnedList<T>.Caster);
  45. caster.list = list;
  46. caster.data.size = count;
  47. }
  48. public int Capacity
  49. {
  50. get
  51. {
  52. return this.m_data.Length;
  53. }
  54. }
  55. public int Count
  56. {
  57. get
  58. {
  59. return this.m_list.Count;
  60. }
  61. }
  62. public T this[int i]
  63. {
  64. get
  65. {
  66. return this.m_data[i];
  67. }
  68. set
  69. {
  70. this.m_data[i] = value;
  71. }
  72. }
  73. public T[] Array
  74. {
  75. get
  76. {
  77. return this.m_data;
  78. }
  79. }
  80. public List<T> List
  81. {
  82. get
  83. {
  84. return this.m_list;
  85. }
  86. }
  87. public IntPtr Pointer
  88. {
  89. get
  90. {
  91. return (this.Count != 0) ? this.m_gch.AddrOfPinnedObject() : IntPtr.Zero;
  92. }
  93. }
  94. public void LockList(Action<List<T>> body)
  95. {
  96. if (this.m_gch.IsAllocated)
  97. {
  98. this.m_gch.Free();
  99. }
  100. body(this.m_list);
  101. this.m_data = PinnedList<T>.ListGetInternalArray(this.m_list);
  102. this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
  103. }
  104. public void Resize(int size)
  105. {
  106. if (size > this.m_data.Length)
  107. {
  108. this.LockList(delegate(List<T> l)
  109. {
  110. l.Capacity = size;
  111. });
  112. }
  113. PinnedList<T>.ListSetCount(this.m_list, size);
  114. }
  115. public void ResizeDiscard(int size)
  116. {
  117. if (size > this.m_data.Length)
  118. {
  119. if (this.m_gch.IsAllocated)
  120. {
  121. this.m_gch.Free();
  122. }
  123. this.m_data = new T[size];
  124. this.m_list = PinnedList<T>.ListCreateIntrusive(this.m_data);
  125. this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
  126. }
  127. else
  128. {
  129. PinnedList<T>.ListSetCount(this.m_list, size);
  130. }
  131. }
  132. public void Clear()
  133. {
  134. if (this.m_data.Length > 0)
  135. {
  136. PinnedList<T>.ListSetCount(this.m_list, 0);
  137. }
  138. }
  139. public PinnedList<T> Clone()
  140. {
  141. return new PinnedList<T>(this.m_list, true);
  142. }
  143. public void Assign(T[] source)
  144. {
  145. this.ResizeDiscard(source.Length);
  146. System.Array.Copy(source, this.m_data, source.Length);
  147. }
  148. public void Assign(List<T> sourceList)
  149. {
  150. T[] sourceArray = PinnedList<T>.ListGetInternalArray(sourceList);
  151. int count = sourceList.Count;
  152. this.ResizeDiscard(count);
  153. System.Array.Copy(sourceArray, this.m_data, count);
  154. }
  155. public void Dispose()
  156. {
  157. this.Dispose(true);
  158. GC.SuppressFinalize(this);
  159. }
  160. protected virtual void Dispose(bool disposing)
  161. {
  162. if (disposing && this.m_gch.IsAllocated)
  163. {
  164. this.m_gch.Free();
  165. }
  166. }
  167. public IEnumerator<T> GetEnumerator()
  168. {
  169. return (IEnumerator<T>)this.m_data.GetEnumerator();
  170. }
  171. IEnumerator IEnumerable.GetEnumerator()
  172. {
  173. return this.GetEnumerator();
  174. }
  175. public static implicit operator IntPtr(PinnedList<T> v)
  176. {
  177. return (v != null) ? v.Pointer : IntPtr.Zero;
  178. }
  179. public static implicit operator T[](PinnedList<T> v)
  180. {
  181. return (v != null) ? v.Array : null;
  182. }
  183. public static implicit operator List<T>(PinnedList<T> v)
  184. {
  185. return (v != null) ? v.List : null;
  186. }
  187. private List<T> m_list;
  188. private T[] m_data;
  189. private GCHandle m_gch;
  190. private class ListData
  191. {
  192. public T[] items;
  193. public int size;
  194. }
  195. [StructLayout(LayoutKind.Explicit)]
  196. private struct Caster
  197. {
  198. [FieldOffset(0)]
  199. public List<T> list;
  200. [FieldOffset(0)]
  201. public PinnedList<T>.ListData data;
  202. }
  203. }
  204. }