-
-
Notifications
You must be signed in to change notification settings - Fork 103
Clear pool buffer array on disposal in ZCharArray
#519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,9 @@ public struct ZCharArray : IDisposable | |
| private static readonly ArrayPool<char> | ||
| Pool = ArrayPool<char>.Create(MaxBufferCapacity, 100); | ||
|
|
||
| private char[]? _bufferArray; | ||
| private char[] _bufferArray; | ||
| private int _currentLength; | ||
| private bool _isDisposed; | ||
|
|
||
| /// <summary> | ||
| /// The default capacity of the array. | ||
|
|
@@ -48,6 +49,7 @@ public ZCharArray(int length) | |
| { | ||
| _bufferArray = Pool.Rent(length); | ||
| _currentLength = 0; | ||
| _isDisposed = false; | ||
|
axunonb marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -96,7 +98,7 @@ public int Capacity | |
| get | ||
| { | ||
| ThrowIfDisposed(); | ||
| return _bufferArray!.Length; | ||
| return _bufferArray.Length; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -119,7 +121,7 @@ public void Reset() | |
| private void Grow(int length) | ||
| { | ||
| var newArray = Pool.Rent(length); | ||
| Array.Copy(_bufferArray!, newArray, Math.Min(_bufferArray!.Length, length)); | ||
| Array.Copy(_bufferArray, newArray, Math.Min(_bufferArray.Length, length)); | ||
| Pool.Return(_bufferArray); | ||
|
axunonb marked this conversation as resolved.
Outdated
|
||
| _bufferArray = newArray; | ||
| } | ||
|
|
@@ -133,7 +135,7 @@ public void Write(Span<char> data) | |
| { | ||
| ThrowIfDisposed(); | ||
| GrowBufferIfNeeded(data.Length); | ||
| data.CopyTo(_bufferArray!.AsSpan(_currentLength, data.Length)); | ||
| data.CopyTo(_bufferArray.AsSpan(_currentLength, data.Length)); | ||
| _currentLength += data.Length; | ||
| } | ||
|
|
||
|
|
@@ -146,7 +148,7 @@ public void Write(ReadOnlySpan<char> data) | |
| { | ||
| ThrowIfDisposed(); | ||
| GrowBufferIfNeeded(data.Length); | ||
| data.CopyTo(_bufferArray!.AsSpan(_currentLength, data.Length)); | ||
| data.CopyTo(_bufferArray.AsSpan(_currentLength, data.Length)); | ||
| _currentLength += data.Length; | ||
| } | ||
|
|
||
|
|
@@ -159,7 +161,7 @@ public void Write(string data) | |
| { | ||
| ThrowIfDisposed(); | ||
| GrowBufferIfNeeded(data.Length); | ||
| data.AsSpan().CopyTo(_bufferArray!.AsSpan(_currentLength, data.Length)); | ||
| data.AsSpan().CopyTo(_bufferArray.AsSpan(_currentLength, data.Length)); | ||
| _currentLength += data.Length; | ||
| } | ||
|
|
||
|
|
@@ -172,7 +174,7 @@ public void Write(char c) | |
| { | ||
| ThrowIfDisposed(); | ||
| GrowBufferIfNeeded(1); | ||
| _bufferArray![_currentLength++] = c; | ||
| _bufferArray[_currentLength++] = c; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -188,7 +190,7 @@ public void Write(char c, int count) | |
|
|
||
| for (var i = 0; i < count; i++) | ||
| { | ||
| _bufferArray![_currentLength++] = c; | ||
| _bufferArray[_currentLength++] = c; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -246,7 +248,7 @@ private void GrowBufferIfNeeded(int dataLength) | |
| /// <summary> | ||
| /// Returns <see langword="true"/> if the array has been disposed. | ||
| /// </summary> | ||
| public bool IsDisposed => _bufferArray is null; | ||
| public bool IsDisposed => _isDisposed; | ||
|
|
||
| private void ThrowIfDisposed() | ||
| { | ||
|
|
@@ -261,17 +263,17 @@ private void ThrowIfDisposed() | |
| public override string ToString() | ||
| { | ||
| ThrowIfDisposed(); | ||
| return new string(_bufferArray!, 0, _currentLength); | ||
| return new string(_bufferArray, 0, _currentLength); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Disposes the array, returning it to the <see cref="ArrayPool{T}"/>. | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| if (IsDisposed) return; | ||
| if (_isDisposed) return; | ||
|
|
||
| Pool.Return(_bufferArray!); | ||
| _bufferArray = null; | ||
| Pool.Return(_bufferArray, clearArray: true); | ||
|
||
| _isDisposed = true; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.