Esempio n. 1
0
        static void GrowMemory(ref WorldAllocator storage, int newElementCapacity)
        {
            if (storage.elementAllocationCapacity < 0)
            {
                throw new InvalidOperationException();
            }

            long extraBytes = UnsafeUtility.SizeOf <RLEElement>() * (newElementCapacity - storage.elementAllocationCapacity);

            long newBytes = UnsafeUtility.SizeOf <RLEColumn>() * (long)storage.columnCount;

            newBytes += UnsafeUtility.SizeOf <RLEElement>() * (long)newElementCapacity;
            void *newPointer = UnsafeUtility.Malloc(newBytes, UnsafeUtility.AlignOf <RLEColumn>(), Allocator.Persistent);

            if (storage.pointer == null)
            {
                UnsafeUtility.MemClear(newPointer, newBytes);
            }
            else
            {
                UnsafeUtility.MemCpy(newPointer, storage.pointer, newBytes - extraBytes);
                UnsafeUtility.MemClear((byte *)newPointer + newBytes - extraBytes, extraBytes);
                UnsafeUtility.Free(storage.pointer, Allocator.Persistent);
            }

            storage.pointer = newPointer;
            storage.elementAllocationCapacity = newElementCapacity;
            storage.elementsStart             = storage.GetColumnPointer(storage.columnCount);
        }
Esempio n. 2
0
        public static WorldAllocator Allocate(int columnCount, void *data)
        {
            WorldAllocator storage = new WorldAllocator();

            storage.columnCount               = columnCount;
            storage.allocationLock            = new System.Threading.SpinLock(false);
            storage.pointer                   = data;
            storage.elementsStart             = storage.GetColumnPointer(storage.columnCount);
            storage.elementAllocationCapacity = -1;
            return(storage);
        }