public NativeStringImpl(int capacity, Allocator allocatorLabel) #endif { #if ENABLE_UNITY_COLLECTIONS_CHECKS this.sentinel = sentinel; m_StringData = null; if (!UnsafeUtility.IsBlittable <T>()) { this.sentinel.Dispose(); throw new ArgumentException(string.Format("{0} used in NativeString<{0}> must be blittable", typeof(T))); } #endif m_MemoryAllocator = default(TMemManager); m_StringData = (NativeStringData *)m_MemoryAllocator.Init(UnsafeUtility.SizeOf <NativeStringData>(), UnsafeUtility.AlignOf <NativeStringData>(), allocatorLabel); var elementSize = UnsafeUtility.SizeOf <T>(); //@TODO: Find out why this is needed? capacity = Math.Max(1, capacity); m_StringData->buffer = UnsafeUtility.Malloc(capacity * elementSize, UnsafeUtility.AlignOf <T>(), allocatorLabel); m_StringData->length = 0; m_StringData->capacity = capacity; }
public void Dispose() { if (m_StringData != null) { #if ENABLE_UNITY_COLLECTIONS_CHECKS sentinel.Dispose(); #endif UnsafeUtility.Free(m_StringData->buffer, m_MemoryAllocator.Label); #if ENABLE_UNITY_COLLECTIONS_CHECKS m_StringData->buffer = (void *)0xDEADF00D; #endif m_MemoryAllocator.Dispose(m_StringData); m_StringData = null; } #if ENABLE_UNITY_COLLECTIONS_CHECKS else { throw new Exception("NativeString has yet to be allocated or has been dealocated!"); } #endif }