public static T *AllocArray <T>(int length, Allocator allocator) where T : unmanaged { return((T *)UnsafeUtility.Malloc(length * UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), allocator)); }
public void Init() { m = (float4x4 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <float4x4>() * 10000, UnsafeUtility.AlignOf <float4x4>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { m[i] = float4x4.identity; } }
/// <summary> /// Creates the top-level fields of a single blob asset. /// </summary> /// <remarks> /// This function allocates memory for the top-level fields of a blob asset and returns a reference to it. Use /// this root reference to initialize field values and to allocate memory for arrays and structs. /// </remarks> /// <typeparam name="T">A struct that defines the structure of the blob asset.</typeparam> /// <returns>A reference to the blob data under construction.</returns> public ref T ConstructRoot <T>() where T : struct { var allocation = Allocate(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>()); return(ref UnsafeUtilityEx.AsRef <T>(AllocationToPointer(allocation))); }
public unsafe void ResizeUninitialized(int length) { this.InvalidateArrayAliases(); BufferHeader.EnsureCapacity(this.m_Buffer, length, UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), BufferHeader.TrashMode.RetainOldData); this.m_Buffer.Length = length; }
public void Init() { rng = new Random(1); v1 = (float2*)UnsafeUtility.Malloc(UnsafeUtility.SizeOf<float2>() * iterations, UnsafeUtility.AlignOf<float2>(), Allocator.Persistent); for (int i = 0; i < iterations; ++i) { v1[i] = new float2(1.0f); } v2 = (float2*)UnsafeUtility.Malloc(UnsafeUtility.SizeOf<float2>() * iterations, UnsafeUtility.AlignOf<float2>(), Allocator.Persistent); for (int i = 0; i < iterations; ++i) { v2[i] = new float2(2.0f); } result = (float2*)UnsafeUtility.Malloc(UnsafeUtility.SizeOf<float2>() * iterations, UnsafeUtility.AlignOf<float2>(), Allocator.Persistent); for (int i = 0; i < iterations; ++i) { result[i] = new float2(1.0f); } }
unsafe NativeList(int capacity, Allocator i_label, int stackDepth) { //@TODO: Find out why this is needed? capacity = Math.Max(1, capacity); var totalSize = UnsafeUtility.SizeOf <T>() * (long)capacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS // Native allocation is only valid for Temp, Job and Persistent. if (i_label <= Allocator.None) { throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(i_label)); } if (capacity < 0) { throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be >= 0"); } CollectionHelper.CheckIsUnmanaged <T>(); // Make sure we cannot allocate more than int.MaxValue (2,147,483,647 bytes) // because the underlying UnsafeUtility.Malloc is expecting a int. // TODO: change UnsafeUtility.Malloc to accept a UIntPtr length instead to match C++ API if (totalSize > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(capacity), $"Capacity * sizeof(T) cannot exceed {int.MaxValue} bytes"); } #endif m_Allocator = i_label; m_ListData = (NativeListData *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <NativeListData>(), UnsafeUtility.AlignOf <NativeListData>(), m_Allocator); m_ListData->buffer = UnsafeUtility.Malloc(totalSize, UnsafeUtility.AlignOf <T>(), m_Allocator); m_ListData->length = 0; m_ListData->capacity = capacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, stackDepth, m_Allocator); #endif }
public NativeByteArray(int size, Allocator label) { Length = size; // This check is redundant since we always use an int which is blittable. // It is here as an example of how to check for type correctness for generic types. #if ENABLE_UNITY_COLLECTIONS_CHECKS if (!UnsafeUtility.IsBlittable <byte>()) { throw new ArgumentException(string.Format("{0} used in NativeQueue<{0}> must be blittable", typeof(int))); } #endif m_AllocatorLabel = label; // Allocate native memory for a single integer m_Counter = (byte *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <byte>() * size, UnsafeUtility.AlignOf <byte>(), label); // Create a dispose sentinel to track memory leaks. This also creates the AtomicSafetyHandle #if ENABLE_UNITY_COLLECTIONS_CHECKS #if UNITY_2018_3_OR_NEWER DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0, label); #else DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0); #endif #endif // Initialize the count to 0 to avoid uninitialized data for (int i = 0; i < size; ++i) { *(m_Counter + i) = 0; } }
private NativeQuadTree(int capacity, Allocator allocator, int disposeSentinelStackDepth) { var totalSize = UnsafeUtility.SizeOf(typeof(NativeQuadTreeNode)) * (long)capacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS if (allocator <= Allocator.None) { throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(allocator)); } if (capacity < 0) { throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be >= 0"); } CollectionHelper.CheckIsUnmanaged <NativeQuadTreeNode>(); if (totalSize > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(capacity), $"Capacity * sizeof(T) cannot exceed {int.MaxValue} bytes"); } DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator); #if UNITY_2020_1_OR_NEWER if (s_staticSafetyId.Data == 0) { CreateStaticSafetyId(); } AtomicSafetyHandle.SetStaticSafetyId(ref m_Safety, s_staticSafetyId.Data); #endif #endif m_ListData = UnsafeList.Create(UnsafeUtility.SizeOf <NativeQuadTreeNode>(), UnsafeUtility.AlignOf <NativeQuadTreeNode>(), capacity, allocator); m_SequenceId = UnsafeList.Create(UnsafeUtility.SizeOf <int>(), UnsafeUtility.AlignOf <int>(), 0, allocator); m_AllocatorLabel = allocator; _eps = 0; _resolution = 1; _min = int2.zero; _max = int2.zero; #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true); #endif }
public int AddRangeNoResize(void *ptr, int length) { CheckArgPositive(length); return(AddRangeNoResize(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), ptr, AssumePositive(length))); }
public void Init() { v = (double2 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <double2>() * 10000, UnsafeUtility.AlignOf <double2>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { v[i] = new double2(1.0f); } }
public ArchetypeManager(SharedComponentDataManager sharedComponentManager) { m_SharedComponentManager = sharedComponentManager; m_TypeLookup = new NativeMultiHashMap <uint, IntPtr>(256, Allocator.Persistent); m_EmptyChunkPool = (UnsafeLinkedListNode *)m_ArchetypeChunkAllocator.Allocate(sizeof(UnsafeLinkedListNode), UnsafeUtility.AlignOf <UnsafeLinkedListNode>()); UnsafeLinkedListNode.InitializeList(m_EmptyChunkPool); #if UNITY_ASSERTIONS // Buffer should be 16 byte aligned to ensure component data layout itself can gurantee being aligned var offset = UnsafeUtility.GetFieldOffset(typeof(Chunk).GetField("Buffer")); Assert.IsTrue(offset % 16 == 0, "Chunk buffer must be 16 byte aligned"); #endif }
public void Init() { v = (float3x3 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <float3x3>() * 100000, UnsafeUtility.AlignOf <float3x3>(), Allocator.Persistent); for (int i = 0; i < 100000; ++i) { v[i] = new float3x3(1.0f); } hash = (uint3 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <uint3>() * 100000, UnsafeUtility.AlignOf <uint3>(), Allocator.Persistent); for (int i = 0; i < 100000; ++i) { hash[i] = 0; } }
public void Init() { v = (double4 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <double4>() * 100000, UnsafeUtility.AlignOf <double4>(), Allocator.Persistent); for (int i = 0; i < 100000; ++i) { v[i] = new double4(1.0f); } hash = (uint *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <uint>() * 100000, UnsafeUtility.AlignOf <uint>(), Allocator.Persistent); for (int i = 0; i < 100000; ++i) { hash[i] = 0; } }
internal InstantiateCommandBufferUntyped(Allocator allocator, FixedList64 <ComponentType> componentTypesWithData, int disposeSentinalStackDepth) { int dataPayloadSize = 0; FixedListInt64 typesSizes = new FixedListInt64(); FixedListInt64 typesWithData = new FixedListInt64(); for (int i = 0; i < componentTypesWithData.Length; i++) { var size = TypeManager.GetTypeInfo(componentTypesWithData[i].TypeIndex).ElementSize; dataPayloadSize += size; typesSizes.Add(size); typesWithData.Add(componentTypesWithData[i].TypeIndex); } CheckComponentTypesValid(BuildComponentTypesFromFixedList(typesWithData)); m_prefabSortkeyBlockList = (UnsafeParallelBlockList *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <UnsafeParallelBlockList>(), UnsafeUtility.AlignOf <UnsafeParallelBlockList>(), allocator); m_componentDataBlockList = (UnsafeParallelBlockList *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <UnsafeParallelBlockList>(), UnsafeUtility.AlignOf <UnsafeParallelBlockList>(), allocator); m_state = (State *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <State>(), UnsafeUtility.AlignOf <State>(), allocator); *m_prefabSortkeyBlockList = new UnsafeParallelBlockList(UnsafeUtility.SizeOf <PrefabSortkey>(), 256, allocator); *m_componentDataBlockList = new UnsafeParallelBlockList(dataPayloadSize, 256, allocator); *m_state = new State { typesWithData = typesWithData, tagsToAdd = default,
public T *Allocate <T>(int items = 1) where T : unmanaged { return((T *)Allocate(items * UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>())); }
public int AddRangeNoResize(UnsafeList <T> list) { return(AddRangeNoResize(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), list.Ptr, list.Length)); }
public T *Construct <T>(T *src) where T : unmanaged { return((T *)Construct(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), src)); }
public void Init() { q = (quaternion *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <quaternion>() * 10000, UnsafeUtility.AlignOf <quaternion>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { q[i] = quaternion.identity; } }
public unsafe UnsafeIntList(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory) { Ptr = null; Length = 0; Capacity = 0; Allocator = Allocator.Invalid; this.ListData() = new UnsafeList(UnsafeUtility.SizeOf <int>(), UnsafeUtility.AlignOf <int>(), initialCapacity, allocator, options); }
public void ResizeUninitialized(int length) { CheckWriteAccessAndInvalidateArrayAliases(); BufferHeader.EnsureCapacity(m_Buffer, length, UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), BufferHeader.TrashMode.RetainOldData); m_Buffer->Length = length; }
/// <summary> /// Create an empty hash set with a given capacity /// </summary> /// /// <param name="capacity"> /// Capacity of the hash set. If less than four, four is used. /// </param> /// /// <param name="allocator"> /// Allocator to allocate unmanaged memory with. Must be valid. /// </param> public NativeHashSet(int capacity, Allocator allocator) { // Require a valid allocator if (allocator <= Allocator.None) { throw new ArgumentException( "Allocator must be Temp, TempJob or Persistent", "allocator"); } RequireBlittable(); // Insist on a minimum capacity if (capacity < 4) { capacity = 4; } m_Allocator = allocator; // Allocate the state NativeHashSetState *state = (NativeHashSetState *)UnsafeUtility.Malloc( sizeof(NativeHashSetState), UnsafeUtility.AlignOf <NativeHashSetState>(), allocator); state->ItemCapacity = capacity; // To reduce collisions, use twice as many buckets int bucketLength = capacity * 2; bucketLength = NextHigherPowerOfTwo(bucketLength); state->BucketCapacityMask = bucketLength - 1; // Allocate state arrays int nextOffset; int bucketOffset; int totalSize = CalculateDataLayout( capacity, bucketLength, out nextOffset, out bucketOffset); state->Items = (byte *)UnsafeUtility.Malloc( totalSize, JobsUtility.CacheLineSize, allocator); state->Next = state->Items + nextOffset; state->Buckets = state->Items + bucketOffset; m_State = state; #if ENABLE_UNITY_COLLECTIONS_CHECKS #if UNITY_2018_3_OR_NEWER DisposeSentinel.Create( out m_Safety, out m_DisposeSentinel, 1, allocator); #else DisposeSentinel.Create( out m_Safety, out m_DisposeSentinel, 1); #endif #endif Clear(); }
/// <summary> /// Initializes a new instance of the UnsafeBaselibNetworkArray struct. /// </summary> /// <param name="capacity"></param> /// <exception cref="ArgumentOutOfRangeException">Thrown if the capacity is less then 0 or if the value exceeds <see cref="int.MaxValue"/> </exception> public UnsafeBaselibNetworkArray(int capacity) { var totalSize = (long)capacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS if (capacity < 0) { throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be >= 0"); } if (totalSize > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(capacity), $"Capacity * sizeof(T) cannot exceed {int.MaxValue} bytes"); } #endif var pageInfo = stackalloc Binding.Baselib_Memory_PageSizeInfo[1]; Binding.Baselib_Memory_GetPageSizeInfo(pageInfo); var defaultPageSize = (ulong)pageInfo->defaultPageSize; var pageCount = (ulong)1; if ((ulong)totalSize > defaultPageSize) { pageCount = (ulong)math.ceil(totalSize / (double)defaultPageSize); } var error = default(ErrorState); var pageAllocation = Binding.Baselib_Memory_AllocatePages( pageInfo->defaultPageSize, pageCount, 1, Binding.Baselib_Memory_PageState.ReadWrite, &error); if (error.code != ErrorCode.Success) { throw new Exception(); } UnsafeUtility.MemSet((void *)pageAllocation.ptr, 0, (long)(pageAllocation.pageCount * pageAllocation.pageSize)); m_Buffer = (Binding.Baselib_RegisteredNetwork_Buffer *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <Binding.Baselib_RegisteredNetwork_Buffer>(), UnsafeUtility.AlignOf <Binding.Baselib_RegisteredNetwork_Buffer>(), Allocator.Persistent); *m_Buffer = Binding.Baselib_RegisteredNetwork_Buffer_Register(pageAllocation, &error); if (error.code != (int)ErrorCode.Success) { Binding.Baselib_Memory_ReleasePages(pageAllocation, &error); *m_Buffer = default; throw new Exception(); } }
public NativeListImpl(int capacity, Allocator allocatorLabel) #endif { #if ENABLE_UNITY_COLLECTIONS_CHECKS this.sentinel = sentinel; m_ListData = null; if (!UnsafeUtility.IsBlittable <T>()) { this.sentinel.Dispose(); throw new ArgumentException(string.Format("{0} used in NativeList<{0}> must be blittable", typeof(T))); } #endif m_MemoryAllocator = default(TMemManager); m_ListData = (NativeListData *)m_MemoryAllocator.Init(UnsafeUtility.SizeOf <NativeListData>(), UnsafeUtility.AlignOf <NativeListData>(), allocatorLabel); var elementSize = UnsafeUtility.SizeOf <T> (); //@TODO: Find out why this is needed? capacity = Math.Max(1, capacity); m_ListData->buffer = UnsafeUtility.Malloc(capacity * elementSize, UnsafeUtility.AlignOf <T>(), allocatorLabel); m_ListData->length = 0; m_ListData->capacity = capacity; }
public unsafe static void *MallocHelper <T> (int count) where T : struct { return(UnsafeUtility.Malloc(count * UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), Allocator.Persistent)); }
public void Reserve(int length) { CheckWriteAccessAndInvalidateArrayAliases(); #if ENABLE_UNITY_COLLECTIONS_CHECKS BufferHeader.EnsureCapacity(m_Buffer, length, UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), BufferHeader.TrashMode.RetainOldData, m_useMemoryInitPattern, m_memoryInitPattern); #else BufferHeader.EnsureCapacity(m_Buffer, length, UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), BufferHeader.TrashMode.RetainOldData, false, 0); #endif }
static void GatherIntermediateCrawlData(CachedSnapshot snapshot, IntermediateCrawlData crawlData) { unsafe { var uniqueHandlesPtr = (ulong *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <ulong>() * snapshot.gcHandles.Count, UnsafeUtility.AlignOf <ulong>(), Collections.Allocator.Temp); ulong *uniqueHandlesBegin = uniqueHandlesPtr; ulong *uniqueHandlesEnd = uniqueHandlesPtr + snapshot.gcHandles.Count; // Parse all handles for (int i = 0; i != snapshot.gcHandles.Count; i++) { var moi = new ManagedObjectInfo(); var target = snapshot.gcHandles.target[i]; if (target == 0) { #if SNAPSHOT_CRAWLER_DIAG Debuging.DebugUtility.LogWarning("null object in gc handles " + i); #endif moi.ManagedObjectIndex = i; crawlData.ManagedObjectInfos.Add(moi); } else if (snapshot.CrawledData.ManagedObjectByAddress.ContainsKey(target)) { #if SNAPSHOT_CRAWLER_DIAG Debuging.DebugUtility.LogWarning("Duplicate gc handles " + i + " addr:" + snapshot.gcHandles.target[i]); #endif moi.ManagedObjectIndex = i; moi.PtrObject = target; crawlData.ManagedObjectInfos.Add(moi); crawlData.DuplicatedGCHandlesStack.Push(i); } else { moi.ManagedObjectIndex = i; crawlData.ManagedObjectInfos.Add(moi); snapshot.CrawledData.ManagedObjectByAddress.Add(target, moi); UnsafeUtility.CopyStructureToPtr(ref target, uniqueHandlesBegin++); } } uniqueHandlesBegin = uniqueHandlesPtr; //reset iterator //add handles for processing while (uniqueHandlesBegin != uniqueHandlesEnd) { crawlData.CrawlDataStack.Push(new StackCrawlData { ptr = UnsafeUtility.ReadArrayElement <ulong>(uniqueHandlesBegin++, 0), ptrFrom = 0, typeFrom = -1, indexOfFrom = -1, fieldFrom = -1, fromArrayIndex = -1 }); } UnsafeUtility.Free(uniqueHandlesPtr, Collections.Allocator.Temp); } }
public void Init() { m = (double2x2 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <double2x2>() * 10000, UnsafeUtility.AlignOf <double2x2>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { m[i] = double2x2.identity; } }
ArchetypeQuery *CreateQuery(ref UnsafeScratchAllocator unsafeScratchAllocator, EntityQueryDesc[] queryDesc) { var outQuery = (ArchetypeQuery *)unsafeScratchAllocator.Allocate(sizeof(ArchetypeQuery) * queryDesc.Length, UnsafeUtility.AlignOf <ArchetypeQuery>()); for (int q = 0; q != queryDesc.Length; q++) { // Validate the queryDesc has components declared in a consistent way queryDesc[q].Validate(); var typesNone = queryDesc[q].None; var typesAll = queryDesc[q].All; var typesAny = queryDesc[q].Any; // None forced to read only { for (int i = 0; i < typesNone.Length; i++) { if (typesNone[i].AccessModeType != ComponentType.AccessMode.ReadOnly) { typesNone[i] = ComponentType.ReadOnly(typesNone[i].TypeIndex); } } } var isFilterWriteGroup = (queryDesc[q].Options & EntityQueryOptions.FilterWriteGroup) != 0; if (isFilterWriteGroup) { // Each ReadOnly<type> in any or all // if has WriteGroup types, // - Recursively add to any (if not explictly mentioned) var explicitList = CreateExplicitTypeList(typesNone, typesAll, typesAny); for (int i = 0; i < typesAny.Length; i++) { IncludeDependentWriteGroups(typesAny[i], explicitList); } for (int i = 0; i < typesAll.Length; i++) { IncludeDependentWriteGroups(typesAll[i], explicitList); } // Each ReadWrite<type> in any or all // if has WriteGroup types, // Add to none (if not exist in any or all or none) var noneList = new NativeList <ComponentType>(typesNone.Length, Allocator.Temp); for (int i = 0; i < typesNone.Length; i++) { noneList.Add(typesNone[i]); } for (int i = 0; i < typesAny.Length; i++) { ExcludeWriteGroups(typesAny[i], noneList, explicitList); } for (int i = 0; i < typesAll.Length; i++) { ExcludeWriteGroups(typesAll[i], noneList, explicitList); } typesNone = new ComponentType[noneList.Length]; for (int i = 0; i < noneList.Length; i++) { typesNone[i] = noneList[i]; } noneList.Dispose(); explicitList.Dispose(); } ConstructTypeArray(ref unsafeScratchAllocator, typesNone, out outQuery[q].None, out outQuery[q].NoneAccessMode, out outQuery[q].NoneCount); ConstructTypeArray(ref unsafeScratchAllocator, typesAll, out outQuery[q].All, out outQuery[q].AllAccessMode, out outQuery[q].AllCount); ConstructTypeArray(ref unsafeScratchAllocator, typesAny, out outQuery[q].Any, out outQuery[q].AnyAccessMode, out outQuery[q].AnyCount); outQuery[q].Options = queryDesc[q].Options; } return(outQuery); }
public void Init() { v = (float2 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <float2>() * 10000, UnsafeUtility.AlignOf <float2>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { v[i] = new float2(); } }
/// <summary> /// Constructs a new instance of <see cref="PackedBinaryStream"/> using the given capacities. /// </summary> /// <param name="initialTokensCapacity">Initial number of tokens to allocate.</param> /// <param name="initialBufferCapacity">Initial buffer size to allocate.</param> /// <param name="label">Allocator to use for internal buffers.</param> public PackedBinaryStream(int initialTokensCapacity, int initialBufferCapacity, Allocator label) { m_Label = label; m_Data = (PackedBinaryStreamData *)UnsafeUtility.Malloc(sizeof(PackedBinaryStreamData), UnsafeUtility.AlignOf <PackedBinaryStreamData>(), m_Label); UnsafeUtility.MemClear(m_Data, sizeof(PackedBinaryStreamData)); // Allocate token and handle buffers. m_Data->Tokens = (BinaryToken *)UnsafeUtility.Malloc(sizeof(BinaryToken) * initialTokensCapacity, UnsafeUtility.AlignOf <BinaryToken>(), m_Label); m_Data->Handles = (HandleData *)UnsafeUtility.Malloc(sizeof(HandleData) * initialTokensCapacity, UnsafeUtility.AlignOf <HandleData>(), m_Label); m_Data->TokensCapacity = initialTokensCapacity; // Allocate string/primitive storage. m_Data->Buffer = (byte *)UnsafeUtility.Malloc(sizeof(byte) * initialBufferCapacity, UnsafeUtility.AlignOf <byte>(), m_Label); m_Data->BufferCapacity = initialBufferCapacity; // Initialize handles and tokens with the correct indices. new InitializeJob { Handles = m_Data->Handles, BinaryTokens = m_Data->Tokens, StartIndex = 0 }.Run(initialTokensCapacity); Clear(); }