diff --git a/compiler-rt/lib/scudo/standalone/local_cache.h b/compiler-rt/lib/scudo/standalone/local_cache.h index 6e5b09fdb709e6..6e84158659ae95 100644 --- a/compiler-rt/lib/scudo/standalone/local_cache.h +++ b/compiler-rt/lib/scudo/standalone/local_cache.h @@ -31,7 +31,8 @@ template struct SizeClassAllocatorLocalCache { void appendFromArray(CompactPtrT *Array, u16 N) { DCHECK_LE(N, MaxNumCached - Count); memcpy(Batch + Count, Array, sizeof(Batch[0]) * N); - Count += N; + // u16 will be promoted to int by arithmetic type conversion. + Count = static_cast(Count + N); } void clear() { Count = 0; } void add(CompactPtrT P) { @@ -189,7 +190,7 @@ template struct SizeClassAllocatorLocalCache { for (uptr I = 0; I < NumClasses; I++) { PerClass *P = &PerClassArray[I]; const uptr Size = SizeClassAllocator::getSizeByClassId(I); - P->MaxCount = 2 * TransferBatch::getMaxCached(Size); + P->MaxCount = static_cast(2 * TransferBatch::getMaxCached(Size)); if (I != BatchClassId) { P->ClassSize = Size; } else { @@ -221,7 +222,8 @@ template struct SizeClassAllocatorLocalCache { NOINLINE void drain(PerClass *C, uptr ClassId) { const u16 Count = Min(static_cast(C->MaxCount / 2), C->Count); Allocator->pushBlocks(this, ClassId, &C->Chunks[0], Count); - C->Count -= Count; + // u16 will be promoted to int by arithmetic type conversion. + C->Count = static_cast(C->Count - Count); for (u16 I = 0; I < C->Count; I++) C->Chunks[I] = C->Chunks[I + Count]; } diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h index fb6cb53392358e..c30f22f3fe990f 100644 --- a/compiler-rt/lib/scudo/standalone/primary32.h +++ b/compiler-rt/lib/scudo/standalone/primary32.h @@ -436,7 +436,8 @@ template class SizeClassAllocator32 { for (u32 I = 0; I < Size;) { DCHECK_GE(BG->MaxCachedPerBatch, CurBatch->getCount()); - u16 UnusedSlots = BG->MaxCachedPerBatch - CurBatch->getCount(); + u16 UnusedSlots = + static_cast(BG->MaxCachedPerBatch - CurBatch->getCount()); if (UnusedSlots == 0) { CurBatch = C->createBatch(ClassId, reinterpret_cast( decompactPtr(ClassId, Array[I]))); @@ -444,6 +445,7 @@ template class SizeClassAllocator32 { Batches.push_front(CurBatch); UnusedSlots = BG->MaxCachedPerBatch; } + // `UnusedSlots` is u16 so the result will be also fit in u16. u16 AppendSize = static_cast(Min(UnusedSlots, Size - I)); CurBatch->appendFromArray(&Array[I], AppendSize); I += AppendSize; diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h index 2b6e71c8584aba..29d498f309a47f 100644 --- a/compiler-rt/lib/scudo/standalone/primary64.h +++ b/compiler-rt/lib/scudo/standalone/primary64.h @@ -442,7 +442,8 @@ template class SizeClassAllocator64 { for (u32 I = 0; I < Size;) { DCHECK_GE(BG->MaxCachedPerBatch, CurBatch->getCount()); - u16 UnusedSlots = BG->MaxCachedPerBatch - CurBatch->getCount(); + u16 UnusedSlots = + static_cast(BG->MaxCachedPerBatch - CurBatch->getCount()); if (UnusedSlots == 0) { CurBatch = C->createBatch(ClassId, reinterpret_cast( decompactPtr(ClassId, Array[I])));