Пример #1
0
        /// <summary>
        /// Constructs a new container with the specified initial capacity and type of memory allocation.
        /// </summary>
        /// <param name="numBits">Number of bits.</param>
        /// <param name="allocator">A member of the
        /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param>
        /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param>
        public UnsafeBitArray(int numBits, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
        {
            Allocator = allocator;
            var sizeInBytes = Bitwise.AlignUp(numBits, 64) / 8;

            Ptr    = (ulong *)UnsafeUtility.Malloc(sizeInBytes, 16, allocator);
            Length = sizeInBytes * 8;

            if (options == NativeArrayOptions.ClearMemory)
            {
                UnsafeUtility.MemClear(Ptr, sizeInBytes);
            }
        }
Пример #2
0
        /// <summary>
        /// Constructs a new container with the specified initial capacity and type of memory allocation.
        /// </summary>
        /// <param name="numBits">Number of bits.</param>
        /// <param name="allocator">A member of the
        /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param>
        /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param>
        /// <note>Allocated number of bits will be aligned-up to closest 64-bits. For example, passing 1 as numBits will create BitArray that's
        /// 64-bit (8 bytes) long.</note>
        public UnsafeBitArray(int numBits, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
        {
            CheckAllocator(allocator);
            Allocator = allocator;
            var sizeInBytes = Bitwise.AlignUp(numBits, 64) / 8;

            Ptr    = (ulong *)Memory.Unmanaged.Allocate(sizeInBytes, 16, allocator);
            Length = numBits;

            if (options == NativeArrayOptions.ClearMemory)
            {
                UnsafeUtility.MemClear(Ptr, sizeInBytes);
            }
        }
Пример #3
0
        /// <summary>
        /// Constructs a new container with the specified initial capacity and type of memory allocation.
        /// </summary>
        /// <param name="numBits">Number of bits.</param>
        /// <param name="allocator">A member of the
        /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param>
        /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param>
        public UnsafeBitArray(int numBits, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
        {
            if (allocator <= Allocator.None)
            {
                throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(allocator));
            }

            Allocator = allocator;
            var sizeInBytes = Bitwise.AlignUp(numBits, 64) / 8;

            Ptr    = (ulong *)UnsafeUtility.Malloc(sizeInBytes, 16, allocator);
            Length = sizeInBytes * 8;

            if (options == NativeArrayOptions.ClearMemory)
            {
                UnsafeUtility.MemClear(Ptr, sizeInBytes);
            }
        }
Пример #4
0
        /// <summary>
        /// Clear all bits to 0.
        /// </summary>
        public void Clear()
        {
            var sizeInBytes = Bitwise.AlignUp(Length, 64) / 8;

            UnsafeUtility.MemClear(Ptr, sizeInBytes);
        }