コード例 #1
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
        public void AlignedAllocOverflowByteCountTest()
        {
            // POSIX requires byteCount to be a multiple of alignment and so we will internally upsize.
            // This upsizing can overflow for certain values since we do (byteCount + (alignment - 1)) & ~(alignment - 1)
            //
            // However, this overflow is "harmless" since it will result in a value that is less than alignment
            // given that alignment is a power of two and will ultimately be a value less than alignment which
            // will be treated as invalid and result in OOM.
            //
            // Take for example a 64-bit system where the max power of two is (1UL << 63): 9223372036854775808
            // * 9223372036854775808 + 9223372036854775807 == ulong.MaxValue, so no overflow
            // * 9223372036854775809 + 9223372036854775807 == 0, so overflows and is less than alignment
            // *      ulong.MaxValue + 9223372036854775807 == 9223372036854775806, so overflows and is less than alignment
            //
            // Likewise, for small alignments such as 8 (which is the smallest on a 64-bit system for POSIX):
            // * 18446744073709551608 + 7 == ulong.MaxValue, so no overflow
            // * 18446744073709551609 + 7 == 0, so overflows and is less than alignment
            // *       ulong.MaxValue + 7 == 6, so overflows and is less than alignment

            nuint maxAlignment = (nuint)1 << ((sizeof(nuint) * 8) - 1);

            Assert.Throws <OutOfMemoryException>(() => NativeMemory.AlignedAlloc(maxAlignment + 1, maxAlignment));

            Assert.Throws <OutOfMemoryException>(() => NativeMemory.AlignedAlloc(nuint.MaxValue, (uint)sizeof(nuint)));
        }
コード例 #2
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
        public void AlignedAllocLessThanVoidPtrAlignmentTest()
        {
            void *ptr = NativeMemory.AlignedAlloc(1, 1);

            Assert.True(ptr != null);
            NativeMemory.AlignedFree(ptr);
        }
コード例 #3
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
        public void AlignedAllocZeroSizeTest()
        {
            void *ptr = NativeMemory.AlignedAlloc(0, (uint)sizeof(nuint));

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % (uint)sizeof(nuint) == 0);

            NativeMemory.AlignedFree(ptr);
        }
コード例 #4
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
        public void AlignedAllocTest(uint alignment)
        {
            void *ptr = NativeMemory.AlignedAlloc(1, alignment);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % alignment == 0);

            NativeMemory.AlignedFree(ptr);
        }
コード例 #5
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
        public void AlignedReallocZeroAlignmentTest()
        {
            void *ptr = NativeMemory.AlignedAlloc(1, (uint)sizeof(nuint));

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % (uint)sizeof(nuint) == 0);

            Assert.Throws <ArgumentException>(() => NativeMemory.AlignedRealloc(ptr, (uint)sizeof(nuint), 0));
            NativeMemory.AlignedFree(ptr);
        }
コード例 #6
0
        public void ZeroMemoryWithSizeEqualTo0ShouldNoOpTest(int offset)
        {
            byte *ptr = (byte *)NativeMemory.AlignedAlloc(512, 8);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 8 == 0);

            new Span <byte>(ptr, 512).Fill(0b10101010);

            NativeMemory.ZeroMemory(ptr + offset, 0);

            Assert.Equal(-1, new Span <byte>(ptr, 512).IndexOfAnyExcept((byte)0b10101010));

            NativeMemory.AlignedFree(ptr);
        }
コード例 #7
0
        public void ZeroMemoryTest(int size, int offset)
        {
            byte *ptr = (byte *)NativeMemory.AlignedAlloc((nuint)(size + offset), 8);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 8 == 0);

            new Span <byte>(ptr, size + offset).Fill(0b10101010);

            NativeMemory.ZeroMemory(ptr + offset, (nuint)size);

            Assert.Equal(-1, new Span <byte>(ptr + offset, size).IndexOfAnyExcept((byte)0));

            NativeMemory.AlignedFree(ptr);
        }
コード例 #8
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
        public void AlignedReallocSmallerToLargerTest()
        {
            void *ptr = NativeMemory.AlignedAlloc(16, 16);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 16 == 0);

            for (int i = 0; i < 16; i++)
            {
                ((byte *)ptr)[i] = (byte)i;
            }

            void *newPtr = NativeMemory.AlignedRealloc(ptr, 32, 16);

            Assert.True(newPtr != null);
            Assert.True((nuint)newPtr % 16 == 0);

            for (int i = 0; i < 16; i++)
            {
                Assert.True(((byte *)newPtr)[i] == i);
            }

            NativeMemory.AlignedFree(newPtr);
        }
コード例 #9
0
        public void ZeroMemoryWithExactRangeTest(int size, int offset)
        {
            int headLength = offset;
            int bodyLength = size;
            int tailLength = 512 - headLength - bodyLength;
            int headOffset = 0;
            int bodyOffset = headLength;
            int tailOffset = headLength + bodyLength;

            byte *ptr = (byte *)NativeMemory.AlignedAlloc(512, 8);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 8 == 0);

            new Span <byte>(ptr, 512).Fill(0b10101010);

            NativeMemory.ZeroMemory(ptr + bodyOffset, (nuint)bodyLength);

            Assert.Equal(-1, new Span <byte>(ptr + headOffset, headLength).IndexOfAnyExcept((byte)0b10101010));
            Assert.Equal(-1, new Span <byte>(ptr + bodyOffset, bodyLength).IndexOfAnyExcept((byte)0));
            Assert.Equal(-1, new Span <byte>(ptr + tailOffset, tailLength).IndexOfAnyExcept((byte)0b10101010));

            NativeMemory.AlignedFree(ptr);
        }
コード例 #10
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
 public void AlignedAllocNonPowerOfTwoAlignmentTest()
 {
     Assert.Throws <ArgumentException>(() => NativeMemory.AlignedAlloc((uint)sizeof(nuint), (uint)sizeof(nuint) + 1));
     Assert.Throws <ArgumentException>(() => NativeMemory.AlignedAlloc((uint)sizeof(nuint), (uint)sizeof(nuint) * 3));
 }
コード例 #11
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
 public void AlignedAllocZeroAlignmentTest()
 {
     Assert.Throws <ArgumentException>(() => NativeMemory.AlignedAlloc((uint)sizeof(nuint), 0));
 }
コード例 #12
0
ファイル: NativeMemoryTests.cs プロジェクト: z77ma/runtime
 public void AlignedAllocOOMTest()
 {
     Assert.Throws <OutOfMemoryException>(() => NativeMemory.AlignedAlloc(nuint.MaxValue - ((uint)sizeof(nuint) - 1), (uint)sizeof(nuint)));
 }
コード例 #13
0
 public static unsafe AlignedMemory Allocate(nuint length, nuint alignment)
 => new AlignedMemory(NativeMemory.AlignedAlloc(length, alignment), (int)length);
コード例 #14
0
 public void Setup()
 {
     arr1 = (int *)NativeMemory.AlignedAlloc(LENGTH, 32);
     arr2 = (int *)NativeMemory.AlignedAlloc(LENGTH, 32);
 }
 private static void *_aligned_malloc([NativeTypeName("size_t")] nuint _Size, [NativeTypeName("size_t")] nuint _Alignment)
 {
     return(NativeMemory.AlignedAlloc(_Size, _Alignment));
 }