Skip to content
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

BlobBuilder.LinkSuffix/LinkPrefix need to Free zero length chunks #100039

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public partial class BlobBuilder
private uint _length;

private const uint IsFrozenMask = 0x80000000;
private bool IsHead => (_length & IsFrozenMask) == 0;
internal bool IsHead => (_length & IsFrozenMask) == 0;
private int Length => (int)(_length & ~IsFrozenMask);
private uint FrozenLength => _length | IsFrozenMask;
private Span<byte> Span => _buffer.AsSpan(0, Length);
Expand Down Expand Up @@ -97,8 +97,7 @@ public void Clear()
{
if (chunk != this)
{
chunk.ClearChunk();
chunk.FreeChunk();
chunk.ClearAndFreeChunk();
}
}

Expand Down Expand Up @@ -396,6 +395,7 @@ public void LinkPrefix(BlobBuilder prefix)
// avoid chaining empty chunks:
if (prefix.Count == 0)
{
prefix.ClearAndFreeChunk();
return;
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -456,6 +456,7 @@ public void LinkSuffix(BlobBuilder suffix)
// avoid chaining empty chunks:
if (suffix.Count == 0)
{
suffix.ClearAndFreeChunk();
return;
}

Expand Down Expand Up @@ -1177,5 +1178,11 @@ private static string Display(byte[] bytes, int length)
BitConverter.ToString(bytes, 0, length) :
BitConverter.ToString(bytes, 0, MaxDisplaySize / 2) + "-...-" + BitConverter.ToString(bytes, length - MaxDisplaySize / 2, MaxDisplaySize / 2);
}

private void ClearAndFreeChunk()
{
ClearChunk();
FreeChunk();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1090,5 +1090,31 @@ public void PrematureEndOfStream()

AssertEx.Equal(sourceArray, builder.ToArray());
}

[Fact]
public void LinkEmptySuffixAndPrefixShouldFreeThem()
{
var b1 = PooledBlobBuilder.GetInstance();
var b2 = PooledBlobBuilder.GetInstance();
var b3 = PooledBlobBuilder.GetInstance();
var b4 = PooledBlobBuilder.GetInstance();
var b5 = PooledBlobBuilder.GetInstance();

b1.WriteBytes(1, 1);
b2.WriteBytes(1, 1);
b3.WriteBytes(1, 1);

b1.LinkSuffix(b2);
Assert.False(b2.IsHead);

b1.LinkPrefix(b3);
Assert.False(b3.IsHead);

b1.LinkSuffix(b4);
Assert.True(b4.IsHead);

b1.LinkPrefix(b5);
Assert.True(b4.IsHead);
}
}
}
Loading