public static unsafe void Assign(BufferHeaderProxy *header, byte *source, int count, int typeSize, int alignment, bool useMemoryInitPattern, byte memoryInitPattern) { EnsureCapacity(header, count, typeSize, alignment, TrashMode.TrashOldData, useMemoryInitPattern, memoryInitPattern); byte *elementPtr = GetElementPointer(header); UnsafeUtility.MemCpy(elementPtr, source, (long)typeSize * (long)count); header->Length = count; }
public static unsafe void EnsureCapacity(BufferHeaderProxy *header, int count, int typeSize, int alignment, TrashMode trashMode, bool useMemoryInitPattern, byte memoryInitPattern) { if (count > header->Capacity) { int adjustedCount = math.max(8, math.max(2 * header->Capacity, count)); SetCapacity(header, adjustedCount, typeSize, alignment, trashMode, useMemoryInitPattern, memoryInitPattern, 0); } }
public static unsafe byte *GetElementPointer(BufferHeaderProxy *header) { if (header->Pointer != null) { return(header->Pointer); } return((byte *)(header + 1)); }
public static unsafe void SetCapacity(BufferHeaderProxy *header, int count, int typeSize, int alignment, TrashMode trashMode, bool useMemoryInitPattern, byte memoryInitPattern, int internalCapacity) { if (count == header->Capacity) { return; } long newSizeInBytes = (long)count * (long)typeSize; byte *oldData = GetElementPointer(header); byte *newData = (byte *)((count <= internalCapacity) ? (header + 1) : UnsafeUtility.Malloc(newSizeInBytes, alignment, Allocator.Persistent)); if (oldData != newData) { if (useMemoryInitPattern) { if (trashMode == TrashMode.RetainOldData) { int oldSizeInBytes = header->Capacity * typeSize; long bytesToInitialize = newSizeInBytes - oldSizeInBytes; if (bytesToInitialize > 0) { UnsafeUtility.MemSet(newData + oldSizeInBytes, memoryInitPattern, bytesToInitialize); } } else { UnsafeUtility.MemSet(newData, memoryInitPattern, newSizeInBytes); } } if (trashMode == TrashMode.RetainOldData) { long bytesToCopy = math.min((long)header->Capacity, (long)count) * typeSize; UnsafeUtility.MemCpy(newData, oldData, bytesToCopy); } if (header->Pointer != null) { UnsafeUtility.Free(header->Pointer, Allocator.Persistent); } } header->Pointer = (byte *)(long)((newData == header + 1) ? ((IntPtr)(void *)null) : ((IntPtr)newData)); header->Capacity = count; }