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

Fix #29 #32

Merged
merged 1 commit into from
Dec 21, 2020
Merged
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
32 changes: 30 additions & 2 deletions src/DotNetty.Buffers/PoolChunkList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,36 @@ public PoolChunkList(PoolArena<T> arena, PoolChunkList<T> nextList, int minUsage
// freeBytes = 16777216 == freeMaxThreshold: 16777216, usage = 0 < minUsage: 1, chunkSize: 16777216
// At the same time we want to have zero thresholds in case of (maxUsage == 100) and (minUsage == 100).
//
_freeMinThreshold = (maxUsage == 100) ? 0 : (int)(uint)(chunkSize * (100.0d - maxUsage + 0.99999999d) / 100L);
_freeMaxThreshold = (minUsage == 100) ? 0 : (int)(uint)(chunkSize * (100.0d - minUsage + 0.99999999d) / 100L);
_freeMinThreshold = CalculateThresholdWithOverflow(chunkSize, maxUsage);
_freeMaxThreshold = CalculateThresholdWithOverflow(chunkSize, minUsage);
}

// https://github.com/cuteant/SpanNetty/issues/29
private static int CalculateThresholdWithOverflow(int chunkSize, int usage)
{
int freeThreshold;
if (usage == 100)
{
freeThreshold = 0;
}
else
{
var tmp = chunkSize * (100.0d - usage + 0.99999999d) / 100L;
if (tmp <= int.MinValue)
{
freeThreshold = int.MinValue;
}
else if (tmp >= int.MaxValue)
{
freeThreshold = int.MaxValue;
}
else
{
freeThreshold = (int)tmp;
}
}

return freeThreshold;
}

/// Calculates the maximum capacity of a buffer that will ever be possible to allocate out of the {@link PoolChunk}s
Expand Down