Collections casts fix

This commit is contained in:
Mateusz Karbowiak
2024-10-30 22:18:47 +01:00
parent 02db7d02f2
commit a55866d558
5 changed files with 38 additions and 36 deletions

View File

@@ -66,7 +66,7 @@ public:
/// <param name="initList">The initial values defined in the array.</param> /// <param name="initList">The initial values defined in the array.</param>
Array(std::initializer_list<T> initList) Array(std::initializer_list<T> initList)
{ {
_count = _capacity = (int32)initList.size(); _count = _capacity = static_cast<int32>(initList.size());
if (_count > 0) if (_count > 0)
{ {
_allocation.Allocate(_count); _allocation.Allocate(_count);
@@ -160,8 +160,8 @@ public:
Clear(); Clear();
if (initList.size() > 0) if (initList.size() > 0)
{ {
EnsureCapacity((int32)initList.size()); EnsureCapacity(static_cast<int32>(initList.size()));
_count = (int32)initList.size(); _count = static_cast<int32>(initList.size());
Memory::ConstructItems(_allocation.Get(), initList.begin(), _count); Memory::ConstructItems(_allocation.Get(), initList.begin(), _count);
} }
return *this; return *this;

View File

@@ -214,8 +214,8 @@ public:
{ {
ASSERT(index >= 0 && index < _count); ASSERT(index >= 0 && index < _count);
const ItemType offset = index / sizeof(ItemType); const ItemType offset = index / sizeof(ItemType);
const ItemType bitMask = (ItemType)(int32)(1 << (index & ((int32)sizeof(ItemType) - 1))); const ItemType bitMask = static_cast<ItemType>((int32)(1 << (index & (static_cast<int32>(sizeof(ItemType)) - 1))));
const ItemType item = ((ItemType*)_allocation.Get())[offset]; const ItemType item = static_cast<ItemType*>(_allocation.Get())[offset];
return (item & bitMask) != 0; return (item & bitMask) != 0;
} }
@@ -228,8 +228,8 @@ public:
{ {
ASSERT(index >= 0 && index < _count); ASSERT(index >= 0 && index < _count);
const ItemType offset = index / sizeof(ItemType); const ItemType offset = index / sizeof(ItemType);
const ItemType bitMask = (ItemType)(int32)(1 << (index & ((int32)sizeof(ItemType) - 1))); const ItemType bitMask = static_cast<ItemType>((int32)(1 << (index & (static_cast<int32>(sizeof(ItemType)) - 1))));
ItemType& item = ((ItemType*)_allocation.Get())[offset]; ItemType& item = reinterpret_cast<ItemType*>(_allocation.Get())[offset];
if (value) if (value)
item |= bitMask; item |= bitMask;
else else

View File

@@ -498,7 +498,7 @@ public:
FindPosition(key, pos); FindPosition(key, pos);
if (pos.ObjectIndex == -1) if (pos.ObjectIndex == -1)
return nullptr; return nullptr;
return (ValueType*)&_allocation.Get()[pos.ObjectIndex].Value; return static_cast<ValueType*>(&_allocation.Get()[pos.ObjectIndex].Value);
} }
public: public:

View File

@@ -31,12 +31,12 @@ public:
FORCE_INLINE T* Get() FORCE_INLINE T* Get()
{ {
return (T*)_data; return reinterpret_cast<T*>(_data);
} }
FORCE_INLINE const T* Get() const FORCE_INLINE const T* Get() const
{ {
return (T*)_data; return reinterpret_cast<const T*>(_data);
} }
FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, const int32 minCapacity) const FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, const int32 minCapacity) const
@@ -134,7 +134,7 @@ public:
#if ENABLE_ASSERTION_LOW_LAYERS #if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(!_data); ASSERT(!_data);
#endif #endif
_data = (T*)Allocator::Allocate(capacity * sizeof(T)); _data = static_cast<T*>(Allocator::Allocate(capacity * sizeof(T)));
#if !BUILD_RELEASE #if !BUILD_RELEASE
if (!_data) if (!_data)
OUT_OF_MEMORY; OUT_OF_MEMORY;
@@ -143,7 +143,7 @@ public:
FORCE_INLINE void Relocate(const int32 capacity, int32 oldCount, int32 newCount) FORCE_INLINE void Relocate(const int32 capacity, int32 oldCount, int32 newCount)
{ {
T* newData = capacity != 0 ? (T*)Allocator::Allocate(capacity * sizeof(T)) : nullptr; T* newData = capacity != 0 ? static_cast<T*>(Allocator::Allocate(capacity * sizeof(T))) : nullptr;
#if !BUILD_RELEASE #if !BUILD_RELEASE
if (!newData && capacity != 0) if (!newData && capacity != 0)
OUT_OF_MEMORY; OUT_OF_MEMORY;
@@ -203,12 +203,12 @@ public:
FORCE_INLINE T* Get() FORCE_INLINE T* Get()
{ {
return _useOther ? _other.Get() : (T*)_data; return _useOther ? _other.Get() : reinterpret_cast<T*>(_data);
} }
FORCE_INLINE const T* Get() const FORCE_INLINE const T* Get() const
{ {
return _useOther ? _other.Get() : (T*)_data; return _useOther ? _other.Get() : reinterpret_cast<const T*>(_data);
} }
FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, int32 minCapacity) const FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, int32 minCapacity) const
@@ -227,13 +227,15 @@ public:
FORCE_INLINE void Relocate(int32 capacity, int32 oldCount, int32 newCount) FORCE_INLINE void Relocate(int32 capacity, int32 oldCount, int32 newCount)
{ {
T* data = reinterpret_cast<T*>(_data);
// Check if the new allocation will fit into inlined storage // Check if the new allocation will fit into inlined storage
if (capacity <= Capacity) if (capacity <= Capacity)
{ {
if (_useOther) if (_useOther)
{ {
// Move the items from other allocation to the inlined storage // Move the items from other allocation to the inlined storage
Memory::MoveItems((T*)_data, _other.Get(), newCount); Memory::MoveItems(data, _other.Get(), newCount);
// Free the other allocation // Free the other allocation
Memory::DestructItems(_other.Get(), oldCount); Memory::DestructItems(_other.Get(), oldCount);
@@ -255,8 +257,8 @@ public:
_useOther = true; _useOther = true;
// Move the items from the inlined storage to the other allocation // Move the items from the inlined storage to the other allocation
Memory::MoveItems(_other.Get(), (T*)_data, newCount); Memory::MoveItems(_other.Get(), data, newCount);
Memory::DestructItems((T*)_data, oldCount); Memory::DestructItems(data, oldCount);
} }
} }
} }

View File

@@ -76,7 +76,7 @@ public:
if (_capacity > 0) if (_capacity > 0)
{ {
_allocation.Allocate(_capacity); _allocation.Allocate(_capacity);
Memory::ConstructItems(_allocation.Get(), other.Get(), (int32)other._count); Memory::ConstructItems(_allocation.Get(), other.Get(), static_cast<int32>(other._count));
} }
} }
@@ -102,7 +102,7 @@ public:
{ {
if (this != &other) if (this != &other)
{ {
Memory::DestructItems(_allocation.Get(), (int32)_count); Memory::DestructItems(_allocation.Get(), static_cast<int32>(_count));
if (_capacity < other.Count()) if (_capacity < other.Count())
{ {
_allocation.Free(); _allocation.Free();
@@ -110,7 +110,7 @@ public:
_allocation.Allocate(_capacity); _allocation.Allocate(_capacity);
} }
_count = other.Count(); _count = other.Count();
Memory::ConstructItems(_allocation.Get(), other.Get(), (int32)_count); Memory::ConstructItems(_allocation.Get(), other.Get(), static_cast<int32>(_count));
} }
return *this; return *this;
} }
@@ -124,7 +124,7 @@ public:
{ {
if (this != &other) if (this != &other)
{ {
Memory::DestructItems(_allocation.Get(), (int32)_count); Memory::DestructItems(_allocation.Get(), static_cast<int32>(_count));
_allocation.Free(); _allocation.Free();
_count = other._count; _count = other._count;
_capacity = other._capacity; _capacity = other._capacity;
@@ -140,7 +140,7 @@ public:
/// </summary> /// </summary>
~RenderListBuffer() ~RenderListBuffer()
{ {
Memory::DestructItems(_allocation.Get(), (int32)_count); Memory::DestructItems(_allocation.Get(), static_cast<int32>(_count));
} }
public: public:
@@ -149,7 +149,7 @@ public:
/// </summary> /// </summary>
FORCE_INLINE int32 Count() const FORCE_INLINE int32 Count() const
{ {
return (int32)Platform::AtomicRead((volatile int64*)&_count); return static_cast<int32>(Platform::AtomicRead((volatile int64*)&_count));
} }
/// <summary> /// <summary>
@@ -157,7 +157,7 @@ public:
/// </summary> /// </summary>
FORCE_INLINE int32 Capacity() const FORCE_INLINE int32 Capacity() const
{ {
return (int32)Platform::AtomicRead((volatile int64*)&_capacity); return static_cast<int32>(Platform::AtomicRead((volatile int64*)&_capacity));
} }
/// <summary> /// <summary>
@@ -232,7 +232,7 @@ public:
void Clear() void Clear()
{ {
_locker.Lock(); _locker.Lock();
Memory::DestructItems(_allocation.Get(), (int32)_count); Memory::DestructItems(_allocation.Get(), static_cast<int32>(_count));
_count = 0; _count = 0;
_locker.Unlock(); _locker.Unlock();
} }
@@ -248,8 +248,8 @@ public:
return; return;
_locker.Lock(); _locker.Lock();
ASSERT(capacity >= 0); ASSERT(capacity >= 0);
const int32 count = preserveContents ? ((int32)_count < capacity ? (int32)_count : capacity) : 0; const int32 count = preserveContents ? (static_cast<int32>(_count) < capacity ? static_cast<int32>(_count) : capacity) : 0;
_allocation.Relocate(capacity, (int32)_count, count); _allocation.Relocate(capacity, static_cast<int32>(_count), count);
Platform::AtomicStore(&_capacity, capacity); Platform::AtomicStore(&_capacity, capacity);
Platform::AtomicStore(&_count, count); Platform::AtomicStore(&_count, count);
_locker.Unlock(); _locker.Unlock();
@@ -265,12 +265,12 @@ public:
_locker.Lock(); _locker.Lock();
if (_count > size) if (_count > size)
{ {
Memory::DestructItems(_allocation.Get() + size, (int32)_count - size); Memory::DestructItems(_allocation.Get() + size, static_cast<int32>(_count) - size);
} }
else else
{ {
EnsureCapacity(size, preserveContents); EnsureCapacity(size, preserveContents);
Memory::ConstructItems(_allocation.Get() + _count, size - (int32)_count); Memory::ConstructItems(_allocation.Get() + _count, size - static_cast<int32>(_count));
} }
_count = size; _count = size;
_locker.Unlock(); _locker.Unlock();
@@ -283,11 +283,11 @@ public:
void EnsureCapacity(int32 minCapacity) void EnsureCapacity(int32 minCapacity)
{ {
_locker.Lock(); _locker.Lock();
int32 capacity = (int32)Platform::AtomicRead(&_capacity); int32 capacity = static_cast<int32>(Platform::AtomicRead(&_capacity));
if (capacity < minCapacity) if (capacity < minCapacity)
{ {
capacity = _allocation.CalculateCapacityGrow(capacity, minCapacity); capacity = _allocation.CalculateCapacityGrow(capacity, minCapacity);
const int32 count = (int32)_count; const int32 count = static_cast<int32>(_count);
_allocation.Relocate(capacity, count, count); _allocation.Relocate(capacity, count, count);
Platform::AtomicStore(&_capacity, capacity); Platform::AtomicStore(&_capacity, capacity);
} }
@@ -324,8 +324,8 @@ private:
int32 AddOne() int32 AddOne()
{ {
Platform::InterlockedIncrement(&_threadsAdding); Platform::InterlockedIncrement(&_threadsAdding);
int32 count = (int32)Platform::AtomicRead(&_count); int32 count = static_cast<int32>(Platform::AtomicRead(&_count));
int32 capacity = (int32)Platform::AtomicRead(&_capacity); int32 capacity = static_cast<int32>(Platform::AtomicRead(&_capacity));
const int32 minCapacity = GetMinCapacity(count); const int32 minCapacity = GetMinCapacity(count);
if (minCapacity > capacity || Platform::AtomicRead(&_threadsResizing)) // Resize if not enough space or someone else is already doing it (don't add mid-resizing) if (minCapacity > capacity || Platform::AtomicRead(&_threadsResizing)) // Resize if not enough space or someone else is already doing it (don't add mid-resizing)
{ {
@@ -340,7 +340,7 @@ private:
// Thread-safe resizing // Thread-safe resizing
_locker.Lock(); _locker.Lock();
capacity = (int32)Platform::AtomicRead(&_capacity); capacity = static_cast<int32>(Platform::AtomicRead(&_capacity));
if (capacity < minCapacity) if (capacity < minCapacity)
{ {
if (Platform::AtomicRead(&_threadsAdding)) if (Platform::AtomicRead(&_threadsAdding))
@@ -350,7 +350,7 @@ private:
goto RETRY; goto RETRY;
} }
capacity = _allocation.CalculateCapacityGrow(capacity, minCapacity); capacity = _allocation.CalculateCapacityGrow(capacity, minCapacity);
count = (int32)Platform::AtomicRead(&_count); count = static_cast<int32>(Platform::AtomicRead(&_count));
_allocation.Relocate(capacity, count, count); _allocation.Relocate(capacity, count, count);
Platform::AtomicStore(&_capacity, capacity); Platform::AtomicStore(&_capacity, capacity);
} }
@@ -362,7 +362,7 @@ private:
// Let other thread enter resizing-area // Let other thread enter resizing-area
_locker.Unlock(); _locker.Unlock();
} }
return (int32)Platform::InterlockedIncrement(&_count) - 1; return static_cast<int32>(Platform::InterlockedIncrement(&_count)) - 1;
} }
FORCE_INLINE static int32 GetMinCapacity(const int32 count) FORCE_INLINE static int32 GetMinCapacity(const int32 count)