diff --git a/src/core/SkArenaAlloc.cpp b/src/core/SkArenaAlloc.cpp index 69ce2d53c4..29bb3f4fa4 100644 --- a/src/core/SkArenaAlloc.cpp +++ b/src/core/SkArenaAlloc.cpp @@ -74,19 +74,14 @@ char* SkArenaAlloc::NextBlock(char* footerEnd) { void SkArenaAlloc::ensureSpace(uint32_t size, uint32_t alignment) { constexpr uint32_t headerSize = sizeof(Footer) + sizeof(ptrdiff_t); - // The chrome c++ library we use does not define std::max_align_t. - // This must be conservative to add the right amount of extra memory to handle the alignment - // padding. - constexpr uint32_t alignof_max_align_t = 8; constexpr uint32_t maxSize = std::numeric_limits::max(); constexpr uint32_t overhead = headerSize + sizeof(Footer); AssertRelease(size <= maxSize - overhead); uint32_t objSizeAndOverhead = size + overhead; - if (alignment > alignof_max_align_t) { - uint32_t alignmentOverhead = alignment - 1; - AssertRelease(objSizeAndOverhead <= maxSize - alignmentOverhead); - objSizeAndOverhead += alignmentOverhead; - } + + const uint32_t alignmentOverhead = alignment - 1; + AssertRelease(objSizeAndOverhead <= maxSize - alignmentOverhead); + objSizeAndOverhead += alignmentOverhead; uint32_t minAllocationSize = fNextHeapAlloc; diff --git a/src/core/SkArenaAlloc.h b/src/core/SkArenaAlloc.h index df8fa102ed..507abafbe7 100644 --- a/src/core/SkArenaAlloc.h +++ b/src/core/SkArenaAlloc.h @@ -8,6 +8,7 @@ #ifndef SkArenaAlloc_DEFINED #define SkArenaAlloc_DEFINED +#include "include/core/SkTypes.h" #include "include/private/SkTFitsIn.h" #include @@ -170,7 +171,13 @@ private: this->ensureSpace(size, alignment); alignedOffset = (~reinterpret_cast(fCursor) + 1) & mask; } - return fCursor + alignedOffset; + + char* object = fCursor + alignedOffset; + + SkASSERT((reinterpret_cast(object) & (alignment - 1)) == 0); + SkASSERT(object + size <= fEnd); + + return object; } char* allocObjectWithFooter(uint32_t sizeIncludingFooter, uint32_t alignment); diff --git a/tests/ArenaAllocTest.cpp b/tests/ArenaAllocTest.cpp index 27d200c2b7..68b010ff17 100644 --- a/tests/ArenaAllocTest.cpp +++ b/tests/ArenaAllocTest.cpp @@ -180,4 +180,8 @@ DEF_TEST(ArenaAlloc, r) { REPORTER_ASSERT(r, created == 128); REPORTER_ASSERT(r, destroyed == 128); + { + SkArenaAlloc arena(4096); + arena.makeBytesAlignedTo(4081, 8); + } }