123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- namespace UTJ.FbxExporter
- {
- public class PinnedList<T> : IDisposable, IEnumerable<T>, IEnumerable
- {
- public PinnedList(int size = 0)
- {
- this.m_data = new T[size];
- this.m_list = PinnedList<T>.ListCreateIntrusive(this.m_data);
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- public PinnedList(T[] data, bool clone = false)
- {
- this.m_data = ((!clone) ? data : ((T[])data.Clone()));
- this.m_list = PinnedList<T>.ListCreateIntrusive(this.m_data);
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- public PinnedList(List<T> data, bool clone = false)
- {
- this.m_list = ((!clone) ? data : new List<T>(data));
- this.m_data = PinnedList<T>.ListGetInternalArray(this.m_list);
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- public static T[] ListGetInternalArray(List<T> list)
- {
- PinnedList<T>.Caster caster = default(PinnedList<T>.Caster);
- caster.list = list;
- return caster.data.items;
- }
- public static List<T> ListCreateIntrusive(T[] data)
- {
- List<T> list = new List<T>();
- PinnedList<T>.Caster caster = default(PinnedList<T>.Caster);
- caster.list = list;
- caster.data.items = data;
- caster.data.size = data.Length;
- return list;
- }
- public static void ListSetCount(List<T> list, int count)
- {
- PinnedList<T>.Caster caster = default(PinnedList<T>.Caster);
- caster.list = list;
- caster.data.size = count;
- }
- public int Capacity
- {
- get
- {
- return this.m_data.Length;
- }
- }
- public int Count
- {
- get
- {
- return this.m_list.Count;
- }
- }
- public T this[int i]
- {
- get
- {
- return this.m_data[i];
- }
- set
- {
- this.m_data[i] = value;
- }
- }
- public T[] Array
- {
- get
- {
- return this.m_data;
- }
- }
- public List<T> List
- {
- get
- {
- return this.m_list;
- }
- }
- public IntPtr Pointer
- {
- get
- {
- return (this.Count != 0) ? this.m_gch.AddrOfPinnedObject() : IntPtr.Zero;
- }
- }
- public void LockList(Action<List<T>> body)
- {
- if (this.m_gch.IsAllocated)
- {
- this.m_gch.Free();
- }
- body(this.m_list);
- this.m_data = PinnedList<T>.ListGetInternalArray(this.m_list);
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- public void Resize(int size)
- {
- if (size > this.m_data.Length)
- {
- this.LockList(delegate(List<T> l)
- {
- l.Capacity = size;
- });
- }
- PinnedList<T>.ListSetCount(this.m_list, size);
- }
- public void ResizeDiscard(int size)
- {
- if (size > this.m_data.Length)
- {
- if (this.m_gch.IsAllocated)
- {
- this.m_gch.Free();
- }
- this.m_data = new T[size];
- this.m_list = PinnedList<T>.ListCreateIntrusive(this.m_data);
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- else
- {
- PinnedList<T>.ListSetCount(this.m_list, size);
- }
- }
- public void Clear()
- {
- if (this.m_data.Length > 0)
- {
- PinnedList<T>.ListSetCount(this.m_list, 0);
- }
- }
- public PinnedList<T> Clone()
- {
- return new PinnedList<T>(this.m_list, true);
- }
- public void Assign(T[] source)
- {
- this.ResizeDiscard(source.Length);
- System.Array.Copy(source, this.m_data, source.Length);
- }
- public void Assign(List<T> sourceList)
- {
- T[] sourceArray = PinnedList<T>.ListGetInternalArray(sourceList);
- int count = sourceList.Count;
- this.ResizeDiscard(count);
- System.Array.Copy(sourceArray, this.m_data, count);
- }
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing && this.m_gch.IsAllocated)
- {
- this.m_gch.Free();
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- return (IEnumerator<T>)this.m_data.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- public static implicit operator IntPtr(PinnedList<T> v)
- {
- return (v != null) ? v.Pointer : IntPtr.Zero;
- }
- public static implicit operator T[](PinnedList<T> v)
- {
- return (v != null) ? v.Array : null;
- }
- public static implicit operator List<T>(PinnedList<T> v)
- {
- return (v != null) ? v.List : null;
- }
- private List<T> m_list;
- private T[] m_data;
- private GCHandle m_gch;
- private class ListData
- {
- public T[] items;
- public int size;
- }
- [StructLayout(LayoutKind.Explicit)]
- private struct Caster
- {
- [FieldOffset(0)]
- public List<T> list;
- [FieldOffset(0)]
- public PinnedList<T>.ListData data;
- }
- }
- }
|