Пример #1
0
        public void AllocZeroedZeroElementCountTest()
        {
            void *ptr = NativeMemory.AllocZeroed(0, 1);

            Assert.True(ptr != null);
            NativeMemory.Free(ptr);
        }
Пример #2
0
        public void AllocZeroedZeroElementSizeTest()
        {
            void *ptr = NativeMemory.AllocZeroed(1, 0);

            Assert.True(ptr != null);
            NativeMemory.Free(ptr);
        }
        private static unsafe void AllocNullTerminatedArray(string[] arr, ref byte **arrPtr)
        {
            nuint arrLength = (nuint)arr.Length + 1; // +1 is for null termination

            // Allocate the unmanaged array to hold each string pointer.
            // It needs to have an extra element to null terminate the array.
            // Zero the memory so that if any of the individual string allocations fails,
            // we can loop through the array to free any that succeeded.
            // The last element will remain null.
            arrPtr = (byte **)NativeMemory.AllocZeroed(arrLength, (nuint)sizeof(byte *));

            // Now copy each string to unmanaged memory referenced from the array.
            // We need the data to be an unmanaged, null-terminated array of UTF8-encoded bytes.
            for (int i = 0; i < arr.Length; i++)
            {
                string str = arr[i];

                int byteLength = Encoding.UTF8.GetByteCount(str);
                arrPtr[i] = (byte *)NativeMemory.Alloc((nuint)byteLength + 1); //+1 for null termination

                int bytesWritten = Encoding.UTF8.GetBytes(str, new Span <byte>(arrPtr[i], byteLength));
                Debug.Assert(bytesWritten == byteLength);

                arrPtr[i][bytesWritten] = (byte)'\0'; // null terminate
            }
        }
Пример #4
0
 public void setup()
 {
     len   = 256;
     src   = NativeMemory.AllocZeroed(len);
     dest  = NativeMemory.AllocZeroed(len);
     src1  = src;
     dest1 = dest;
 }
Пример #5
0
        public void AllocZeroedElementCountTest()
        {
            void *ptr = NativeMemory.AllocZeroed(1, 1);

            Assert.True(ptr != null);
            Assert.Equal(expected: 0, actual: ((byte *)ptr)[0]);

            NativeMemory.Free(ptr);
        }
Пример #6
0
 private void Reserve(int count)
 {
     if (count > _count)
     {
         FreeNativeMemory();
         _buffers = (QUIC_BUFFER *)NativeMemory.AllocZeroed((nuint)count, (nuint)sizeof(QUIC_BUFFER));
         _count   = count;
     }
 }
Пример #7
0
        public void CopyTest(int sourceSize, int destinationSize, int byteCount)
        {
            void *source      = NativeMemory.AllocZeroed((nuint)sourceSize);
            void *destination = NativeMemory.AllocZeroed((nuint)destinationSize);

            new Span <byte>(source, sourceSize).Fill(0b10101010);

            NativeMemory.Copy(source, destination, (nuint)byteCount);

            Equals(byteCount - 1, new Span <byte>(destination, destinationSize).LastIndexOf <byte>(0b10101010));

            NativeMemory.Free(source);
            NativeMemory.Free(destination);
        }
Пример #8
0
        public SRP6(string username, byte[] salt, ReadOnlySpan <byte> verifier, SHA1Hash sha1)
        {
            _username = username;
            _sha1     = sha1;

            Span <byte> span = stackalloc byte[32];

            GetRandomBytes(span);
            _b = new BigInteger(span, true);
            _v = new BigInteger(verifier, true);

            Salt  = salt;
            _ptrB = NativeMemory.AllocZeroed(32);
            _B(ref _b, ref _v, new Span <byte>(_ptrB, 32));
        }
Пример #9
0
        public void CopyToOverlappedMemoryTest(int size, int offset, int byteCount)
        {
            byte *source = (byte *)NativeMemory.AllocZeroed((nuint)size);

            var expectedBlock = new byte[byteCount];

            Random.Shared.NextBytes(expectedBlock);
            expectedBlock.CopyTo(new Span <byte>(source, byteCount));

            NativeMemory.Copy(source, source + offset, (nuint)byteCount);

            Assert.True(expectedBlock.AsSpan().SequenceEqual(new ReadOnlySpan <byte>(source + offset, byteCount)));

            NativeMemory.Free(source);
        }
Пример #10
0
 public void AllocZeroedElementCountOOMTest()
 {
     Assert.Throws <OutOfMemoryException>(() => NativeMemory.AllocZeroed(1, nuint.MaxValue));
     Assert.Throws <OutOfMemoryException>(() => NativeMemory.AllocZeroed(nuint.MaxValue, 1));
     Assert.Throws <OutOfMemoryException>(() => NativeMemory.AllocZeroed(nuint.MaxValue, nuint.MaxValue));
 }
Пример #11
0
 public void AllocZeroedByteCountOOMTest()
 {
     Assert.Throws <OutOfMemoryException>(() => NativeMemory.AllocZeroed(nuint.MaxValue));
 }
Пример #12
0
 public UArray(int s) : this(NativeMemory.AllocZeroed((nuint)s, s_elementSize), s)
 {
 }
Пример #13
0
 public void setup()
 {
     len   = 4;
     src1  = NativeMemory.AllocZeroed((nuint)len);
     dest1 = new int[len];
 }
Пример #14
0
	public Pointer Alloc(nuint n) => NativeMemory.AllocZeroed(n);