@@ -228,7 +228,11 @@ static int compute_free_list_bucket(size_t allocSize)
228
228
{
229
229
if (allocSize < 128 ) return (allocSize >> 3 ) - 1 ;
230
230
int clz = __builtin_clz (allocSize );
231
- int bucketIndex = (clz > 19 ) ? 110 - (clz <<2 ) + ((allocSize >> (29 - clz )) ^ 4 ) : MIN (71 - (clz <<1 ) + ((allocSize >> (30 - clz )) ^ 2 ), NUM_FREE_BUCKETS - 1 );
231
+ int bucketIndex =
232
+ (clz > 19 )
233
+ ? 110 - (clz <<2 ) + ((allocSize >> (29 - clz )) ^ 4 )
234
+ : MIN ( 71 - (clz <<1 ) + ((allocSize >> (30 - clz )) ^ 2 ), NUM_FREE_BUCKETS - 1 );
235
+
232
236
assert (bucketIndex >= 0 );
233
237
assert (bucketIndex < NUM_FREE_BUCKETS );
234
238
return bucketIndex ;
@@ -584,7 +588,7 @@ static void *attempt_allocate(Region *freeRegion, size_t alignment, size_t size)
584
588
// from the list of free regions: whatever slop remains will be later added back to the free region pool.
585
589
unlink_from_free_list (freeRegion );
586
590
587
- // Before we proceed further, fix up the boundary of this region and the region that precedes this one ,
591
+ // Before we proceed further, fix up the boundary between this and the preceding region ,
588
592
// so that the boundary between the two regions happens at a right spot for the payload to be aligned.
589
593
if (payloadStartPtr != payloadStartPtrAligned )
590
594
{
@@ -739,8 +743,7 @@ static void *allocate_memory(size_t alignment, size_t size)
739
743
bucketMask ^= 1 ;
740
744
}
741
745
// Instead of recomputing bucketMask from scratch at the end of each loop, it is updated as we go,
742
- // to avoid undefined behavior with (x >> 32)/(x >> 64) when bucketIndex reaches 32/64, (the shift would comes out as a no-op instead of 0).
743
-
746
+ // to avoid undefined behavior with (x >> 32)/(x >> 64) when bucketIndex reaches 32/64, (the shift would come out as a no-op instead of 0).
744
747
assert ((bucketIndex == NUM_FREE_BUCKETS && bucketMask == 0 ) || (bucketMask == freeRegionBucketsUsed >> bucketIndex ));
745
748
}
746
749
@@ -753,6 +756,7 @@ static void *allocate_memory(size_t alignment, size_t size)
753
756
int largestBucketIndex = NUM_FREE_BUCKETS - 1 - __builtin_clzll (freeRegionBucketsUsed );
754
757
// freeRegion will be null if there is absolutely no memory left. (all buckets are 100% used)
755
758
Region * freeRegion = freeRegionBucketsUsed ? freeRegionBuckets [largestBucketIndex ].next : 0 ;
759
+ // The 30 first free region buckets cover memory blocks < 2048 bytes, so skip looking at those here (too small)
756
760
if (freeRegionBucketsUsed >> 30 )
757
761
{
758
762
// Look only at a constant number of regions in this bucket max, to avoid bad worst case behavior.
0 commit comments