示例#1
0
        public void ReadWriteFromUnmanagedMemoryStream()
        {
            var startMemory = GC.GetTotalMemory(true);

            using (var buffer = new HGlobalSafeBuffer(Capacity))
                using (var stream = new UnmanagedMemoryStream(buffer, 0, Capacity, FileAccess.ReadWrite))
                {
                    buffer.Write(0, (byte)100);

                    var position = stream.Position;
                    Assert.Equal(100, stream.ReadByte());
                    Assert.Equal(stream.Position, position + 1);

                    var endMemory = GC.GetTotalMemory(false);
                    WriteLine($"Memory: {endMemory - startMemory} bytes");
                }
        }
示例#2
0
        public void ReadWriteStructUsingUma()
        {
            var startMemory = GC.GetTotalMemory(true);

            using (var buffer = new HGlobalSafeBuffer(Capacity))
                using (var accessor = new UnmanagedMemoryAccessor(buffer, 0, Capacity, FileAccess.ReadWrite))
                {
                    accessor.Write(0, ref _inStruct);
                    accessor.Read(0, out TestStruct outStruct);

                    Assert.Equal(_inStruct.int1, outStruct.int1);
                    Assert.Equal(_inStruct.int2, outStruct.int2);
                    Assert.Equal(_inStruct.bool1, outStruct.bool1);
                    Assert.Equal(_inStruct.char1, outStruct.char1);
                    Assert.Equal(_inStruct.bool2, outStruct.bool2);

                    var endMemory = GC.GetTotalMemory(false);
                    WriteLine($"Memory: {endMemory - startMemory} bytes");
                }
        }