Skip to content

Commit 53c9e8b

Browse files
committed
Fixed unit test for allocating a fragment with no available slot
1 parent 2c4a2f6 commit 53c9e8b

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

tests/src/resources/test_ResourceSystem.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,9 @@ class Allocator
680680
size_t freeListIdx = 0, fl = 0, sl = 0;
681681

682682
FreeBlockNode* block = FindFreeBlock(requiredSize, fl, sl, freeListIdx);
683+
684+
if (freeListIdx == INVALID_INDEX) return nullptr;
685+
683686
BlockHeader* header = GetHeader(block);
684687

685688
TrySplitBlock(header, block, requiredSize, fl, sl, freeListIdx);
@@ -809,8 +812,9 @@ class Allocator
809812
void TrySplitBlock(BlockHeader* header, FreeBlockNode* block, size_t size, size_t fl, size_t sl, size_t index)
810813
{
811814
size_t blockSize = GetHeaderSize(header);
812-
if (blockSize <= size) return;
813815
RemoveFromFreeList(block, fl, sl, index);
816+
if (blockSize <= size) return;
817+
814818
CreateNewBlock(TO_BYTES(header) + size, blockSize - size);
815819
}
816820

@@ -850,8 +854,6 @@ class Allocator
850854

851855
size_t sizeFlag = (size << FLAG_BITS);
852856

853-
// 0000 0000 0000 0000
854-
855857
BlockHeader* header = TO_HBLOCK(ptr);
856858
header->sizeAndFlags = (sizeFlag | IS_FREE_FLAG);
857859

@@ -1232,7 +1234,29 @@ UTEST(test_ResourceSystem, TestBlockCoalescing)
12321234
ASSERT_FALSE(badPointer);
12331235

12341236
// try to deallocate pointer not in allocator;
1237+
12351238
uint64_t* val = new uint64_t;
1239+
a.Deallocate(val); // Should do nothing and be ignored.
1240+
free(val);
1241+
}
1242+
1243+
UTEST(test_ResourceSystem, TestAllocationWhenNoAppropriateFragmentExists)
1244+
{
1245+
Allocator a(128);
1246+
ASSERT_NE(a.Data(), nullptr);
1247+
ASSERT_EQ(a.Capacity(), 128);
1248+
ASSERT_EQ(128, a.BytesRemaining());
1249+
1250+
void* p0 = a.Allocate(16);
1251+
void* p1 = a.Allocate(32);
1252+
void* p2 = a.Allocate(16);
1253+
void* p3 = a.Allocate(32);
1254+
ASSERT_EQ(0, a.BytesRemaining());
1255+
1256+
a.Deallocate(p0);
1257+
a.Deallocate(p2);
12361258

1237-
a.Deallocate(val);
1259+
ASSERT_EQ(48, a.BytesRemaining());
1260+
void* tooLargeVal = a.Allocate(24);
1261+
ASSERT_FALSE(tooLargeVal);
12381262
}

0 commit comments

Comments
 (0)