You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Found problems:
1. The convention for sbrk() changed at some point to require 16b
alignment. (`__alignof__(max_align_t)`) This can create gaps in the
alignment of emmalloc heap end sentinel region and the previous free
region. This must require changing the assumption in emmalloc that heap
end sentinel region would always be fixed 16b -> allow it to be
arbitrary sized (in practice 16-31b).
2. sbrk() implementation had round-up to nearest max_align_t, but didn't
handle negative sbrk() values.
3. in 4GB mode, a 32-bit sbrk() is not enough to accommodate support for
both sbrk() increases and decreases (unless we take a convention that
sbrk() can only be increased by < 2GB amount.. which probably won't hold
in use cases that want to claim the whole heap immediately). Add a
function sbrk64() that can be used in 4GB builds to be able to express
both memory increases and decresases.
4. test case expectations have changed at some point. test_emmalloc_trim
was written with an assumption that emmalloc would acquire the whole
heap with sbrk() at startup.. but no longer does so. Adjusted test to
not assume this.
5. printing bare memory values in test doesn't work well in e.g. ubsan
test mode, so remove the direct value prints in the test, and just
assert() the expectations.
Fixes: #23343
MAIN_THREAD_ASYNC_EM_ASM(out('emmalloc_trim(): Last actual region '+ptrToString($0) +' is in use, there is nothing to trim from.'), lastActualRegion);
1261
+
#endif
1262
+
return0;
1251
1263
}
1252
1264
1253
-
if (!region_is_free(lastActualRegion) ||lastActualRegion->size <= newRegionSize) {
1254
-
return0; // Last actual region is in use, or caller desired to leave more free memory intact than there is.
1265
+
// Sanitize odd alignments for padding values - this is the minimum alignment
1266
+
// that emmalloc could handle. Align up to be conservative towards caller.
1267
+
pad= (size_t)ALIGN_UP(pad, 4);
1268
+
1269
+
// Calculate how many bytes we can shrink the sbrk() reservation by.
1270
+
// Is the last free region smaller than what was requested to be left behind?
1271
+
// If so, then there is nothing we can trim.
1272
+
if (pad >= lastActualRegion->size) {
1273
+
#ifdefEMMALLOC_VERBOSE
1274
+
MAIN_THREAD_ASYNC_EM_ASM(out('emmalloc_trim(): Last actual region does not have enough space to leave '+Number($0) +' bytes of free memory in it.'), pad);
MAIN_THREAD_ASYNC_EM_ASM(out('emmalloc_trim(): Shrinking '+Number($0) +' bytes off the free heap end region. New free heap end region size: '+Number($1) +' bytes.'), shrinkAmount, newRegionSize);
1304
+
#endif
1305
+
// If we can't fit a free Region in the shrunk space, we should delete the
MAIN_THREAD_ASYNC_EM_ASM(out('emmalloc_trim(): Created new free heap end region at '+ptrToString($0) +'. Size: '+Number($1) +' bytes.'), lastActualRegion, newRegionSize);
1312
+
#endif
1313
+
} else {
1314
+
#ifdefEMMALLOC_VERBOSE
1315
+
MAIN_THREAD_ASYNC_EM_ASM(out('emmalloc_trim(): Not enough room to fit a free heap end region. Discarding it altogether.'));
1316
+
#endif
1317
+
newRegionSize=0;
1267
1318
}
1268
1319
1269
-
// Recreate the sentinel region at the end of the last free region
MAIN_THREAD_ASYNC_EM_ASM(out('emmalloc_trim(): Created new sentinel end region at '+ptrToString($0) +'. Size: '+Number($1) +' bytes.'), newEndSentinelRegion, newEndSentinelRegionSize);
0 commit comments