Skip to content
Permalink
Browse files

Adds support for stream id to be explicitly set (#70)

  • Loading branch information...
tgnm authored and benmwatson committed Jun 11, 2019
1 parent 8a10b24 commit c21b25b72eafd0b6916e4afe84289a2b6cddbf8f
Showing with 106 additions and 11 deletions.
  1. +33 −6 src/RecyclableMemoryStream.cs
  2. +73 −5 src/RecyclableMemoryStreamManager.cs
@@ -156,15 +156,32 @@ internal RecyclableMemoryStreamManager MemoryManager
/// </summary>
/// <param name="memoryManager">The memory manager</param>
public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager)
: this(memoryManager, null, 0, null) { }
: this(memoryManager, Guid.NewGuid(), null, 0, null) { }

/// <summary>
/// Allocate a new RecyclableMemoryStream object.
/// </summary>
/// <param name="memoryManager">The memory manager</param>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, Guid id)
: this(memoryManager, id, null, 0, null) { }

/// <summary>
/// Allocate a new RecyclableMemoryStream object
/// </summary>
/// <param name="memoryManager">The memory manager</param>
/// <param name="tag">A string identifying this stream for logging and debugging purposes</param>
public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, string tag)
: this(memoryManager, tag, 0, null) { }
: this(memoryManager, Guid.NewGuid(), tag, 0, null) { }

/// <summary>
/// Allocate a new RecyclableMemoryStream object
/// </summary>
/// <param name="memoryManager">The memory manager</param>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A string identifying this stream for logging and debugging purposes</param>
public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, Guid id, string tag)
: this(memoryManager, id, tag, 0, null) { }

/// <summary>
/// Allocate a new RecyclableMemoryStream object
@@ -173,21 +190,31 @@ public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, strin
/// <param name="tag">A string identifying this stream for logging and debugging purposes</param>
/// <param name="requestedSize">The initial requested size to prevent future allocations</param>
public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, string tag, int requestedSize)
: this(memoryManager, tag, requestedSize, null) { }
: this(memoryManager, Guid.NewGuid(), tag, requestedSize, null) { }

/// <summary>
/// Allocate a new RecyclableMemoryStream object
/// </summary>
/// <param name="memoryManager">The memory manager</param>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A string identifying this stream for logging and debugging purposes</param>
/// <param name="requestedSize">The initial requested size to prevent future allocations</param>
public RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, Guid id, string tag, int requestedSize)
: this(memoryManager, id, tag, requestedSize, null) { }

/// <summary>
/// Allocate a new RecyclableMemoryStream object
/// </summary>
/// <param name="memoryManager">The memory manager</param>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A string identifying this stream for logging and debugging purposes</param>
/// <param name="requestedSize">The initial requested size to prevent future allocations</param>
/// <param name="initialLargeBuffer">An initial buffer to use. This buffer will be owned by the stream and returned to the memory manager upon Dispose.</param>
internal RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, string tag, int requestedSize,
byte[] initialLargeBuffer)
internal RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, Guid id, string tag, int requestedSize, byte[] initialLargeBuffer)
: base(emptyArray)
{
this.memoryManager = memoryManager;
this.id = Guid.NewGuid();
this.id = id;
this.tag = tag;

if (requestedSize < memoryManager.BlockSize)
@@ -578,6 +578,16 @@ public MemoryStream GetStream()
return new RecyclableMemoryStream(this);
}

/// <summary>
/// Retrieve a new MemoryStream object with no tag and a default initial capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(Guid id)
{
return new RecyclableMemoryStream(this, id);
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and a default initial capacity.
/// </summary>
@@ -588,9 +598,21 @@ public MemoryStream GetStream(string tag)
return new RecyclableMemoryStream(this, tag);
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and a default initial capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(Guid id, string tag)
{
return new RecyclableMemoryStream(this, id, tag);
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and at least the given capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <returns>A MemoryStream.</returns>
@@ -599,6 +621,18 @@ public MemoryStream GetStream(string tag, int requiredSize)
return new RecyclableMemoryStream(this, tag, requiredSize);
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and at least the given capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(Guid id, string tag, int requiredSize)
{
return new RecyclableMemoryStream(this, id, tag, requiredSize);
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and at least the given capacity, possibly using
/// a single continugous underlying buffer.
@@ -607,36 +641,55 @@ public MemoryStream GetStream(string tag, int requiredSize)
/// where the initial size is known and it is desirable to avoid copying data between the smaller underlying
/// buffers to a single large one. This is most helpful when you know that you will always call GetBuffer
/// on the underlying stream.</remarks>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <param name="asContiguousBuffer">Whether to attempt to use a single contiguous buffer.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(string tag, int requiredSize, bool asContiguousBuffer)
public MemoryStream GetStream(Guid id, string tag, int requiredSize, bool asContiguousBuffer)
{
if (!asContiguousBuffer || requiredSize <= this.BlockSize)
{
return this.GetStream(tag, requiredSize);
return this.GetStream(id, tag, requiredSize);
}

return new RecyclableMemoryStream(this, tag, requiredSize, this.GetLargeBuffer(requiredSize, tag));
return new RecyclableMemoryStream(this, id, tag, requiredSize, this.GetLargeBuffer(requiredSize, tag));
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and at least the given capacity, possibly using
/// a single continugous underlying buffer.
/// </summary>
/// <remarks>Retrieving a MemoryStream which provides a single contiguous buffer can be useful in situations
/// where the initial size is known and it is desirable to avoid copying data between the smaller underlying
/// buffers to a single large one. This is most helpful when you know that you will always call GetBuffer
/// on the underlying stream.</remarks>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <param name="asContiguousBuffer">Whether to attempt to use a single contiguous buffer.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(string tag, int requiredSize, bool asContiguousBuffer)
{
return GetStream(Guid.NewGuid(), tag, requiredSize, asContiguousBuffer);
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <param name="offset">The offset from the start of the buffer to copy from.</param>
/// <param name="count">The number of bytes to copy from the buffer.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(string tag, byte[] buffer, int offset, int count)
public MemoryStream GetStream(Guid id, string tag, byte[] buffer, int offset, int count)
{
RecyclableMemoryStream stream = null;
try
{
stream = new RecyclableMemoryStream(this, tag, count);
stream = new RecyclableMemoryStream(this, id, tag, count);
stream.Write(buffer, offset, count);
stream.Position = 0;
return stream;
@@ -648,6 +701,21 @@ public MemoryStream GetStream(string tag, byte[] buffer, int offset, int count)
}
}

/// <summary>
/// Retrieve a new MemoryStream object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <param name="offset">The offset from the start of the buffer to copy from.</param>
/// <param name="count">The number of bytes to copy from the buffer.</param>
/// <returns>A MemoryStream.</returns>
public MemoryStream GetStream(string tag, byte[] buffer, int offset, int count)
{
return GetStream(Guid.NewGuid(), tag, buffer, offset, count);
}

/// <summary>
/// Triggered when a new block is created.
/// </summary>

0 comments on commit c21b25b

Please sign in to comment.
You can’t perform that action at this time.