@@ -680,6 +680,9 @@ class Allocator
680
680
size_t freeListIdx = 0 , fl = 0 , sl = 0 ;
681
681
682
682
FreeBlockNode* block = FindFreeBlock (requiredSize, fl, sl, freeListIdx);
683
+
684
+ if (freeListIdx == INVALID_INDEX) return nullptr ;
685
+
683
686
BlockHeader* header = GetHeader (block);
684
687
685
688
TrySplitBlock (header, block, requiredSize, fl, sl, freeListIdx);
@@ -809,8 +812,9 @@ class Allocator
809
812
void TrySplitBlock (BlockHeader* header, FreeBlockNode* block, size_t size, size_t fl, size_t sl, size_t index)
810
813
{
811
814
size_t blockSize = GetHeaderSize (header);
812
- if (blockSize <= size) return ;
813
815
RemoveFromFreeList (block, fl, sl, index);
816
+ if (blockSize <= size) return ;
817
+
814
818
CreateNewBlock (TO_BYTES (header) + size, blockSize - size);
815
819
}
816
820
@@ -850,8 +854,6 @@ class Allocator
850
854
851
855
size_t sizeFlag = (size << FLAG_BITS);
852
856
853
- // 0000 0000 0000 0000
854
-
855
857
BlockHeader* header = TO_HBLOCK (ptr);
856
858
header->sizeAndFlags = (sizeFlag | IS_FREE_FLAG);
857
859
@@ -1232,7 +1234,29 @@ UTEST(test_ResourceSystem, TestBlockCoalescing)
1232
1234
ASSERT_FALSE (badPointer);
1233
1235
1234
1236
// try to deallocate pointer not in allocator;
1237
+
1235
1238
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);
1236
1258
1237
- a.Deallocate (val);
1259
+ ASSERT_EQ (48 , a.BytesRemaining ());
1260
+ void * tooLargeVal = a.Allocate (24 );
1261
+ ASSERT_FALSE (tooLargeVal);
1238
1262
}
0 commit comments