Skip to content

Commit

Permalink
Adds support for stream id to be explicitly set (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgnm authored and benmwatson committed Jun 11, 2019
1 parent 8a10b24 commit c21b25b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
39 changes: 33 additions & 6 deletions src/RecyclableMemoryStream.cs
Expand Up @@ -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
Expand All @@ -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)
Expand Down
78 changes: 73 additions & 5 deletions src/RecyclableMemoryStreamManager.cs
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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>
Expand Down

0 comments on commit c21b25b

Please sign in to comment.