Skip to content

Commit dcfb771

Browse files
authored
Malloc cleanups and comments (#18257)
1 parent 94c5984 commit dcfb771

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

system/include/compat/malloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern "C" {
88
#endif
99

1010
/* This version of struct mallinfo must match the one in
11-
system/lib/dlmallo.c. */
11+
system/lib/dlmalloc.c. */
1212

1313
struct mallinfo {
1414
int arena; /* total space allocated from system */

system/lib/emmalloc.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ static int compute_free_list_bucket(size_t allocSize)
228228
{
229229
if (allocSize < 128) return (allocSize >> 3) - 1;
230230
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+
232236
assert(bucketIndex >= 0);
233237
assert(bucketIndex < NUM_FREE_BUCKETS);
234238
return bucketIndex;
@@ -584,7 +588,7 @@ static void *attempt_allocate(Region *freeRegion, size_t alignment, size_t size)
584588
// from the list of free regions: whatever slop remains will be later added back to the free region pool.
585589
unlink_from_free_list(freeRegion);
586590

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,
588592
// so that the boundary between the two regions happens at a right spot for the payload to be aligned.
589593
if (payloadStartPtr != payloadStartPtrAligned)
590594
{
@@ -739,8 +743,7 @@ static void *allocate_memory(size_t alignment, size_t size)
739743
bucketMask ^= 1;
740744
}
741745
// 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).
744747
assert((bucketIndex == NUM_FREE_BUCKETS && bucketMask == 0) || (bucketMask == freeRegionBucketsUsed >> bucketIndex));
745748
}
746749

@@ -753,6 +756,7 @@ static void *allocate_memory(size_t alignment, size_t size)
753756
int largestBucketIndex = NUM_FREE_BUCKETS - 1 - __builtin_clzll(freeRegionBucketsUsed);
754757
// freeRegion will be null if there is absolutely no memory left. (all buckets are 100% used)
755758
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)
756760
if (freeRegionBucketsUsed >> 30)
757761
{
758762
// Look only at a constant number of regions in this bucket max, to avoid bad worst case behavior.

0 commit comments

Comments
 (0)