diff --git a/src/libANGLE/HandleAllocator.cpp b/src/libANGLE/HandleAllocator.cpp index 1b9162aeb..16365dc99 100644 --- a/src/libANGLE/HandleAllocator.cpp +++ b/src/libANGLE/HandleAllocator.cpp @@ -20,7 +20,7 @@ struct HandleAllocator::HandleRangeComparator { bool operator()(const HandleRange &range, GLuint handle) const { - return (handle < range.begin); + return (range.end < handle); } }; @@ -120,13 +120,13 @@ void HandleAllocator::reserve(GLuint handle) // need to split the range auto placementIt = mUnallocatedList.erase(boundIt); - if (begin != handle) - { - placementIt = mUnallocatedList.insert(placementIt, HandleRange(begin, handle)); - } if (handle + 1 != end) { - mUnallocatedList.insert(placementIt, HandleRange(handle + 1, end)); + placementIt = mUnallocatedList.insert(placementIt, HandleRange(handle + 1, end)); + } + if (begin != handle) + { + mUnallocatedList.insert(placementIt, HandleRange(begin, handle)); } } diff --git a/src/libANGLE/HandleAllocator_unittest.cpp b/src/libANGLE/HandleAllocator_unittest.cpp index 196b346ac..726b64bab 100644 --- a/src/libANGLE/HandleAllocator_unittest.cpp +++ b/src/libANGLE/HandleAllocator_unittest.cpp @@ -103,4 +103,24 @@ TEST(HandleAllocatorTest, ReserveMaxUintHandle) EXPECT_EQ(1u, normalHandle); } +// To test if the allocator keep the handle in a sorted order. +TEST(HandleAllocatorTest, SortedOrderHandle) +{ + gl::HandleAllocator allocator; + + allocator.reserve(3); + + GLuint allocatedList[5]; + for (GLuint count = 0; count < 5; count++) + { + allocatedList[count] = allocator.allocate(); + } + + EXPECT_EQ(1u, allocatedList[0]); + EXPECT_EQ(2u, allocatedList[1]); + EXPECT_EQ(4u, allocatedList[2]); + EXPECT_EQ(5u, allocatedList[3]); + EXPECT_EQ(6u, allocatedList[4]); +} + }