Skip to content

Commit 540e28c

Browse files
committed
Move emscripten_get_heap_size to native code
1 parent fde16e0 commit 540e28c

18 files changed

+53
-21
lines changed

src/library.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,6 @@ LibraryManager.library = {
437437
return -1;
438438
},
439439

440-
emscripten_get_heap_size: function() {
441-
return HEAPU8.length;
442-
},
443-
444440
#if ABORTING_MALLOC
445441
$abortOnCannotGrowMemory: function(requestedSize) {
446442
#if ASSERTIONS
@@ -487,7 +483,7 @@ LibraryManager.library = {
487483
},
488484
#endif // ~TEST_MEMORY_GROWTH_FAILS
489485

490-
emscripten_resize_heap__deps: ['emscripten_get_heap_size'
486+
__emscripten_resize_heap__deps: [
491487
#if ASSERTIONS == 2
492488
, 'emscripten_get_now'
493489
#endif
@@ -498,7 +494,7 @@ LibraryManager.library = {
498494
, '$emscripten_realloc_buffer'
499495
#endif
500496
],
501-
emscripten_resize_heap: function(requestedSize) {
497+
__emscripten_resize_heap: function(oldSize, requestedSize) {
502498
#if CAN_ADDRESS_2GB
503499
requestedSize = requestedSize >>> 0;
504500
#endif
@@ -509,7 +505,6 @@ LibraryManager.library = {
509505
return false; // malloc will report failure
510506
#endif // ABORTING_MALLOC
511507
#else // ALLOW_MEMORY_GROWTH == 0
512-
var oldSize = _emscripten_get_heap_size();
513508
// With pthreads, races can happen (another thread might increase the size in between), so return a failure, and let the caller retry.
514509
#if USE_PTHREADS
515510
if (requestedSize <= oldSize) {

system/include/emscripten/heap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <stddef.h>
11+
#include <stdint.h>
1012
#include <emscripten/emscripten.h>
1113

1214
#define WASM_PAGE_SIZE 65536
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright 2021 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <emscripten/heap.h>
9+
10+
size_t emscripten_get_heap_size() {
11+
return __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2021 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <emscripten/heap.h>
9+
10+
int __emscripten_resize_heap(size_t requested_size, size_t current_size) EM_IMPORT(__emscripten_resize_heap);
11+
12+
// This is just a small wrapper the JavaScript function `__emscripten_resize_heap`.
13+
// We pass in the current size to avoid reverse dependency on
14+
// 'emscripten_get_heap_size`
15+
int emscripten_resize_heap(size_t requested_size) {
16+
return __emscripten_resize_heap(emscripten_get_heap_size(), requested_size);
17+
}

system/lib/standalone/standalone.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ void *emscripten_memcpy_big(void *restrict dest, const void *restrict src, size_
117117

118118
extern void emscripten_notify_memory_growth(size_t memory_index);
119119

120-
int emscripten_resize_heap(size_t size) {
120+
int __emscripten_resize_heap(size_t old_size, size_t size) {
121121
#ifdef __EMSCRIPTEN_MEMORY_GROWTH__
122-
size_t old_size = __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
123122
assert(old_size < size);
124123
ssize_t diff = (size - old_size + WASM_PAGE_SIZE - 1) / WASM_PAGE_SIZE;
125124
size_t result = __builtin_wasm_memory_grow(0, diff);

tests/other/metadce/hello_libcxx_O2.imports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
env.__emscripten_resize_heap
12
env.abort
23
env.emscripten_memcpy_big
3-
env.emscripten_resize_heap
44
env.setTempRet0
55
env.strftime_l
66
wasi_snapshot_preview1.environ_get

tests/other/metadce/hello_libcxx_O2.sent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__cxa_atexit
2+
__emscripten_resize_heap
23
abort
34
emscripten_memcpy_big
4-
emscripten_resize_heap
55
environ_get
66
environ_sizes_get
77
fd_close

tests/other/metadce/hello_libcxx_O2_fexceptions.imports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ env.__cxa_free_exception
77
env.__cxa_rethrow
88
env.__cxa_throw
99
env.__cxa_uncaught_exceptions
10+
env.__emscripten_resize_heap
1011
env.__resumeException
1112
env.abort
1213
env.emscripten_memcpy_big
13-
env.emscripten_resize_heap
1414
env.getTempRet0
1515
env.invoke_diii
1616
env.invoke_fiii

tests/other/metadce/hello_libcxx_O2_fexceptions.sent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ __cxa_free_exception
88
__cxa_rethrow
99
__cxa_throw
1010
__cxa_uncaught_exceptions
11+
__emscripten_resize_heap
1112
__resumeException
1213
abort
1314
emscripten_memcpy_big
14-
emscripten_resize_heap
1515
environ_get
1616
environ_sizes_get
1717
fd_close

tests/other/metadce/hello_libcxx_O2_fexceptions_DEMANGLE_SUPPORT.imports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ env.__cxa_free_exception
77
env.__cxa_rethrow
88
env.__cxa_throw
99
env.__cxa_uncaught_exceptions
10+
env.__emscripten_resize_heap
1011
env.__resumeException
1112
env.abort
1213
env.emscripten_memcpy_big
13-
env.emscripten_resize_heap
1414
env.getTempRet0
1515
env.invoke_diii
1616
env.invoke_fiii

0 commit comments

Comments
 (0)