diff --git a/Source/Engine/Core/Memory/Allocation.h b/Source/Engine/Core/Memory/Allocation.h index ffd51d96c..f274d37a5 100644 --- a/Source/Engine/Core/Memory/Allocation.h +++ b/Source/Engine/Core/Memory/Allocation.h @@ -18,9 +18,7 @@ namespace AllocationUtils capacity |= capacity >> 8; capacity |= capacity >> 16; uint64 capacity64 = (uint64)(capacity + 1) * 2; - if (capacity64 > MAX_int32) - capacity64 = MAX_int32; - return (int32)capacity64; + return capacity64 >= MAX_int32 ? MAX_int32 : (int32)capacity64 / 2; } // Aligns the input value to the next power of 2 to be used as bigger memory allocation block. diff --git a/Source/Engine/Tests/TestCollections.cpp b/Source/Engine/Tests/TestCollections.cpp index b477140cf..a96c6ce1c 100644 --- a/Source/Engine/Tests/TestCollections.cpp +++ b/Source/Engine/Tests/TestCollections.cpp @@ -27,6 +27,19 @@ void CheckBitArray(const BitArray& array) TEST_CASE("Array") { + SECTION("Test Capacity") + { + // Ensure correct collections capacity growing to meet proper memory usage vs safe slack + CHECK(AllocationUtils::CalculateCapacityGrow(1, 0) == 8); + CHECK(AllocationUtils::CalculateCapacityGrow(7, 0) == 8); + CHECK(AllocationUtils::CalculateCapacityGrow(1, 16) == 16); + CHECK(AllocationUtils::CalculateCapacityGrow(31, 0) == 32); + CHECK(AllocationUtils::CalculateCapacityGrow(32, 0) == 32); + CHECK(AllocationUtils::CalculateCapacityGrow(1000, 0) == 1024); + CHECK(AllocationUtils::CalculateCapacityGrow(1024, 0) == 1024); + CHECK(AllocationUtils::CalculateCapacityGrow(1025, 0) == 2048); + } + SECTION("Test Allocators") { Array a1;