diff --git a/src/dawn_native/BuddyAllocator.cpp b/src/dawn_native/BuddyAllocator.cpp index a96dd88318..38b2dd8e45 100644 --- a/src/dawn_native/BuddyAllocator.cpp +++ b/src/dawn_native/BuddyAllocator.cpp @@ -91,7 +91,7 @@ namespace dawn_native { break; } } - return INVALID_OFFSET; // No free block exists at any level. + return kInvalidOffset; // No free block exists at any level. } // Inserts existing free block into the free-list. @@ -141,7 +141,7 @@ namespace dawn_native { uint64_t BuddyAllocator::Allocate(uint64_t allocationSize, uint64_t alignment) { if (allocationSize == 0 || allocationSize > mMaxBlockSize) { - return INVALID_OFFSET; + return kInvalidOffset; } // Compute the level @@ -152,8 +152,8 @@ namespace dawn_native { uint64_t currBlockLevel = GetNextFreeAlignedBlock(allocationSizeToLevel, alignment); // Error when no free blocks exist (allocator is full) - if (currBlockLevel == INVALID_OFFSET) { - return INVALID_OFFSET; + if (currBlockLevel == kInvalidOffset) { + return kInvalidOffset; } // Split free blocks level-by-level. diff --git a/src/dawn_native/BuddyAllocator.h b/src/dawn_native/BuddyAllocator.h index c613375413..af82636268 100644 --- a/src/dawn_native/BuddyAllocator.h +++ b/src/dawn_native/BuddyAllocator.h @@ -22,9 +22,7 @@ namespace dawn_native { - static constexpr uint64_t INVALID_OFFSET = std::numeric_limits::max(); - - // Buddy allocator uses the buddy memory allocation technique to satisify an allocation request. + // Buddy allocator uses the buddy memory allocation technique to satisfy an allocation request. // Memory is split into halves until just large enough to fit to the request. This // requires the allocation size to be a power-of-two value. The allocator "allocates" a block by // returning the starting offset whose size is guaranteed to be greater than or equal to the @@ -47,6 +45,8 @@ namespace dawn_native { // For testing purposes only. uint64_t ComputeTotalNumOfFreeBlocksForTesting() const; + static constexpr uint64_t kInvalidOffset = std::numeric_limits::max(); + private: uint32_t ComputeLevelFromBlockSize(uint64_t blockSize) const; uint64_t GetNextFreeAlignedBlock(size_t allocationBlockLevel, uint64_t alignment) const; diff --git a/src/tests/unittests/BuddyAllocatorTests.cpp b/src/tests/unittests/BuddyAllocatorTests.cpp index 6ef6fceded..cdb8908dc4 100644 --- a/src/tests/unittests/BuddyAllocatorTests.cpp +++ b/src/tests/unittests/BuddyAllocatorTests.cpp @@ -17,6 +17,8 @@ using namespace dawn_native; +constexpr uint64_t BuddyAllocator::kInvalidOffset; + // Verify the buddy allocator with a basic test. TEST(BuddyAllocatorTests, SingleBlock) { // After one 32 byte allocation: @@ -29,17 +31,17 @@ TEST(BuddyAllocatorTests, SingleBlock) { BuddyAllocator allocator(maxBlockSize); // Check that we cannot allocate a oversized block. - ASSERT_EQ(allocator.Allocate(maxBlockSize * 2), INVALID_OFFSET); + ASSERT_EQ(allocator.Allocate(maxBlockSize * 2), BuddyAllocator::kInvalidOffset); // Check that we cannot allocate a zero sized block. - ASSERT_EQ(allocator.Allocate(0u), INVALID_OFFSET); + ASSERT_EQ(allocator.Allocate(0u), BuddyAllocator::kInvalidOffset); // Allocate the block. uint64_t blockOffset = allocator.Allocate(maxBlockSize); ASSERT_EQ(blockOffset, 0u); // Check that we are full. - ASSERT_EQ(allocator.Allocate(maxBlockSize), INVALID_OFFSET); + ASSERT_EQ(allocator.Allocate(maxBlockSize), BuddyAllocator::kInvalidOffset); ASSERT_EQ(allocator.ComputeTotalNumOfFreeBlocksForTesting(), 0u); // Deallocate the block. @@ -86,7 +88,7 @@ TEST(BuddyAllocatorTests, SingleSplitBlock) { ASSERT_EQ(allocator.ComputeTotalNumOfFreeBlocksForTesting(), 1u); // Check that we cannot allocate a block that is oversized. - ASSERT_EQ(allocator.Allocate(maxBlockSize * 2), INVALID_OFFSET); + ASSERT_EQ(allocator.Allocate(maxBlockSize * 2), BuddyAllocator::kInvalidOffset); // Re-allocate the largest block allowed after merging. blockOffset = allocator.Allocate(maxBlockSize); @@ -188,7 +190,7 @@ TEST(BuddyAllocatorTests, MultipleSplitBlockIncreasingSize) { ASSERT_EQ(allocator.ComputeTotalNumOfFreeBlocksForTesting(), 0u); // Check if we're full. - ASSERT_EQ(allocator.Allocate(32), INVALID_OFFSET); + ASSERT_EQ(allocator.Allocate(32), BuddyAllocator::kInvalidOffset); } // Verify very small allocations using a larger allocator works correctly. @@ -284,7 +286,7 @@ TEST(BuddyAllocatorTests, SameSizeVariousAlignment) { ASSERT_EQ(allocator.ComputeTotalNumOfFreeBlocksForTesting(), 2u); // Check that we cannot fit another. - ASSERT_EQ(allocator.Allocate(8, 16), INVALID_OFFSET); + ASSERT_EQ(allocator.Allocate(8, 16), BuddyAllocator::kInvalidOffset); // Allocate Ac (zero splits and Ab's buddy is now the first free block). ASSERT_EQ(allocator.Allocate(8, 8), 24u);