public static byte *GetElementPointer(FakeBufferHeader *header)
        {
            if (header->Pointer != null)
            {
                return(header->Pointer);
            }

            return((byte *)(header + 1));
        }
        private void CopyBuffer(Entity src, Entity dst, ComponentType componentType)
        {
            int length      = emGetBufferLength(src, componentType.TypeIndex);
            var typeInfo    = TypeManager.GetTypeInfo(componentType.TypeIndex);
            var elementSize = typeInfo.ElementSize;
            var alignment   = typeInfo.AlignmentInBytes;

            FakeBufferHeader *dstHeader = (FakeBufferHeader *)emGetComponentDataRawRW(dst, componentType.TypeIndex);

            FakeBufferHeader.EnsureCapacity(dstHeader, length, elementSize, alignment, FakeBufferHeader.TrashMode.RetainOldData, false, 0);
            var dstBufferPtr = emGetBufferRawRW(dst, componentType.TypeIndex);
            var srcBufferPtr = emGetBufferRawRO(src, componentType.TypeIndex);

            UnsafeUtility.MemCpy(dstBufferPtr, srcBufferPtr, elementSize * length);
        }
        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;
        }