<< protected with "checked", where needed.

This commit is contained in:
mlemanczyk committed 2023-10-07 14:04:33 +02:00
1 parent 262590ed6e
commit 39d63d1d65
7 files changed
+27 -27

No files matched your search

@@ -37,7 +37,7 @@ namespace Recyclable.Collections.Benchmarks.POC
[GlobalSetup]
public void Setup()
{
_cutoff = BlockSize << 4;
_cutoff = checked((long)BlockSize << 4);
_increment = Vector128.Create(-BlockSize, 1L);
_lastCaseIndex = 0;
}
@@ -54,7 +54,7 @@ namespace Recyclable.Collections.Benchmarks.POC
int result = 1;
while (result < value)
{
result <<= 1;
result = checked(result << 1);
}
return result;
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Recyclable.Collections
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long DivMod(long number, int divider, out long remainder)
{
if (number >= divider << 4)
if (number >= checked((long)divider << 4))
{
return Math.DivRem(number, divider, out remainder);
}
@@ -15,7 +15,7 @@ namespace Recyclable.Collections
// TODO: Measure performance
// T[] newMemoryBlock = RecyclableListHelpers<T>._arrayPool.Rent(_capacity <<= 1);
T[] oldMemoryBlock = list._memoryBlock;
list._memoryBlock = (list._capacity <<= 1) < RecyclableDefaults.MinPooledArrayLength
list._memoryBlock = checked(list._capacity <<= 1) < RecyclableDefaults.MinPooledArrayLength
? new T[list._capacity]
: RecyclableArrayPool<T>.RentShared(list._capacity);
@@ -192,7 +192,7 @@ namespace Recyclable.Collections
}
}
return requiredBlockCount << minBlockSizePow2Shift;
return checked(requiredBlockCount << minBlockSizePow2Shift);
}
#pragma warning disable CA2208
@@ -28,7 +28,7 @@ namespace Recyclable.Collections
long index = Array.IndexOf(list._memoryBlocks[blockIndex], itemToFind, 0, list._blockSize);
if (index >= 0 && (!context._isItemFound || index < context.FoundItemIndex))
{
ExchangeFoundItemIndexIfLower(context, blockIndex, ((long)blockIndex << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex, checked((long)blockIndex << list._blockSizePow2BitShift) + index);
return;
}
else if (context._isItemFound)
@@ -41,7 +41,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 1], itemToFind, 0, list._blockSize);
if (index >= 0)
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 1, ((long)(blockIndex + 1) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 1, checked((long)(blockIndex + 1) << list._blockSizePow2BitShift) + index);
return;
}
else if (context._isItemFound)
@@ -56,7 +56,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 2], itemToFind, 0, list._blockSize);
if (index >= 0 && (!context._isItemFound || index < context.FoundItemIndex))
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 2, ((long)(blockIndex + 2) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 2, checked((long)(blockIndex + 2) << list._blockSizePow2BitShift) + index);
return;
}
else if (context._isItemFound)
@@ -71,7 +71,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 3], itemToFind, 0, list._blockSize);
if (!context._isItemFound && index >= 0)
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 3, ((long)(blockIndex + 3) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 3, checked((long)(blockIndex + 3) << list._blockSizePow2BitShift) + index);
}
#endif
@@ -81,7 +81,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 4], itemToFind, 0, list._blockSize);
if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex))
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 4, ((long)(blockIndex + 4) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 4, checked((long)(blockIndex + 4) << list._blockSizePow2BitShift) + index);
}
#endif
@@ -91,7 +91,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 5], itemToFind, 0, list._blockSize);
if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex))
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 5, ((long)(blockIndex + 5) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 5, checked((long)(blockIndex + 5) << list._blockSizePow2BitShift) + index);
}
#endif
@@ -101,7 +101,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 6], itemToFind, 0, list._blockSize);
if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex))
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 6, ((long)(blockIndex + 6) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 6, checked((long)(blockIndex + 6) << list._blockSizePow2BitShift) + index);
}
#endif
@@ -111,7 +111,7 @@ namespace Recyclable.Collections
index = Array.IndexOf(list._memoryBlocks[blockIndex + 7], itemToFind, 0, list._blockSize);
if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex))
{
ExchangeFoundItemIndexIfLower(context, blockIndex + 7, ((long)(blockIndex + 7) << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex + 7, checked((long)(blockIndex + 7) << list._blockSizePow2BitShift) + index);
}
#endif
@@ -157,7 +157,7 @@ namespace Recyclable.Collections
long index = Array.IndexOf(list._memoryBlocks[blockIndex], itemToFind, 0, list._blockSize);
if (!context._isItemFound && index >= 0)
{
ExchangeFoundItemIndexIfLower(context, blockIndex, ((long)blockIndex << list._blockSizePow2BitShift) + index);
ExchangeFoundItemIndexIfLower(context, blockIndex, checked((long)blockIndex << list._blockSizePow2BitShift) + index);
}
}
catch (Exception e)
@@ -214,8 +214,8 @@ namespace Recyclable.Collections
// & WAS SLOWER
// int blockSize = _blockSize;
// & WAS SLOWER
// while ((blockIndex << list._blockSizePow2BitShift) + itemIndex > step)
//long blockSize = list._blockSize << 1;
// while (checked((long)blockIndex << list._blockSizePow2BitShift) + itemIndex > step)
//long blockSize = checked((long)list._blockSize << 1);
int lastBlockWithData = list._lastBlockWithData - BlocksForEachTask;
while (blockIndex < lastBlockWithData)
{
@@ -265,7 +265,7 @@ namespace Recyclable.Collections
int itemIndex = Array.IndexOf(list._memoryBlocks[list._lastBlockWithData], item, 0, list._nextItemIndex > 0 ? list._nextItemIndex : list._blockSize);
if (!context._isItemFound && itemIndex >= 0)
{
ExchangeFoundItemIndexIfLower(context, blockIndex, ((long)blockIndex << list._blockSizePow2BitShift) + itemIndex);
ExchangeFoundItemIndexIfLower(context, blockIndex, checked((long)blockIndex << list._blockSizePow2BitShift) + itemIndex);
}
}
@@ -307,7 +307,7 @@ namespace Recyclable.Collections
itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex], item, 0, list._blockSize);
if (itemIndex >= 0)
{
return itemIndex + ((long)blockIndex << list._blockSizePow2BitShift);
return itemIndex + checked((long)blockIndex << list._blockSizePow2BitShift);
}
if (blockIndex + 1 < list._lastBlockWithData)
@@ -315,7 +315,7 @@ namespace Recyclable.Collections
itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex + 1], item, 0, list._blockSize);
if (itemIndex >= 0)
{
return itemIndex + ((long)(blockIndex + 1)<< list._blockSizePow2BitShift);
return itemIndex + checked((long)(blockIndex + 1)<< list._blockSizePow2BitShift);
}
if (blockIndex + 2 < list._lastBlockWithData)
@@ -323,7 +323,7 @@ namespace Recyclable.Collections
itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex + 2], item, 0, list._blockSize);
if (itemIndex >= 0)
{
return itemIndex + ((long)(blockIndex + 2)<< list._blockSizePow2BitShift);
return itemIndex + checked((long)(blockIndex + 2)<< list._blockSizePow2BitShift);
}
if (blockIndex + 3 < list._lastBlockWithData)
@@ -331,7 +331,7 @@ namespace Recyclable.Collections
itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex + 3], item, 0, list._blockSize);
if (itemIndex >= 0)
{
return itemIndex + ((long)(blockIndex + 3)<< list._blockSizePow2BitShift);
return itemIndex + checked((long)(blockIndex + 3)<< list._blockSizePow2BitShift);
}
}
}
@@ -342,7 +342,7 @@ namespace Recyclable.Collections
? Array.IndexOf(memoryBlocksSpan[list._lastBlockWithData], item, 0, list._nextItemIndex)
: Array.IndexOf(memoryBlocksSpan[list._lastBlockWithData], item, 0, list._blockSize);
return itemIndex >= 0 ? itemIndex + ((long)list._lastBlockWithData << list._blockSizePow2BitShift) : RecyclableDefaults.ItemNotFoundIndexLong;
return itemIndex >= 0 ? itemIndex + checked((long)list._lastBlockWithData << list._blockSizePow2BitShift) : RecyclableDefaults.ItemNotFoundIndexLong;
}
}
}
@@ -183,7 +183,7 @@ namespace Recyclable.CollectionsTests
{
// Prepare
using var list = new RecyclableList<long>();
itemsCount <<= 1;
itemsCount = checked(itemsCount << 1);
// Act
if (testCase.Contains("Array[", StringComparison.OrdinalIgnoreCase))
@@ -275,7 +275,7 @@ namespace Recyclable.CollectionsTests
// Prepare
using var list = new RecyclableList<long>();
testData = testData.Concat(testData.Reverse());
itemsCount <<= 1;
itemsCount = checked(itemsCount << 1);
// Act
foreach (var index in testData)
@@ -340,7 +340,7 @@ namespace Recyclable.CollectionsTests
{
// Prepare
using var list = new RecyclableList<long>();
itemsCount <<= 1;
itemsCount = checked(itemsCount << 1);
// Act
list.AddRange(testData);
@@ -416,7 +416,7 @@ namespace Recyclable.CollectionsTests
{
// Prepare
testData = testData.Concat(testData.Reverse());
itemsCount <<= 1;
itemsCount = checked(itemsCount << 1);
// Act
using var list = new RecyclableList<long>(testData, itemsCount);
@@ -1633,7 +1633,7 @@ namespace Recyclable.CollectionsTests
{
// Prepare
using var list = new RecyclableList<long>(testData, itemsCount);
var expectedCapacity = (int)BitOperations.RoundUpToPowerOf2((uint)itemsCount << 1);
var expectedCapacity = (int)BitOperations.RoundUpToPowerOf2(checked((uint)itemsCount << 1));
expectedCapacity = expectedCapacity >= RecyclableDefaults.InitialCapacity ? expectedCapacity : RecyclableDefaults.InitialCapacity;
// Act