Skip to content

Commit 74af917

Browse files
authored
Cleanup emscripten_resize_heap (#16255)
Format comments, remove dummy self-dependency, and call `emscripten_get_heap_max` to get the max heap size rather than duplicating it.
1 parent ac076f7 commit 74af917

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/library.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ LibraryManager.library = {
147147

148148
emscripten_get_heap_max: function() {
149149
#if ALLOW_MEMORY_GROWTH
150-
// Handle the case of 4GB (which would wrap to 0 in the return value) by
151-
// returning up to 4GB - one wasm page.
150+
// Stay one Wasm page short of 4GB: while e.g. Chrome is able to allocate
151+
// full 4GB Wasm memories, the size will wrap back to 0 bytes in Wasm side
152+
// for any code that deals with heap sizes, which would require special
153+
// casing all heap size related code to treat 0 specially.
152154
return {{{ Math.min(MAXIMUM_MEMORY, FOUR_GB - WASM_PAGE_SIZE) }}};
153155
#else // no growth
154156
return HEAPU8.length;
@@ -201,15 +203,16 @@ LibraryManager.library = {
201203
},
202204
#endif // ~TEST_MEMORY_GROWTH_FAILS
203205

204-
emscripten_resize_heap__deps: ['emscripten_resize_heap' // Dummy depend on itself to allow following ','s to match up.
206+
emscripten_resize_heap__deps: [
207+
'emscripten_get_heap_max',
205208
#if ASSERTIONS == 2
206-
, 'emscripten_get_now'
209+
'emscripten_get_now',
207210
#endif
208211
#if ABORTING_MALLOC
209-
, '$abortOnCannotGrowMemory'
212+
'$abortOnCannotGrowMemory',
210213
#endif
211214
#if ALLOW_MEMORY_GROWTH
212-
, '$emscripten_realloc_buffer'
215+
'$emscripten_realloc_buffer',
213216
#endif
214217
],
215218
emscripten_resize_heap: function(requestedSize) {
@@ -222,13 +225,13 @@ LibraryManager.library = {
222225
return false; // malloc will report failure
223226
#endif // ABORTING_MALLOC
224227
#else // ALLOW_MEMORY_GROWTH == 0
225-
// With pthreads, races can happen (another thread might increase the size in between), so return a failure, and let the caller retry.
228+
// With pthreads, races can happen (another thread might increase the size
229+
// in between), so return a failure, and let the caller retry.
226230
#if USE_PTHREADS
227231
if (requestedSize <= oldSize) {
228232
return false;
229233
}
230-
#endif // USE_PTHREADS
231-
#if ASSERTIONS && !USE_PTHREADS
234+
#elif ASSERTIONS
232235
assert(requestedSize > oldSize);
233236
#endif
234237

@@ -238,21 +241,25 @@ LibraryManager.library = {
238241
#endif
239242

240243
// Memory resize rules:
241-
// 1. Always increase heap size to at least the requested size, rounded up to next page multiple.
242-
// 2a. If MEMORY_GROWTH_LINEAR_STEP == -1, excessively resize the heap geometrically: increase the heap size according to
243-
// MEMORY_GROWTH_GEOMETRIC_STEP factor (default +20%),
244-
// At most overreserve by MEMORY_GROWTH_GEOMETRIC_CAP bytes (default 96MB).
245-
// 2b. If MEMORY_GROWTH_LINEAR_STEP != -1, excessively resize the heap linearly: increase the heap size by at least MEMORY_GROWTH_LINEAR_STEP bytes.
246-
// 3. Max size for the heap is capped at 2048MB-WASM_PAGE_SIZE, or by MAXIMUM_MEMORY, or by ASAN limit, depending on which is smallest
247-
// 4. If we were unable to allocate as much memory, it may be due to over-eager decision to excessively reserve due to (3) above.
248-
// Hence if an allocation fails, cut down on the amount of excess growth, in an attempt to succeed to perform a smaller allocation.
244+
// 1. Always increase heap size to at least the requested size, rounded up
245+
// to next page multiple.
246+
// 2a. If MEMORY_GROWTH_LINEAR_STEP == -1, excessively resize the heap
247+
// geometrically: increase the heap size according to
248+
// MEMORY_GROWTH_GEOMETRIC_STEP factor (default +20%), At most
249+
// overreserve by MEMORY_GROWTH_GEOMETRIC_CAP bytes (default 96MB).
250+
// 2b. If MEMORY_GROWTH_LINEAR_STEP != -1, excessively resize the heap
251+
// linearly: increase the heap size by at least
252+
// MEMORY_GROWTH_LINEAR_STEP bytes.
253+
// 3. Max size for the heap is capped at 2048MB-WASM_PAGE_SIZE, or by
254+
// MAXIMUM_MEMORY, or by ASAN limit, depending on which is smallest
255+
// 4. If we were unable to allocate as much memory, it may be due to
256+
// over-eager decision to excessively reserve due to (3) above.
257+
// Hence if an allocation fails, cut down on the amount of excess
258+
// growth, in an attempt to succeed to perform a smaller allocation.
249259

250260
// A limit is set for how much we can grow. We should not exceed that
251261
// (the wasm binary specifies it, so if we tried, we'd fail anyhow).
252-
// In CAN_ADDRESS_2GB mode, stay one Wasm page short of 4GB: while e.g. Chrome is able to allocate full 4GB Wasm memories, the size will wrap
253-
// back to 0 bytes in Wasm side for any code that deals with heap sizes, which would require special casing all heap size related code to treat
254-
// 0 specially.
255-
var maxHeapSize = {{{ Math.min(MAXIMUM_MEMORY, FOUR_GB - WASM_PAGE_SIZE) }}};
262+
var maxHeapSize = _emscripten_get_heap_max();
256263
if (requestedSize > maxHeapSize) {
257264
#if ASSERTIONS
258265
err('Cannot enlarge memory, asked to go up to ' + requestedSize + ' bytes, but the limit is ' + maxHeapSize + ' bytes!');
@@ -264,8 +271,9 @@ LibraryManager.library = {
264271
#endif
265272
}
266273

267-
// Loop through potential heap size increases. If we attempt a too eager reservation that fails, cut down on the
268-
// attempted size and reserve a smaller bump instead. (max 3 times, chosen somewhat arbitrarily)
274+
// Loop through potential heap size increases. If we attempt a too eager
275+
// reservation that fails, cut down on the attempted size and reserve a
276+
// smaller bump instead. (max 3 times, chosen somewhat arbitrarily)
269277
for (var cutDown = 1; cutDown <= 4; cutDown *= 2) {
270278
#if MEMORY_GROWTH_LINEAR_STEP == -1
271279
var overGrownHeapSize = oldSize * (1 + {{{ MEMORY_GROWTH_GEOMETRIC_STEP }}} / cutDown); // ensure geometric growth

0 commit comments

Comments
 (0)