Skip to content

Commit cc81d25

Browse files
authored
Use out and err for logging in emmalloc.c. NFC (#20642)
This will save a few bytes in builds that do logging (since console.log calls cannot be minimized by closure) and saves a little on line length for these log lines.
1 parent 3fc186c commit cc81d25

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

system/lib/emmalloc.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
*
3333
* - If not NDEBUG, runtime assert()s are in use.
3434
* - If EMMALLOC_MEMVALIDATE is defined, a large amount of extra checks are done.
35-
* - If EMMALLOC_VERBOSE is defined, a lot of operations are logged
36-
* out, in addition to EMMALLOC_MEMVALIDATE.
37-
* - Debugging and logging directly uses console.log via uses EM_ASM, not
35+
* - If EMMALLOC_VERBOSE is defined, a lot of operations are logged using
36+
* `out`, in addition to EMMALLOC_MEMVALIDATE.
37+
* - Debugging and logging directly uses `out` and `err` via EM_ASM, not
3838
* printf etc., to minimize any risk of debugging or logging depending on
3939
* malloc.
4040
*
@@ -368,38 +368,38 @@ static void dump_memory_regions()
368368
{
369369
ASSERT_MALLOC_IS_ACQUIRED();
370370
RootRegion *root = listOfAllRegions;
371-
MAIN_THREAD_ASYNC_EM_ASM(console.log('All memory regions:'));
371+
MAIN_THREAD_ASYNC_EM_ASM(out('All memory regions:'));
372372
while(root)
373373
{
374374
Region *r = (Region*)root;
375375
assert(debug_region_is_consistent(r));
376376
uint8_t *lastRegionEnd = root->endPtr;
377-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Region block 0x'+($0>>>0).toString(16)+' - 0x'+($1>>>0).toString(16)+ ' ('+($2>>>0)+' bytes):'),
377+
MAIN_THREAD_ASYNC_EM_ASM(out('Region block 0x'+($0>>>0).toString(16)+' - 0x'+($1>>>0).toString(16)+ ' ('+($2>>>0)+' bytes):'),
378378
r, lastRegionEnd, lastRegionEnd-(uint8_t*)r);
379379
while((uint8_t*)r < lastRegionEnd)
380380
{
381-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Region 0x'+($0>>>0).toString(16)+', size: '+($1>>>0)+' ('+($2?"used":"--FREE--")+')'),
381+
MAIN_THREAD_ASYNC_EM_ASM(out('Region 0x'+($0>>>0).toString(16)+', size: '+($1>>>0)+' ('+($2?"used":"--FREE--")+')'),
382382
r, r->size, region_ceiling_size(r) == r->size);
383383

384384
assert(debug_region_is_consistent(r));
385385
size_t sizeFromCeiling = size_of_region_from_ceiling(r);
386386
if (sizeFromCeiling != r->size)
387-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Corrupt region! Size marker at the end of the region does not match: '+($0>>>0)), sizeFromCeiling);
387+
MAIN_THREAD_ASYNC_EM_ASM(out('Corrupt region! Size marker at the end of the region does not match: '+($0>>>0)), sizeFromCeiling);
388388
if (r->size == 0)
389389
break;
390390
r = next_region(r);
391391
}
392392
root = root->next;
393-
MAIN_THREAD_ASYNC_EM_ASM(console.log(""));
393+
MAIN_THREAD_ASYNC_EM_ASM(out(""));
394394
}
395-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Free regions:'));
395+
MAIN_THREAD_ASYNC_EM_ASM(out('Free regions:'));
396396
for(int i = 0; i < NUM_FREE_BUCKETS; ++i)
397397
{
398398
Region *prev = &freeRegionBuckets[i];
399399
Region *fr = freeRegionBuckets[i].next;
400400
while(fr != &freeRegionBuckets[i])
401401
{
402-
MAIN_THREAD_ASYNC_EM_ASM(console.log('In bucket '+$0+', free region 0x'+($1>>>0).toString(16)+', size: ' + ($2>>>0) + ' (size at ceiling: '+($3>>>0)+'), prev: 0x' + ($4>>>0).toString(16) + ', next: 0x' + ($5>>>0).toString(16)),
402+
MAIN_THREAD_ASYNC_EM_ASM(out('In bucket '+$0+', free region 0x'+($1>>>0).toString(16)+', size: ' + ($2>>>0) + ' (size at ceiling: '+($3>>>0)+'), prev: 0x' + ($4>>>0).toString(16) + ', next: 0x' + ($5>>>0).toString(16)),
403403
i, fr, fr->size, size_of_region_from_ceiling(fr), fr->prev, fr->next);
404404
assert(debug_region_is_consistent(fr));
405405
assert(region_is_free(fr));
@@ -410,8 +410,8 @@ static void dump_memory_regions()
410410
fr = fr->next;
411411
}
412412
}
413-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Free bucket index map: ' + ($0>>>0).toString(2) + ' ' + ($1>>>0).toString(2)), (uint32_t)(freeRegionBucketsUsed >> 32), (uint32_t)freeRegionBucketsUsed);
414-
MAIN_THREAD_ASYNC_EM_ASM(console.log(""));
413+
MAIN_THREAD_ASYNC_EM_ASM(out('Free bucket index map: ' + ($0>>>0).toString(2) + ' ' + ($1>>>0).toString(2)), (uint32_t)(freeRegionBucketsUsed >> 32), (uint32_t)freeRegionBucketsUsed);
414+
MAIN_THREAD_ASYNC_EM_ASM(out(""));
415415
}
416416

417417
void emmalloc_dump_memory_regions()
@@ -430,7 +430,7 @@ static int validate_memory_regions()
430430
Region *r = (Region*)root;
431431
if (!debug_region_is_consistent(r))
432432
{
433-
MAIN_THREAD_ASYNC_EM_ASM(console.error('Used region 0x'+($0>>>0).toString(16)+', size: '+($1>>>0)+' ('+($2?"used":"--FREE--")+') is corrupt (size markers in the beginning and at the end of the region do not match!)'),
433+
MAIN_THREAD_ASYNC_EM_ASM(err('Used region 0x'+($0>>>0).toString(16)+', size: '+($1>>>0)+' ('+($2?"used":"--FREE--")+') is corrupt (size markers in the beginning and at the end of the region do not match!)'),
434434
r, r->size, region_ceiling_size(r) == r->size);
435435
return 1;
436436
}
@@ -439,7 +439,7 @@ static int validate_memory_regions()
439439
{
440440
if (!debug_region_is_consistent(r))
441441
{
442-
MAIN_THREAD_ASYNC_EM_ASM(console.error('Used region 0x'+($0>>>0).toString(16)+', size: '+($1>>>0)+' ('+($2?"used":"--FREE--")+') is corrupt (size markers in the beginning and at the end of the region do not match!)'),
442+
MAIN_THREAD_ASYNC_EM_ASM(err('Used region 0x'+($0>>>0).toString(16)+', size: '+($1>>>0)+' ('+($2?"used":"--FREE--")+') is corrupt (size markers in the beginning and at the end of the region do not match!)'),
443443
r, r->size, region_ceiling_size(r) == r->size);
444444
return 1;
445445
}
@@ -457,7 +457,7 @@ static int validate_memory_regions()
457457
{
458458
if (!debug_region_is_consistent(fr) || !region_is_free(fr) || fr->prev != prev || fr->next == fr || fr->prev == fr)
459459
{
460-
MAIN_THREAD_ASYNC_EM_ASM(console.log('In bucket '+$0+', free region 0x'+($1>>>0).toString(16)+', size: ' + ($2>>>0) + ' (size at ceiling: '+($3>>>0)+'), prev: 0x' + ($4>>>0).toString(16) + ', next: 0x' + ($5>>>0).toString(16) + ' is corrupt!'),
460+
MAIN_THREAD_ASYNC_EM_ASM(out('In bucket '+$0+', free region 0x'+($1>>>0).toString(16)+', size: ' + ($2>>>0) + ' (size at ceiling: '+($3>>>0)+'), prev: 0x' + ($4>>>0).toString(16) + ', next: 0x' + ($5>>>0).toString(16) + ' is corrupt!'),
461461
i, fr, fr->size, size_of_region_from_ceiling(fr), fr->prev, fr->next);
462462
return 1;
463463
}
@@ -479,7 +479,7 @@ int emmalloc_validate_memory_regions()
479479
static bool claim_more_memory(size_t numBytes)
480480
{
481481
#ifdef EMMALLOC_VERBOSE
482-
MAIN_THREAD_ASYNC_EM_ASM(console.log('claim_more_memory(numBytes='+($0>>>0)+ ')'), numBytes);
482+
MAIN_THREAD_ASYNC_EM_ASM(out('claim_more_memory(numBytes='+($0>>>0)+ ')'), numBytes);
483483
#endif
484484

485485
#ifdef EMMALLOC_MEMVALIDATE
@@ -491,12 +491,12 @@ static bool claim_more_memory(size_t numBytes)
491491
if ((intptr_t)startPtr == -1)
492492
{
493493
#ifdef EMMALLOC_VERBOSE
494-
MAIN_THREAD_ASYNC_EM_ASM(console.error('claim_more_memory: sbrk failed!'));
494+
MAIN_THREAD_ASYNC_EM_ASM(err('claim_more_memory: sbrk failed!'));
495495
#endif
496496
return false;
497497
}
498498
#ifdef EMMALLOC_VERBOSE
499-
MAIN_THREAD_ASYNC_EM_ASM(console.log('claim_more_memory: claimed ' + ptrToString($0) + ' - ' + ptrToString($1) + ' (' + ($2>>>0) + ' bytes) via sbrk()'), startPtr, startPtr + numBytes, numBytes);
499+
MAIN_THREAD_ASYNC_EM_ASM(out('claim_more_memory: claimed ' + ptrToString($0) + ' - ' + ptrToString($1) + ' (' + ($2>>>0) + ' bytes) via sbrk()'), startPtr, startPtr + numBytes, numBytes);
500500
#endif
501501
assert(HAS_ALIGNMENT(startPtr, alignof(size_t)));
502502
uint8_t *endPtr = startPtr + numBytes;
@@ -564,7 +564,7 @@ static void initialize_emmalloc_heap()
564564
freeRegionBuckets[i].prev = freeRegionBuckets[i].next = &freeRegionBuckets[i];
565565

566566
#ifdef EMMALLOC_VERBOSE
567-
MAIN_THREAD_ASYNC_EM_ASM(console.log('initialize_emmalloc_heap()'));
567+
MAIN_THREAD_ASYNC_EM_ASM(out('initialize_emmalloc_heap()'));
568568
#endif
569569

570570
// Start with a tiny dynamic region.
@@ -646,7 +646,7 @@ static void *attempt_allocate(Region *freeRegion, size_t alignment, size_t size)
646646
#endif
647647

648648
#ifdef EMMALLOC_VERBOSE
649-
MAIN_THREAD_ASYNC_EM_ASM(console.log('attempt_allocate - succeeded allocating memory, region ptr=' + ptrToString($0) + ', align=' + $1 + ', payload size=' + ($2>>>0) + ' bytes)'), freeRegion, alignment, size);
649+
MAIN_THREAD_ASYNC_EM_ASM(out('attempt_allocate - succeeded allocating memory, region ptr=' + ptrToString($0) + ', align=' + $1 + ', payload size=' + ($2>>>0) + ' bytes)'), freeRegion, alignment, size);
650650
#endif
651651

652652
return (uint8_t*)freeRegion + sizeof(size_t);
@@ -682,7 +682,7 @@ static void *allocate_memory(size_t alignment, size_t size)
682682
ASSERT_MALLOC_IS_ACQUIRED();
683683

684684
#ifdef EMMALLOC_VERBOSE
685-
MAIN_THREAD_ASYNC_EM_ASM(console.log('allocate_memory(align=' + $0 + ', size=' + ($1>>>0) + ' bytes)'), alignment, size);
685+
MAIN_THREAD_ASYNC_EM_ASM(out('allocate_memory(align=' + $0 + ', size=' + ($1>>>0) + ' bytes)'), alignment, size);
686686
#endif
687687

688688
#ifdef EMMALLOC_MEMVALIDATE
@@ -692,15 +692,15 @@ static void *allocate_memory(size_t alignment, size_t size)
692692
if (!IS_POWER_OF_2(alignment))
693693
{
694694
#ifdef EMMALLOC_VERBOSE
695-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Allocation failed: alignment not power of 2!'));
695+
MAIN_THREAD_ASYNC_EM_ASM(out('Allocation failed: alignment not power of 2!'));
696696
#endif
697697
return 0;
698698
}
699699

700700
if (size > MAX_ALLOC_SIZE)
701701
{
702702
#ifdef EMMALLOC_VERBOSE
703-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
703+
MAIN_THREAD_ASYNC_EM_ASM(out('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
704704
#endif
705705
return 0;
706706
}
@@ -811,7 +811,7 @@ static void *allocate_memory(size_t alignment, size_t size)
811811
}
812812

813813
#ifdef EMMALLOC_VERBOSE
814-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Could not find a free memory block!'));
814+
MAIN_THREAD_ASYNC_EM_ASM(out('Could not find a free memory block!'));
815815
#endif
816816

817817
return 0;
@@ -875,7 +875,7 @@ void emmalloc_free(void *ptr)
875875
return;
876876

877877
#ifdef EMMALLOC_VERBOSE
878-
MAIN_THREAD_ASYNC_EM_ASM(console.log('free(ptr=0x'+($0>>>0).toString(16)+')'), ptr);
878+
MAIN_THREAD_ASYNC_EM_ASM(out('free(ptr=0x'+($0>>>0).toString(16)+')'), ptr);
879879
#endif
880880

881881
uint8_t *regionStartPtr = (uint8_t*)ptr - sizeof(size_t);
@@ -891,9 +891,9 @@ void emmalloc_free(void *ptr)
891891
if (debug_region_is_consistent(region))
892892
// LLVM wasm backend bug: cannot use MAIN_THREAD_ASYNC_EM_ASM() here, that generates internal compiler error
893893
// Reproducible by running e.g. other.test_alloc_3GB
894-
EM_ASM(console.error('Double free at region ptr 0x' + ($0>>>0).toString(16) + ', region->size: 0x' + ($1>>>0).toString(16) + ', region->sizeAtCeiling: 0x' + ($2>>>0).toString(16) + ')'), region, size, region_ceiling_size(region));
894+
EM_ASM(err('Double free at region ptr 0x' + ($0>>>0).toString(16) + ', region->size: 0x' + ($1>>>0).toString(16) + ', region->sizeAtCeiling: 0x' + ($2>>>0).toString(16) + ')'), region, size, region_ceiling_size(region));
895895
else
896-
MAIN_THREAD_ASYNC_EM_ASM(console.error('Corrupt region at region ptr 0x' + ($0>>>0).toString(16) + ' region->size: 0x' + ($1>>>0).toString(16) + ', region->sizeAtCeiling: 0x' + ($2>>>0).toString(16) + ')'), region, size, region_ceiling_size(region));
896+
MAIN_THREAD_ASYNC_EM_ASM(err('Corrupt region at region ptr 0x' + ($0>>>0).toString(16) + ' region->size: 0x' + ($1>>>0).toString(16) + ', region->sizeAtCeiling: 0x' + ($2>>>0).toString(16) + ')'), region, size, region_ceiling_size(region));
897897
}
898898
#endif
899899
assert(size >= sizeof(Region));
@@ -947,7 +947,7 @@ static int attempt_region_resize(Region *region, size_t size)
947947
assert(HAS_ALIGNMENT(size, sizeof(size_t)));
948948

949949
#ifdef EMMALLOC_VERBOSE
950-
MAIN_THREAD_ASYNC_EM_ASM(console.log('attempt_region_resize(region=0x' + ($0>>>0).toString(16) + ', size=' + ($1>>>0) + ' bytes)'), region, size);
950+
MAIN_THREAD_ASYNC_EM_ASM(out('attempt_region_resize(region=0x' + ($0>>>0).toString(16) + ', size=' + ($1>>>0) + ' bytes)'), region, size);
951951
#endif
952952

953953
// First attempt to resize this region, if the next region that follows this one
@@ -999,7 +999,7 @@ static int attempt_region_resize(Region *region, size_t size)
999999
}
10001000
}
10011001
#ifdef EMMALLOC_VERBOSE
1002-
MAIN_THREAD_ASYNC_EM_ASM(console.log('attempt_region_resize failed.'));
1002+
MAIN_THREAD_ASYNC_EM_ASM(out('attempt_region_resize failed.'));
10031003
#endif
10041004
return 0;
10051005
}
@@ -1015,7 +1015,7 @@ static int acquire_and_attempt_region_resize(Region *region, size_t size)
10151015
void *emmalloc_aligned_realloc(void *ptr, size_t alignment, size_t size)
10161016
{
10171017
#ifdef EMMALLOC_VERBOSE
1018-
MAIN_THREAD_ASYNC_EM_ASM(console.log('aligned_realloc(ptr=0x' + ($0>>>0).toString(16) + ', alignment=' + $1 + ', size=' + ($2>>>0)), ptr, alignment, size);
1018+
MAIN_THREAD_ASYNC_EM_ASM(out('aligned_realloc(ptr=0x' + ($0>>>0).toString(16) + ', alignment=' + $1 + ', size=' + ($2>>>0)), ptr, alignment, size);
10191019
#endif
10201020

10211021
if (!ptr)
@@ -1030,7 +1030,7 @@ void *emmalloc_aligned_realloc(void *ptr, size_t alignment, size_t size)
10301030
if (size > MAX_ALLOC_SIZE)
10311031
{
10321032
#ifdef EMMALLOC_VERBOSE
1033-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
1033+
MAIN_THREAD_ASYNC_EM_ASM(out('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
10341034
#endif
10351035
return 0;
10361036
}
@@ -1084,7 +1084,7 @@ void *emmalloc_realloc_try(void *ptr, size_t size)
10841084
if (size > MAX_ALLOC_SIZE)
10851085
{
10861086
#ifdef EMMALLOC_VERBOSE
1087-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
1087+
MAIN_THREAD_ASYNC_EM_ASM(out('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
10881088
#endif
10891089
return 0;
10901090
}
@@ -1119,7 +1119,7 @@ void *emmalloc_aligned_realloc_uninitialized(void *ptr, size_t alignment, size_t
11191119
if (size > MAX_ALLOC_SIZE)
11201120
{
11211121
#ifdef EMMALLOC_VERBOSE
1122-
MAIN_THREAD_ASYNC_EM_ASM(console.log('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
1122+
MAIN_THREAD_ASYNC_EM_ASM(out('Allocation failed: attempted allocation size is too large: ' + ($0 >>> 0) + 'bytes! (negative integer wraparound?)'), size);
11231123
#endif
11241124
return 0;
11251125
}

0 commit comments

Comments
 (0)