Replace custom int conversion with SkTo in SkArenaAlloc

I think this was causing some of the strange errors with
https://skia-review.googlesource.com/c/skia/+/464876

Change-Id: I461630a16f2342aaab43902fadf640ba664a87e7
Bug: skia:12584
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/464956
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
Kevin Lubick 2021-10-28 12:49:30 -04:00
parent af5049b0d7
commit da53bb8d95
2 changed files with 14 additions and 23 deletions

View File

@ -14,8 +14,8 @@ static char* end_chain(char*) { return nullptr; }
SkArenaAlloc::SkArenaAlloc(char* block, size_t size, size_t firstHeapAllocation)
: fDtorCursor {block}
, fCursor {block}
, fEnd {block + ToU32(size)}
, fFibonacciProgression{ToU32(size), ToU32(firstHeapAllocation)}
, fEnd {block + SkToU32(size)}
, fFibonacciProgression{SkToU32(size), SkToU32(firstHeapAllocation)}
{
if (size < sizeof(Footer)) {
fEnd = fCursor = fDtorCursor = nullptr;
@ -128,25 +128,20 @@ restart:
// Install a skip footer if needed, thus terminating a run of POD data. The calling code is
// responsible for installing the footer after the object.
if (needsSkipFooter) {
this->installRaw(ToU32(fCursor - fDtorCursor));
this->installRaw(SkToU32(fCursor - fDtorCursor));
this->installFooter(SkipPod, 0);
}
return objStart;
}
static uint32_t to_uint32_t(size_t v) {
assert(SkTFitsIn<uint32_t>(v));
return (uint32_t)v;
}
SkArenaAllocWithReset::SkArenaAllocWithReset(char* block,
size_t size,
size_t firstHeapAllocation)
: SkArenaAlloc(block, size, firstHeapAllocation)
, fFirstBlock{block}
, fFirstSize{to_uint32_t(size)}
, fFirstHeapAllocationSize{to_uint32_t(firstHeapAllocation)} {}
, fFirstSize{SkToU32(size)}
, fFirstHeapAllocationSize{SkToU32(firstHeapAllocation)} {}
void SkArenaAllocWithReset::reset() {
this->~SkArenaAllocWithReset();

View File

@ -121,8 +121,8 @@ public:
auto make(Ctor&& ctor) -> decltype(ctor(nullptr)) {
using T = std::remove_pointer_t<decltype(ctor(nullptr))>;
uint32_t size = ToU32(sizeof(T));
uint32_t alignment = ToU32(alignof(T));
uint32_t size = SkToU32(sizeof(T));
uint32_t alignment = SkToU32(alignof(T));
char* objStart;
if (std::is_trivially_destructible<T>::value) {
objStart = this->allocObject(size, alignment);
@ -130,7 +130,7 @@ public:
} else {
objStart = this->allocObjectWithFooter(size + sizeof(Footer), alignment);
// Can never be UB because max value is alignof(T).
uint32_t padding = ToU32(objStart - fCursor);
uint32_t padding = SkToU32(objStart - fCursor);
// Advance to end of object to install footer.
fCursor = objStart + size;
@ -185,17 +185,13 @@ public:
// Only use makeBytesAlignedTo if none of the typed variants are impractical to use.
void* makeBytesAlignedTo(size_t size, size_t align) {
AssertRelease(SkTFitsIn<uint32_t>(size));
auto objStart = this->allocObject(ToU32(size), ToU32(align));
auto objStart = this->allocObject(SkToU32(size), SkToU32(align));
fCursor = objStart + size;
return objStart;
}
private:
static void AssertRelease(bool cond) { if (!cond) { ::abort(); } }
static uint32_t ToU32(size_t v) {
assert(SkTFitsIn<uint32_t>(v));
return (uint32_t)v;
}
using FooterAction = char* (char*);
struct Footer {
@ -239,12 +235,12 @@ private:
template <typename T>
T* allocUninitializedArray(size_t countZ) {
AssertRelease(SkTFitsIn<uint32_t>(countZ));
uint32_t count = ToU32(countZ);
uint32_t count = SkToU32(countZ);
char* objStart;
AssertRelease(count <= std::numeric_limits<uint32_t>::max() / sizeof(T));
uint32_t arraySize = ToU32(count * sizeof(T));
uint32_t alignment = ToU32(alignof(T));
uint32_t arraySize = SkToU32(count * sizeof(T));
uint32_t alignment = SkToU32(alignof(T));
if (std::is_trivially_destructible<T>::value) {
objStart = this->allocObject(arraySize, alignment);
@ -256,11 +252,11 @@ private:
objStart = this->allocObjectWithFooter(totalSize, alignment);
// Can never be UB because max value is alignof(T).
uint32_t padding = ToU32(objStart - fCursor);
uint32_t padding = SkToU32(objStart - fCursor);
// Advance to end of array to install footer.
fCursor = objStart + arraySize;
this->installRaw(ToU32(count));
this->installRaw(SkToU32(count));
this->installFooter(
[](char* footerEnd) {
char* objEnd = footerEnd - (sizeof(Footer) + sizeof(uint32_t));