-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from Washi1337/feature/rentable-memory-stream…
…-writers Rentable BinaryStreamWriters that write to temporary MemoryStream
- Loading branch information
Showing
5 changed files
with
284 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
|
||
namespace AsmResolver.IO | ||
{ | ||
/// <summary> | ||
/// Provides a pool of reusable instances of <see cref="BinaryStreamWriter"/> that are meant to be used for | ||
/// constructing byte arrays. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class is thread-safe. All threads are allowed to rent and return writers from this pool simultaneously. | ||
/// </remarks> | ||
public class MemoryStreamWriterPool | ||
{ | ||
private readonly ConcurrentQueue<BinaryStreamWriter> _writers = new(); | ||
|
||
/// <summary> | ||
/// Rents a single binary stream writer. | ||
/// </summary> | ||
/// <returns>The writer.</returns> | ||
public RentedWriter Rent() | ||
{ | ||
if (!_writers.TryDequeue(out var writer)) | ||
writer = new BinaryStreamWriter(new MemoryStream()); | ||
|
||
writer.BaseStream.SetLength(0); | ||
return new RentedWriter(this, writer); | ||
} | ||
|
||
private void Return(BinaryStreamWriter writer) => _writers.Enqueue(writer); | ||
|
||
/// <summary> | ||
/// Represents a single instance of a <see cref="BinaryStreamWriter"/> that is rented by a writer pool. | ||
/// </summary> | ||
public ref struct RentedWriter | ||
{ | ||
private bool _isDisposed = false; | ||
private readonly BinaryStreamWriter _writer; | ||
|
||
internal RentedWriter(MemoryStreamWriterPool pool, BinaryStreamWriter writer) | ||
{ | ||
Pool = pool; | ||
_writer = writer; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the pool the writer was rented from. | ||
/// </summary> | ||
public MemoryStreamWriterPool Pool | ||
{ | ||
get; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the writer instance. | ||
/// </summary> | ||
public BinaryStreamWriter Writer | ||
{ | ||
get | ||
{ | ||
if (_isDisposed) | ||
throw new ObjectDisposedException(nameof(Writer)); | ||
return _writer; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the data that was written to the temporary stream. | ||
/// </summary> | ||
/// <returns></returns> | ||
public byte[] GetData() => ((MemoryStream) Writer.BaseStream).ToArray(); | ||
|
||
/// <summary> | ||
/// Returns the stream writer to the pool. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
if (_isDisposed) | ||
return; | ||
|
||
Pool.Return(Writer); | ||
_isDisposed = true; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.