public static void SetCapacity(BufferHeader *header, int count, int typeSize, int alignment, TrashMode trashMode, bool useMemoryInitPattern, byte memoryInitPattern, int internalCapacity) { var newCapacity = count; if (newCapacity == header->Capacity) { return; } long newSizeInBytes = (long)newCapacity * typeSize; byte *oldData = GetElementPointer(header); byte *newData = (newCapacity <= internalCapacity) ? (byte *)(header + 1) : (byte *)UnsafeUtility.Malloc(newSizeInBytes, alignment, Allocator.Persistent); if (oldData != newData) // if at least one of them isn't the internal pointer... { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (useMemoryInitPattern) { if (trashMode == TrashMode.RetainOldData) { var oldSizeInBytes = (header->Capacity * typeSize); var bytesToInitialize = newSizeInBytes - oldSizeInBytes; if (bytesToInitialize > 0) { UnsafeUtility.MemSet(newData + oldSizeInBytes, memoryInitPattern, bytesToInitialize); } } else { UnsafeUtility.MemSet(newData, memoryInitPattern, newSizeInBytes); } } #endif if (trashMode == TrashMode.RetainOldData) { long bytesToCopy = Math.Min((long)header->Capacity, count) * typeSize; UnsafeUtility.MemCpy(newData, oldData, bytesToCopy); } // Note we're freeing the old buffer only if it was not using the internal capacity. Don't change this to 'oldData', because that would be a bug. if (header->Pointer != null) { UnsafeUtility.Free(header->Pointer, Allocator.Persistent); } } header->Pointer = (newData == (byte *)(header + 1)) ? null : newData; header->Capacity = newCapacity; }
public static void EnsureCapacity(BufferHeader *header, int count, int typeSize, int alignment, TrashMode trashMode) { if (header->Capacity >= count) { return; } int newCapacity = Math.Max(Math.Max(2 * header->Capacity, count), kMinimumCapacity); long newBlockSize = newCapacity * typeSize; byte *oldData = GetElementPointer(header); byte *newData = (byte *)UnsafeUtility.Malloc(newBlockSize, alignment, Allocator.Persistent); if (trashMode == TrashMode.RetainOldData) { long oldBlockSize = header->Capacity * typeSize; UnsafeUtility.MemCpy(newData, oldData, oldBlockSize); } // Note we're freeing the old buffer only if it was not using the internal capacity. Don't change this to 'oldData', because that would be a bug. if (header->Pointer != null) { UnsafeUtility.Free(header->Pointer, Allocator.Persistent); } header->Pointer = newData; header->Capacity = newCapacity; }
public static void EnsureCapacity(BufferHeader *header, int count, int typeSize, int alignment, TrashMode trashMode, bool useMemoryInitPattern, byte memoryInitPattern) { if (count <= header->Capacity) { return; } var adjustedCount = Math.Max(kMinimumCapacity, Math.Max(2 * header->Capacity, count)); // stop pathological performance of ++Capacity allocating every time, tiny Capacities SetCapacity(header, adjustedCount, typeSize, alignment, trashMode, useMemoryInitPattern, memoryInitPattern, 0); }
public static void EnsureCapacity(FakeBufferHeader *header, int count, int typeSize, int alignment, TrashMode trashMode, bool useMemoryInitPattern, byte memoryInitPattern) { if (header->Capacity >= count) { return; } int newCapacity = Math.Max(Math.Max(2 * header->Capacity, count), kMinimumCapacity); long newBlockSize = (long)newCapacity * typeSize; byte *oldData = GetElementPointer(header); byte *newData = (byte *)UnsafeUtility.Malloc(newBlockSize, alignment, Allocator.Persistent); #if ENABLE_UNITY_COLLECTIONS_CHECKS if (useMemoryInitPattern) { if (trashMode == TrashMode.RetainOldData) { var oldBlockSize = (header->Capacity * typeSize); var remainingSize = newBlockSize - oldBlockSize; if (remainingSize > 0) { UnsafeUtility.MemSet(newData + oldBlockSize, memoryInitPattern, remainingSize); } } else { UnsafeUtility.MemSet(newData, memoryInitPattern, newBlockSize); } } #endif if (trashMode == TrashMode.RetainOldData) { long oldBlockSize = (long)header->Capacity * typeSize; UnsafeUtility.MemCpy(newData, oldData, oldBlockSize); } // Note we're freeing the old buffer only if it was not using the internal capacity. Don't change this to 'oldData', because that would be a bug. if (header->Pointer != null) { UnsafeUtility.Free(header->Pointer, Allocator.Persistent); } header->Pointer = newData; header->Capacity = newCapacity; }
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; }
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 void EnsureCapacity(BufferHeader *header, int count, int typeSize, int alignment, TrashMode trashMode) { if (header.Capacity < count) { int num = Math.Max(Math.Max(2 * header.Capacity, count), 8); byte *numPtr = ref GetElementPointer(header); byte *numPtr2 = (byte *)ref UnsafeUtility.Malloc((long)(num * typeSize), alignment, Allocator.Persistent); if (trashMode == TrashMode.RetainOldData) { UnsafeUtility.MemCpy((void *)numPtr2, (void *)numPtr, (long)(header.Capacity * typeSize)); } if (header.Pointer != null) { UnsafeUtility.Free((void *)header.Pointer, Allocator.Persistent); } header.Pointer = numPtr2; header.Capacity = num; } }