From 9dd7d6d937b6a92280b0c7a90d2e09e0c3189385 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 20 Nov 2017 08:42:47 -0800 Subject: [PATCH] Move TaskToApm to shared CoreLib partition (#15113) - Get TaskToApm in sync with CoreFX copy and move it to shared CoreLib partition - Delete redundant __Error file - Delete remaining uses of InternalBlockCopy and replace it with BlockCopy --- src/mscorlib/Resources/Strings.resx | 5 +- src/mscorlib/System.Private.CoreLib.csproj | 2 - .../shared/System.Private.CoreLib.Shared.projitems | 1 + .../System/Threading/Tasks/TaskToApm.cs | 46 ++++++------ src/mscorlib/src/System/Buffer.cs | 8 --- src/mscorlib/src/System/IO/BinaryReader.cs | 38 +++++----- src/mscorlib/src/System/IO/File.cs | 2 +- src/mscorlib/src/System/IO/MemoryStream.cs | 52 +++++++------- src/mscorlib/src/System/IO/Stream.cs | 16 ++--- src/mscorlib/src/System/IO/StreamReader.cs | 28 ++++---- src/mscorlib/src/System/IO/__Error.cs | 81 ---------------------- src/mscorlib/src/System/TimeZoneInfo.Unix.cs | 2 +- src/vm/comutilnative.cpp | 48 ------------- src/vm/comutilnative.h | 1 - src/vm/ecalllist.h | 1 - 15 files changed, 96 insertions(+), 235 deletions(-) rename src/mscorlib/{src => shared}/System/Threading/Tasks/TaskToApm.cs (88%) delete mode 100644 src/mscorlib/src/System/IO/__Error.cs diff --git a/src/mscorlib/Resources/Strings.resx b/src/mscorlib/Resources/Strings.resx index 9cc4f95..a905183 100644 --- a/src/mscorlib/Resources/Strings.resx +++ b/src/mscorlib/Resources/Strings.resx @@ -2428,9 +2428,6 @@ Array does not have that many dimensions. - - Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader. - Unmanaged memory stream position was beyond the capacity of the stream. @@ -3700,4 +3697,4 @@ Specified type is not supported - \ No newline at end of file + diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index c6a87ce..08fbf2e 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -504,7 +504,6 @@ - @@ -513,7 +512,6 @@ - diff --git a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems index 96d878f..f85567b 100644 --- a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems @@ -510,6 +510,7 @@ + diff --git a/src/mscorlib/src/System/Threading/Tasks/TaskToApm.cs b/src/mscorlib/shared/System/Threading/Tasks/TaskToApm.cs similarity index 88% rename from src/mscorlib/src/System/Threading/Tasks/TaskToApm.cs rename to src/mscorlib/shared/System/Threading/Tasks/TaskToApm.cs index 7e22dfa..add41f5 100644 --- a/src/mscorlib/src/System/Threading/Tasks/TaskToApm.cs +++ b/src/mscorlib/shared/System/Threading/Tasks/TaskToApm.cs @@ -2,13 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ -// -// -// // Helper methods for using Tasks to implement the APM pattern. // // Example usage, wrapping a Task-returning FooAsync method with Begin/EndFoo methods: +// // public IAsyncResult BeginFoo(..., AsyncCallback callback, object state) // { // Task t = FooAsync(...); @@ -18,10 +15,7 @@ // { // return TaskToApm.End(asyncResult); // } -// -// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -using System.IO; using System.Diagnostics; namespace System.Threading.Tasks @@ -48,19 +42,19 @@ namespace System.Threading.Tasks IAsyncResult asyncResult; if (task.IsCompleted) { - // Synchronous completion + // Synchronous completion. asyncResult = new TaskWrapperAsyncResult(task, state, completedSynchronously: true); - if (callback != null) - callback(asyncResult); + callback?.Invoke(asyncResult); } - // Otherwise, we need to schedule a callback. Whether we can use the Task as the IAsyncResult - // depends on whether the Task's AsyncState has reference equality with the requested state. else { - // Asynchronous completion + // For asynchronous completion we need to schedule a callback. Whether we can use the Task as the IAsyncResult + // depends on whether the Task's AsyncState has reference equality with the requested state. asyncResult = task.AsyncState == state ? (IAsyncResult)task : new TaskWrapperAsyncResult(task, state, completedSynchronously: false); if (callback != null) + { InvokeCallbackWhenTaskCompletes(task, callback, asyncResult); + } } return asyncResult; } @@ -78,15 +72,18 @@ namespace System.Threading.Tasks task = twar.Task; Debug.Assert(task != null, "TaskWrapperAsyncResult should never wrap a null Task."); } - // Otherwise, the IAsyncResult should be a Task. else { + // Otherwise, the IAsyncResult should be a Task. task = asyncResult as Task; } // Make sure we actually got a task, then complete the operation by waiting on it. if (task == null) - __Error.WrongAsyncResult(); + { + throw new ArgumentNullException(); + } + task.GetAwaiter().GetResult(); } @@ -103,15 +100,18 @@ namespace System.Threading.Tasks task = twar.Task as Task; Debug.Assert(twar.Task != null, "TaskWrapperAsyncResult should never wrap a null Task."); } - // Otherwise, the IAsyncResult should be a Task. else { + // Otherwise, the IAsyncResult should be a Task. task = asyncResult as Task; } // Make sure we actually got a task, then complete the operation by waiting on it. if (task == null) - __Error.WrongAsyncResult(); + { + throw new ArgumentNullException(); + } + return task.GetAwaiter().GetResult(); } @@ -158,9 +158,9 @@ namespace System.Threading.Tasks /// The wrapped Task. internal readonly Task Task; /// The new AsyncState value. - private readonly object m_state; + private readonly object _state; /// The new CompletedSynchronously value. - private readonly bool m_completedSynchronously; + private readonly bool _completedSynchronously; /// Initializes the IAsyncResult with the Task to wrap and the overriding AsyncState and CompletedSynchronously values. /// The Task to wrap. @@ -172,16 +172,16 @@ namespace System.Threading.Tasks Debug.Assert(!completedSynchronously || task.IsCompleted, "If completedSynchronously is true, the task must be completed."); this.Task = task; - m_state = state; - m_completedSynchronously = completedSynchronously; + _state = state; + _completedSynchronously = completedSynchronously; } // The IAsyncResult implementation. // - IsCompleted and AsyncWaitHandle just pass through to the Task. // - AsyncState and CompletedSynchronously return the corresponding values stored in this object. - object IAsyncResult.AsyncState { get { return m_state; } } - bool IAsyncResult.CompletedSynchronously { get { return m_completedSynchronously; } } + object IAsyncResult.AsyncState { get { return _state; } } + bool IAsyncResult.CompletedSynchronously { get { return _completedSynchronously; } } bool IAsyncResult.IsCompleted { get { return this.Task.IsCompleted; } } WaitHandle IAsyncResult.AsyncWaitHandle { get { return ((IAsyncResult)this.Task).AsyncWaitHandle; } } } diff --git a/src/mscorlib/src/System/Buffer.cs b/src/mscorlib/src/System/Buffer.cs index a015e56..9d4d693 100644 --- a/src/mscorlib/src/System/Buffer.cs +++ b/src/mscorlib/src/System/Buffer.cs @@ -35,14 +35,6 @@ namespace System public static extern void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); - // A very simple and efficient memmove that assumes all of the - // parameter validation has already been done. The count and offset - // parameters here are in bytes. If you want to use traditional - // array element indices and counts, use Array.Copy. - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern void InternalBlockCopy(Array src, int srcOffsetBytes, - Array dst, int dstOffsetBytes, int byteCount); - // This is ported from the optimized CRT assembly in memchr.asm. The JIT generates // pretty good code here and this ends up being within a couple % of the CRT asm. // It is however cross platform as the CRT hasn't ported their fast version to 64-bit diff --git a/src/mscorlib/src/System/IO/BinaryReader.cs b/src/mscorlib/src/System/IO/BinaryReader.cs index c5cd6cb..05de184 100644 --- a/src/mscorlib/src/System/IO/BinaryReader.cs +++ b/src/mscorlib/src/System/IO/BinaryReader.cs @@ -117,7 +117,7 @@ namespace System.IO public virtual int PeekChar() { - if (_stream == null) __Error.FileNotOpen(); + if (_stream == null) throw Error.GetFileNotOpen(); if (!_stream.CanSeek) return -1; @@ -131,7 +131,7 @@ namespace System.IO { if (_stream == null) { - __Error.FileNotOpen(); + throw Error.GetFileNotOpen(); } return InternalReadOneChar(); } @@ -145,11 +145,11 @@ namespace System.IO public virtual byte ReadByte() { // Inlined to avoid some method call overhead with FillBuffer. - if (_stream == null) __Error.FileNotOpen(); + if (_stream == null) throw Error.GetFileNotOpen(); int b = _stream.ReadByte(); if (b == -1) - __Error.EndOfFile(); + throw Error.GetEndOfFile(); return (byte)b; } @@ -165,7 +165,7 @@ namespace System.IO int value = Read(); if (value == -1) { - __Error.EndOfFile(); + throw Error.GetEndOfFile(); } return (char)value; } @@ -187,7 +187,7 @@ namespace System.IO { if (_isMemoryStream) { - if (_stream == null) __Error.FileNotOpen(); + if (_stream == null) throw Error.GetFileNotOpen(); // read directly from MemoryStream buffer MemoryStream mStream = _stream as MemoryStream; Debug.Assert(mStream != null, "_stream as MemoryStream != null"); @@ -265,7 +265,7 @@ namespace System.IO public virtual String ReadString() { if (_stream == null) - __Error.FileNotOpen(); + throw Error.GetFileNotOpen(); int currPos = 0; int n; @@ -303,7 +303,7 @@ namespace System.IO n = _stream.Read(_charBytes, 0, readLength); if (n == 0) { - __Error.EndOfFile(); + throw Error.GetEndOfFile(); } charsRead = _decoder.GetChars(_charBytes, 0, n, _charBuffer, 0); @@ -340,7 +340,7 @@ namespace System.IO } if (_stream == null) - __Error.FileNotOpen(); + throw Error.GetFileNotOpen(); // SafeCritical: index and count have already been verified to be a valid range for the buffer return InternalReadChars(new Span(buffer, index, count)); @@ -349,7 +349,7 @@ namespace System.IO public virtual int Read(Span buffer) { if (_stream == null) - __Error.FileNotOpen(); + throw Error.GetFileNotOpen(); return InternalReadChars(buffer); } @@ -524,7 +524,7 @@ namespace System.IO } if (_stream == null) { - __Error.FileNotOpen(); + throw Error.GetFileNotOpen(); } if (count == 0) @@ -538,7 +538,7 @@ namespace System.IO if (n != count) { char[] copy = new char[n]; - Buffer.InternalBlockCopy(chars, 0, copy, 0, 2 * n); // sizeof(char) + Buffer.BlockCopy(chars, 0, copy, 0, 2 * n); // sizeof(char) chars = copy; } @@ -556,14 +556,14 @@ namespace System.IO if (buffer.Length - index < count) throw new ArgumentException(SR.Argument_InvalidOffLen); - if (_stream == null) __Error.FileNotOpen(); + if (_stream == null) throw Error.GetFileNotOpen(); return _stream.Read(buffer, index, count); } public virtual int Read(Span buffer) { if (_stream == null) - __Error.FileNotOpen(); + throw Error.GetFileNotOpen(); return _stream.Read(buffer); } @@ -571,7 +571,7 @@ namespace System.IO public virtual byte[] ReadBytes(int count) { if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); - if (_stream == null) __Error.FileNotOpen(); + if (_stream == null) throw Error.GetFileNotOpen(); if (count == 0) { @@ -594,7 +594,7 @@ namespace System.IO { // Trim array. This should happen on EOF & possibly net streams. byte[] copy = new byte[numRead]; - Buffer.InternalBlockCopy(result, 0, copy, 0, numRead); + Buffer.BlockCopy(result, 0, copy, 0, numRead); result = copy; } @@ -610,7 +610,7 @@ namespace System.IO int bytesRead = 0; int n = 0; - if (_stream == null) __Error.FileNotOpen(); + if (_stream == null) throw Error.GetFileNotOpen(); // Need to find a good threshold for calling ReadByte() repeatedly // vs. calling Read(byte[], int, int) for both buffered & unbuffered @@ -619,7 +619,7 @@ namespace System.IO { n = _stream.ReadByte(); if (n == -1) - __Error.EndOfFile(); + throw Error.GetEndOfFile(); _buffer[0] = (byte)n; return; } @@ -629,7 +629,7 @@ namespace System.IO n = _stream.Read(_buffer, bytesRead, numBytes - bytesRead); if (n == 0) { - __Error.EndOfFile(); + throw Error.GetEndOfFile(); } bytesRead += n; } while (bytesRead < numBytes); diff --git a/src/mscorlib/src/System/IO/File.cs b/src/mscorlib/src/System/IO/File.cs index 6754baa..2b2506c 100644 --- a/src/mscorlib/src/System/IO/File.cs +++ b/src/mscorlib/src/System/IO/File.cs @@ -99,7 +99,7 @@ namespace System.IO { int n = fs.Read(bytes, index, count); if (n == 0) - __Error.EndOfFile(); + throw Error.GetEndOfFile(); index += n; count -= n; } diff --git a/src/mscorlib/src/System/IO/MemoryStream.cs b/src/mscorlib/src/System/IO/MemoryStream.cs index 85930df..ebbb979 100644 --- a/src/mscorlib/src/System/IO/MemoryStream.cs +++ b/src/mscorlib/src/System/IO/MemoryStream.cs @@ -135,7 +135,7 @@ namespace System.IO private void EnsureWriteable() { - if (!CanWrite) __Error.WriteNotSupported(); + if (!CanWrite) throw Error.GetWriteNotSupported(); } protected override void Dispose(bool disposing) @@ -235,7 +235,7 @@ namespace System.IO // PERF: True cursor position, we don't need _origin for direct access internal int InternalGetPosition() { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); return _position; } @@ -243,13 +243,13 @@ namespace System.IO internal int InternalReadInt32() { if (!_isOpen) - __Error.StreamIsClosed(); + throw Error.GetStreamIsClosed(); int pos = (_position += 4); // use temp to avoid a race condition if (pos > _length) { _position = _length; - __Error.EndOfFile(); + throw Error.GetEndOfFile(); } return (int)(_buffer[pos - 4] | _buffer[pos - 3] << 8 | _buffer[pos - 2] << 16 | _buffer[pos - 1] << 24); } @@ -257,7 +257,7 @@ namespace System.IO // PERF: Get actual length of bytes available for read; do sanity checks; shift position - i.e. everything except actual copying bytes internal int InternalEmulateRead(int count) { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); int n = _length - _position; if (n > count) n = count; @@ -276,17 +276,21 @@ namespace System.IO { get { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); return _capacity - _origin; } set { // Only update the capacity if the MS is expandable and the value is different than the current capacity. // Special behavior if the MS isn't expandable: we don't throw if value is the same as the current capacity - if (value < Length) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity); + if (value < Length) + throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity); - if (!_isOpen) __Error.StreamIsClosed(); - if (!_expandable && (value != Capacity)) __Error.MemoryStreamNotExpandable(); + if (!_isOpen) + throw Error.GetStreamIsClosed(); + + if (!_expandable && (value != Capacity)) + throw new NotSupportedException(SR.NotSupported_MemStreamNotExpandable); // MemoryStream has this invariant: _origin > 0 => !expandable (see ctors) if (_expandable && value != _capacity) @@ -294,7 +298,7 @@ namespace System.IO if (value > 0) { byte[] newBuffer = new byte[value]; - if (_length > 0) Buffer.InternalBlockCopy(_buffer, 0, newBuffer, 0, _length); + if (_length > 0) Buffer.BlockCopy(_buffer, 0, newBuffer, 0, _length); _buffer = newBuffer; } else @@ -310,7 +314,7 @@ namespace System.IO { get { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); return _length - _origin; } } @@ -319,7 +323,7 @@ namespace System.IO { get { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); return _position - _origin; } set @@ -327,7 +331,7 @@ namespace System.IO if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum); - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); if (value > MemStreamMaxLength) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_StreamLength); @@ -346,7 +350,7 @@ namespace System.IO if (buffer.Length - offset < count) throw new ArgumentException(SR.Argument_InvalidOffLen); - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); int n = _length - _position; if (n > count) n = count; @@ -362,7 +366,7 @@ namespace System.IO buffer[offset + byteCount] = _buffer[_position + byteCount]; } else - Buffer.InternalBlockCopy(_buffer, _position, buffer, offset, n); + Buffer.BlockCopy(_buffer, _position, buffer, offset, n); _position += n; return n; @@ -380,7 +384,7 @@ namespace System.IO if (!_isOpen) { - __Error.StreamIsClosed(); + throw Error.GetStreamIsClosed(); } int n = Math.Min(_length - _position, destination.Length); @@ -471,7 +475,7 @@ namespace System.IO public override int ReadByte() { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); if (_position >= _length) return -1; @@ -556,7 +560,7 @@ namespace System.IO public override long Seek(long offset, SeekOrigin loc) { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); if (offset > MemStreamMaxLength) throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_StreamLength); @@ -630,7 +634,7 @@ namespace System.IO public virtual byte[] ToArray() { byte[] copy = new byte[_length - _origin]; - Buffer.InternalBlockCopy(_buffer, _origin, copy, 0, _length - _origin); + Buffer.BlockCopy(_buffer, _origin, copy, 0, _length - _origin); return copy; } @@ -645,7 +649,7 @@ namespace System.IO if (buffer.Length - offset < count) throw new ArgumentException(SR.Argument_InvalidOffLen); - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); EnsureWriteable(); int i = _position + count; @@ -673,7 +677,7 @@ namespace System.IO _buffer[_position + byteCount] = buffer[offset + byteCount]; } else - Buffer.InternalBlockCopy(buffer, offset, _buffer, _position, count); + Buffer.BlockCopy(buffer, offset, _buffer, _position, count); _position = i; } @@ -690,7 +694,7 @@ namespace System.IO if (!_isOpen) { - __Error.StreamIsClosed(); + throw Error.GetStreamIsClosed(); } EnsureWriteable(); @@ -786,7 +790,7 @@ namespace System.IO public override void WriteByte(byte value) { - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); EnsureWriteable(); if (_position >= _length) @@ -812,7 +816,7 @@ namespace System.IO if (stream == null) throw new ArgumentNullException(nameof(stream), SR.ArgumentNull_Stream); - if (!_isOpen) __Error.StreamIsClosed(); + if (!_isOpen) throw Error.GetStreamIsClosed(); stream.Write(_buffer, _origin, _length - _origin); } } diff --git a/src/mscorlib/src/System/IO/Stream.cs b/src/mscorlib/src/System/IO/Stream.cs index b8df5aa..6715497 100644 --- a/src/mscorlib/src/System/IO/Stream.cs +++ b/src/mscorlib/src/System/IO/Stream.cs @@ -284,7 +284,7 @@ namespace System.IO byte[] buffer, int offset, int count, AsyncCallback callback, Object state, bool serializeAsynchronously, bool apm) { - if (!CanRead) __Error.ReadNotSupported(); + if (!CanRead) throw Error.GetReadNotSupported(); // To avoid a race with a stream's position pointer & generating race conditions // with internal buffer indexes in our own streams that @@ -449,7 +449,7 @@ namespace System.IO byte[] buffer, int offset, int count, AsyncCallback callback, Object state, bool serializeAsynchronously, bool apm) { - if (!CanWrite) __Error.WriteNotSupported(); + if (!CanWrite) throw Error.GetWriteNotSupported(); // To avoid a race condition with a stream's position pointer & generating conditions // with internal buffer indexes in our own streams that @@ -950,7 +950,7 @@ namespace System.IO public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state) { - if (!CanRead) __Error.ReadNotSupported(); + if (!CanRead) throw Error.GetReadNotSupported(); return BlockingBeginRead(buffer, offset, count, callback, state); } @@ -965,7 +965,7 @@ namespace System.IO public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, Object state) { - if (!CanWrite) __Error.WriteNotSupported(); + if (!CanWrite) throw Error.GetWriteNotSupported(); return BlockingBeginWrite(buffer, offset, count, callback, state); } @@ -1105,10 +1105,10 @@ namespace System.IO { SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult; if (ar == null || ar._isWrite) - __Error.WrongAsyncResult(); + throw new ArgumentException(SR.Arg_WrongAsyncResult); if (ar._endXxxCalled) - __Error.EndReadCalledTwice(); + throw new ArgumentException(SR.InvalidOperation_EndReadCalledMultiple); ar._endXxxCalled = true; @@ -1120,10 +1120,10 @@ namespace System.IO { SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult; if (ar == null || !ar._isWrite) - __Error.WrongAsyncResult(); + throw new ArgumentException(SR.Arg_WrongAsyncResult); if (ar._endXxxCalled) - __Error.EndWriteCalledTwice(); + throw new ArgumentException(SR.InvalidOperation_EndWriteCalledMultiple); ar._endXxxCalled = true; diff --git a/src/mscorlib/src/System/IO/StreamReader.cs b/src/mscorlib/src/System/IO/StreamReader.cs index 092bf69..69cd3e0 100644 --- a/src/mscorlib/src/System/IO/StreamReader.cs +++ b/src/mscorlib/src/System/IO/StreamReader.cs @@ -287,7 +287,7 @@ namespace System.IO public bool EndOfStream { get { if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -302,7 +302,7 @@ namespace System.IO public override int Peek() { if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -315,7 +315,7 @@ namespace System.IO public override int Read() { if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -337,7 +337,7 @@ namespace System.IO throw new ArgumentException(SR.Argument_InvalidOffLen); if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -351,7 +351,7 @@ namespace System.IO if (n == 0) break; // We're at EOF if (n > count) n = count; if (!readToUserBuffer) { - Buffer.InternalBlockCopy(charBuffer, charPos * 2, buffer, (index + charsRead) * 2, n*2); + Buffer.BlockCopy(charBuffer, charPos * 2, buffer, (index + charsRead) * 2, n*2); charPos += n; } charsRead += n; @@ -369,7 +369,7 @@ namespace System.IO public override String ReadToEnd() { if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -394,7 +394,7 @@ namespace System.IO throw new ArgumentException(SR.Argument_InvalidOffLen); if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -405,7 +405,7 @@ namespace System.IO private void CompressBuffer(int n) { Debug.Assert(byteLen >= n, "CompressBuffer was called with a number of bytes greater than the current buffer length. Are two threads using this StreamReader at the same time?"); - Buffer.InternalBlockCopy(byteBuffer, n, byteBuffer, 0, byteLen - n); + Buffer.BlockCopy(byteBuffer, n, byteBuffer, 0, byteLen - n); byteLen -= n; } @@ -704,7 +704,7 @@ namespace System.IO public override String ReadLine() { if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -755,7 +755,7 @@ namespace System.IO return base.ReadLineAsync(); if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -834,7 +834,7 @@ namespace System.IO return base.ReadToEndAsync(); if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -876,7 +876,7 @@ namespace System.IO return base.ReadAsync(buffer, index, count); if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); @@ -1027,7 +1027,7 @@ namespace System.IO if (!readToUserBuffer) { - Buffer.InternalBlockCopy(charBuffer, charPos * 2, buffer, (index + charsRead) * 2, n * 2); + Buffer.BlockCopy(charBuffer, charPos * 2, buffer, (index + charsRead) * 2, n * 2); charPos += n; } @@ -1061,7 +1061,7 @@ namespace System.IO return base.ReadBlockAsync(buffer, index, count); if (stream == null) - __Error.ReaderClosed(); + throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); CheckAsyncTaskInProgress(); diff --git a/src/mscorlib/src/System/IO/__Error.cs b/src/mscorlib/src/System/IO/__Error.cs deleted file mode 100644 index ea58db3..0000000 --- a/src/mscorlib/src/System/IO/__Error.cs +++ /dev/null @@ -1,81 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -/*============================================================ -** -** -** -** -** -** Purpose: Centralized error methods for the IO package. -** Mostly useful for translating Win32 HRESULTs into meaningful -** error strings & exceptions. -** -** -===========================================================*/ - -using System; -using System.Runtime.InteropServices; -using Win32Native = Microsoft.Win32.Win32Native; -using System.Text; -using System.Globalization; -using System.Security; - -namespace System.IO -{ - internal static class __Error - { - internal static void EndOfFile() - { - throw new EndOfStreamException(SR.IO_EOF_ReadBeyondEOF); - } - - internal static void FileNotOpen() - { - throw new ObjectDisposedException(null, SR.ObjectDisposed_FileClosed); - } - - internal static void StreamIsClosed() - { - throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed); - } - - internal static void MemoryStreamNotExpandable() - { - throw new NotSupportedException(SR.NotSupported_MemStreamNotExpandable); - } - - internal static void ReaderClosed() - { - throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed); - } - - internal static void ReadNotSupported() - { - throw new NotSupportedException(SR.NotSupported_UnreadableStream); - } - - internal static void WrongAsyncResult() - { - throw new ArgumentException(SR.Arg_WrongAsyncResult); - } - - internal static void EndReadCalledTwice() - { - // Should ideally be InvalidOperationExc but we can't maitain parity with Stream and FileStream without some work - throw new ArgumentException(SR.InvalidOperation_EndReadCalledMultiple); - } - - internal static void EndWriteCalledTwice() - { - // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and FileStream without some work - throw new ArgumentException(SR.InvalidOperation_EndWriteCalledMultiple); - } - - internal static void WriteNotSupported() - { - throw new NotSupportedException(SR.NotSupported_UnwritableStream); - } - } -} diff --git a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs index ba50616..6158309 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs +++ b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs @@ -461,7 +461,7 @@ namespace System { int n = stream.Read(buffer, index, count); if (n == 0) - __Error.EndOfFile(); + throw Error.GetEndOfFile(); int end = index + n; for (; index < end; index++) diff --git a/src/vm/comutilnative.cpp b/src/vm/comutilnative.cpp index eab048d..f9f159a 100644 --- a/src/vm/comutilnative.cpp +++ b/src/vm/comutilnative.cpp @@ -803,54 +803,6 @@ FCIMPL5(VOID, Buffer::BlockCopy, ArrayBase *src, int srcOffset, ArrayBase *dst, FCIMPLEND -// InternalBlockCopy -// This method from one primitive array to another based -// upon an offset into each an a byte count. -FCIMPL5(VOID, Buffer::InternalBlockCopy, ArrayBase *src, int srcOffset, ArrayBase *dst, int dstOffset, int count) -{ - FCALL_CONTRACT; - - // @TODO: We should consider writing this in managed code. We probably - // cannot easily do this though - how do we get at the array's data? - - // Unfortunately, we must do a check to make sure we're writing within - // the bounds of the array. This will ensure that we don't overwrite - // memory elsewhere in the system nor do we write out junk. This can - // happen if multiple threads interact with our IO classes simultaneously - // without being threadsafe. Throw here. - // Unfortunately this even applies to setting our internal buffers to - // null. We don't want to debug races between Close and Read or Write. - if (src == NULL || dst == NULL) - FCThrowResVoid(kIndexOutOfRangeException, W("IndexOutOfRange_IORaceCondition")); - - SIZE_T srcLen = src->GetNumComponents() * src->GetComponentSize(); - SIZE_T dstLen = srcLen; - if (src != dst) - dstLen = dst->GetNumComponents() * dst->GetComponentSize(); - - if (srcOffset < 0 || dstOffset < 0 || count < 0) - FCThrowResVoid(kIndexOutOfRangeException, W("IndexOutOfRange_IORaceCondition")); - - if (srcLen < (SIZE_T)srcOffset + (SIZE_T)count || dstLen < (SIZE_T)dstOffset + (SIZE_T)count) - FCThrowResVoid(kIndexOutOfRangeException, W("IndexOutOfRange_IORaceCondition")); - - _ASSERTE(srcOffset >= 0); - _ASSERTE((src->GetNumComponents() * src->GetComponentSize()) - (unsigned) srcOffset >= (unsigned) count); - _ASSERTE((dst->GetNumComponents() * dst->GetComponentSize()) - (unsigned) dstOffset >= (unsigned) count); - _ASSERTE(dstOffset >= 0); - _ASSERTE(count >= 0); - - // Copy the data. -#if defined(_AMD64_) && !defined(PLATFORM_UNIX) - JIT_MemCpy(dst->GetDataPtr() + dstOffset, src->GetDataPtr() + srcOffset, count); -#else - memmove(dst->GetDataPtr() + dstOffset, src->GetDataPtr() + srcOffset, count); -#endif - - FC_GC_POLL(); -} -FCIMPLEND - void QCALLTYPE MemoryNative::Clear(void *dst, size_t length) { QCALL_CONTRACT; diff --git a/src/vm/comutilnative.h b/src/vm/comutilnative.h index 2ecaaa6..b1e47be 100644 --- a/src/vm/comutilnative.h +++ b/src/vm/comutilnative.h @@ -89,7 +89,6 @@ public: // This method from one primitive array to another based // upon an offset into each an a byte count. static FCDECL5(VOID, BlockCopy, ArrayBase *src, int srcOffset, ArrayBase *dst, int dstOffset, int count); - static FCDECL5(VOID, InternalBlockCopy, ArrayBase *src, int srcOffset, ArrayBase *dst, int dstOffset, int count); static FCDECL2(FC_UINT8_RET, GetByte, ArrayBase *arrayUNSAFE, INT32 index); static FCDECL3(VOID, SetByte, ArrayBase *arrayUNSAFE, INT32 index, UINT8 bData); static FCDECL1(FC_BOOL_RET, IsPrimitiveTypeArray, ArrayBase *arrayUNSAFE); diff --git a/src/vm/ecalllist.h b/src/vm/ecalllist.h index f3118f4..4ee9826 100644 --- a/src/vm/ecalllist.h +++ b/src/vm/ecalllist.h @@ -839,7 +839,6 @@ FCFuncEnd() FCFuncStart(gBufferFuncs) FCFuncElement("BlockCopy", Buffer::BlockCopy) - FCFuncElement("InternalBlockCopy", Buffer::InternalBlockCopy) FCFuncElement("_GetByte", Buffer::GetByte) FCFuncElement("_SetByte", Buffer::SetByte) FCFuncElement("IsPrimitiveTypeArray", Buffer::IsPrimitiveTypeArray) -- 2.7.4