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

Handling null in Transfer method #5

Merged
merged 7 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
if: ${{ inputs.publish }}
run: |
dotnet pack -c ${{ env.BUILD_CONFIG }} --no-build
dotnet nuget push build_output/packages/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push build_output/packages/*.nupkg -k ${{ secrets.NUGETTEST_API_KEY }} -s https://apiint.nugettest.org/v3/index.json
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<GeneratePackageOnBuild Condition="'$(Configuration)'=='Package'">True</GeneratePackageOnBuild>
<IncludeSymbols>true</IncludeSymbols>

<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>1.0.1-preview.1</PackageVersion>
<Version>$(PackageVersion)</Version>
</PropertyGroup>

Expand Down
132 changes: 79 additions & 53 deletions src/DotNetty.Common/ThreadLocalPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,83 +236,109 @@ internal void Add(DefaultHandle handle)
// transfer as many items as we can from this queue to the stack, returning true if any were transferred
internal bool Transfer(Stack dst)
{
Link head = this.head.link;
if (head == null)
try
{
return false;
}
Link head = this.head?.link;
if (head == null)
{
return false;
}

if (head.ReadIndex == LinkCapacity)
{
if (head.next == null)
if (head.ReadIndex == LinkCapacity)
{
if (head.next == null)
{
return false;
}

this.head.link = head = head.next;
}

int srcStart = head.ReadIndex;
int srcEnd = head.WriteIndex;
int srcSize = srcEnd - srcStart;
if (srcSize == 0)
{
return false;
}
this.head.link = head = head.next;
}

int srcStart = head.ReadIndex;
int srcEnd = head.WriteIndex;
int srcSize = srcEnd - srcStart;
if (srcSize == 0)
{
return false;
}
if (dst?.elements == null)
{
return false;
}

int dstSize = dst.size;
int expectedCapacity = dstSize + srcSize;
int dstSize = dst.size;
int expectedCapacity = dstSize + srcSize;

if (expectedCapacity > dst.elements.Length)
{
int actualCapacity = dst.IncreaseCapacity(expectedCapacity);
srcEnd = Math.Min(srcStart + actualCapacity - dstSize, srcEnd);
}
if (expectedCapacity > dst.elements.Length)
{
int actualCapacity = dst.IncreaseCapacity(expectedCapacity);
srcEnd = Math.Min(srcStart + actualCapacity - dstSize, srcEnd);
}

if (srcStart != srcEnd)
{
DefaultHandle[] srcElems = head.elements;
DefaultHandle[] dstElems = dst.elements;
int newDstSize = dstSize;
for (int i = srcStart; i < srcEnd; i++)
if (srcStart != srcEnd)
{
DefaultHandle element = srcElems[i];
if (element.recycleId == 0)
DefaultHandle[] srcElems = head.elements;
DefaultHandle[] dstElems = dst.elements;
int newDstSize = dstSize;
if (head.elements == null)
{
element.recycleId = element.lastRecycledId;
return false;
}
else if (element.recycleId != element.lastRecycledId)

for (int i = srcStart; i < srcEnd; i++)
{
throw new InvalidOperationException("recycled already");
DefaultHandle element = srcElems[i];
if (element == null)
{
return false;
}

if (element.recycleId == 0)
{
element.recycleId = element.lastRecycledId;
}
else if (element.recycleId != element.lastRecycledId)
{
throw new InvalidOperationException("recycled already");
}

srcElems[i] = null;

if (dst.DropHandle(element))
{
// Drop the object.
continue;
}

element.Stack = dst;
dstElems[newDstSize++] = element;
}
srcElems[i] = null;

if (dst.DropHandle(element))
if (srcEnd == LinkCapacity && head.next != null)
{
// Drop the object.
continue;
// Add capacity back as the Link is GCed.
this.head.ReclaimSpace(LinkCapacity);
this.head.link = head.next;
}
element.Stack = dst;
dstElems[newDstSize++] = element;
}

if (srcEnd == LinkCapacity && head.next != null)
{
// Add capacity back as the Link is GCed.
this.head.ReclaimSpace(LinkCapacity);
this.head.link = head.next;
}
head.ReadIndex = srcEnd;
if (dst.size == newDstSize)
{
return false;
}

head.ReadIndex = srcEnd;
if (dst.size == newDstSize)
dst.size = newDstSize;
return true;
}
else
{
// The destination stack is full already.
return false;
}
dst.size = newDstSize;
return true;
}
else
catch (NullReferenceException)
{
// The destination stack is full already.
return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would skip this part for now, let's see if null checks helped.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done

Expand Down