未验证 提交 023183d7 编写于 作者: T Tomáš Matoušek 提交者: GitHub

Remove unused parameter and clean up MemoryMappedInfo (#40817)

* Remove unused parameter and clean up MemoryMappedInfo
上级 1d4cf98e
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Diagnostics;
using System.IO;
......@@ -38,9 +40,9 @@ internal sealed class MemoryMappedInfo : IDisposable
/// The memory mapped file.
/// </summary>
/// <remarks>
/// <para>It is possible for this accessor to be disposed prior to the view and/or the streams which use it.
/// However, the operating system does not actually close the views which are in use until the view handles
/// are closed as well, even if the <see cref="MemoryMappedFile"/> is disposed first.</para>
/// <para>It is possible for the file to be disposed prior to the view and/or the streams which use it.
/// However, the operating system does not actually close the views which are in use until the file handles
/// are closed as well, even if the file is disposed first.</para>
/// </remarks>
private readonly ReferenceCountedDisposable<MemoryMappedFile> _memoryMappedFile;
......@@ -89,16 +91,6 @@ public MemoryMappedInfo(string name, long offset, long size)
/// </summary>
public long Size { get; }
private static void ForceCompactingGC()
{
// repeated GC.Collect / WaitForPendingFinalizers till memory freed delta is super small, ignore the return value
GC.GetTotalMemory(forceFullCollection: true);
// compact the LOH
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
}
/// <summary>
/// Caller is responsible for disposing the returned stream.
/// multiple call of this will not increase VM.
......@@ -120,7 +112,7 @@ public Stream CreateReadableStream()
}
Debug.Assert(streamAccessor.Target.CanRead);
return new SharedReadableStream(this, streamAccessor, Size);
return new SharedReadableStream(streamAccessor, Size);
}
}
......@@ -167,6 +159,16 @@ public Stream CreateWritableStream()
}
}
private static void ForceCompactingGC()
{
// repeated GC.Collect / WaitForPendingFinalizers till memory freed delta is super small, ignore the return value
GC.GetTotalMemory(forceFullCollection: true);
// compact the LOH
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
}
public void Dispose()
{
// See remarks on field for relation between _memoryMappedFile and the views/streams. There is no
......@@ -182,44 +184,17 @@ private unsafe sealed class SharedReadableStream : Stream, ISupportDirectMemoryA
private byte* _current;
private readonly byte* _end;
public SharedReadableStream(MemoryMappedInfo owner, ReferenceCountedDisposable<MemoryMappedViewAccessor> accessor, long length)
public SharedReadableStream(ReferenceCountedDisposable<MemoryMappedViewAccessor> accessor, long length)
{
_accessor = accessor;
_current = _start = (byte*)_accessor.Target.SafeMemoryMappedViewHandle.DangerousGetHandle() + _accessor.Target.PointerOffset;
_end = checked(_start + length);
}
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanSeek
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override long Length
{
get
{
return _end - _start;
}
}
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => _end - _start;
public override long Position
{
......@@ -270,23 +245,13 @@ public override long Seek(long offset, SeekOrigin origin)
byte* target;
try
{
switch (origin)
target = origin switch
{
case SeekOrigin.Begin:
target = checked(_start + offset);
break;
case SeekOrigin.Current:
target = checked(_current + offset);
break;
case SeekOrigin.End:
target = checked(_end + offset);
break;
default:
throw new ArgumentOutOfRangeException(nameof(origin));
}
SeekOrigin.Begin => checked(_start + offset),
SeekOrigin.Current => checked(_current + offset),
SeekOrigin.End => checked(_end + offset),
_ => throw new ArgumentOutOfRangeException(nameof(origin)),
};
}
catch (OverflowException)
{
......@@ -302,20 +267,9 @@ public override long Seek(long offset, SeekOrigin origin)
return _current - _start;
}
public override void Flush()
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override void Flush() => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册