Skip to content

Move emscripten_get_heap_size to native code #13695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,6 @@ LibraryManager.library = {
return -1;
},

emscripten_get_heap_size: function() {
return HEAPU8.length;
},

#if ABORTING_MALLOC
$abortOnCannotGrowMemory: function(requestedSize) {
#if ASSERTIONS
Expand Down Expand Up @@ -487,7 +483,7 @@ LibraryManager.library = {
},
#endif // ~TEST_MEMORY_GROWTH_FAILS

emscripten_resize_heap__deps: ['emscripten_get_heap_size'
__emscripten_resize_heap__deps: [
#if ASSERTIONS == 2
, 'emscripten_get_now'
#endif
Expand All @@ -498,7 +494,7 @@ LibraryManager.library = {
, '$emscripten_realloc_buffer'
#endif
],
emscripten_resize_heap: function(requestedSize) {
__emscripten_resize_heap: function(oldSize, requestedSize) {
#if CAN_ADDRESS_2GB
requestedSize = requestedSize >>> 0;
#endif
Expand All @@ -509,7 +505,6 @@ LibraryManager.library = {
return false; // malloc will report failure
#endif // ABORTING_MALLOC
#else // ALLOW_MEMORY_GROWTH == 0
var oldSize = _emscripten_get_heap_size();
// With pthreads, races can happen (another thread might increase the size in between), so return a failure, and let the caller retry.
#if USE_PTHREADS
if (requestedSize <= oldSize) {
Expand Down
2 changes: 2 additions & 0 deletions system/include/emscripten/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#include <stddef.h>
#include <stdint.h>
#include <emscripten/emscripten.h>

#define WASM_PAGE_SIZE 65536
Expand Down
12 changes: 12 additions & 0 deletions system/lib/libc/emscripten_get_heap_size.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2021 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <emscripten/heap.h>

size_t emscripten_get_heap_size() {
return __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
}
17 changes: 17 additions & 0 deletions system/lib/libc/emscripten_resize_heap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2021 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <emscripten/heap.h>

int __emscripten_resize_heap(size_t requested_size, size_t current_size) EM_IMPORT(__emscripten_resize_heap);

// This is just a small wrapper the JavaScript function `__emscripten_resize_heap`.
// We pass in the current size to avoid reverse dependency on
// 'emscripten_get_heap_size`
int emscripten_resize_heap(size_t requested_size) {
return __emscripten_resize_heap(emscripten_get_heap_size(), requested_size);
}
3 changes: 1 addition & 2 deletions system/lib/standalone/standalone.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ void *emscripten_memcpy_big(void *restrict dest, const void *restrict src, size_

extern void emscripten_notify_memory_growth(size_t memory_index);

int emscripten_resize_heap(size_t size) {
int __emscripten_resize_heap(size_t old_size, size_t size) {
#ifdef __EMSCRIPTEN_MEMORY_GROWTH__
size_t old_size = __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
assert(old_size < size);
ssize_t diff = (size - old_size + WASM_PAGE_SIZE - 1) / WASM_PAGE_SIZE;
size_t result = __builtin_wasm_memory_grow(0, diff);
Expand Down
2 changes: 1 addition & 1 deletion tests/other/metadce/hello_libcxx_O2.imports
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
env.__emscripten_resize_heap
env.abort
env.emscripten_memcpy_big
env.emscripten_resize_heap
env.setTempRet0
env.strftime_l
wasi_snapshot_preview1.environ_get
Expand Down
2 changes: 1 addition & 1 deletion tests/other/metadce/hello_libcxx_O2.sent
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__cxa_atexit
__emscripten_resize_heap
abort
emscripten_memcpy_big
emscripten_resize_heap
environ_get
environ_sizes_get
fd_close
Expand Down
2 changes: 1 addition & 1 deletion tests/other/metadce/hello_libcxx_O2_fexceptions.imports
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ env.__cxa_free_exception
env.__cxa_rethrow
env.__cxa_throw
env.__cxa_uncaught_exceptions
env.__emscripten_resize_heap
env.__resumeException
env.abort
env.emscripten_memcpy_big
env.emscripten_resize_heap
env.getTempRet0
env.invoke_diii
env.invoke_fiii
Expand Down
2 changes: 1 addition & 1 deletion tests/other/metadce/hello_libcxx_O2_fexceptions.sent
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ __cxa_free_exception
__cxa_rethrow
__cxa_throw
__cxa_uncaught_exceptions
__emscripten_resize_heap
__resumeException
abort
emscripten_memcpy_big
emscripten_resize_heap
environ_get
environ_sizes_get
fd_close
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ env.__cxa_free_exception
env.__cxa_rethrow
env.__cxa_throw
env.__cxa_uncaught_exceptions
env.__emscripten_resize_heap
env.__resumeException
env.abort
env.emscripten_memcpy_big
env.emscripten_resize_heap
env.getTempRet0
env.invoke_diii
env.invoke_fiii
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ __cxa_free_exception
__cxa_rethrow
__cxa_throw
__cxa_uncaught_exceptions
__emscripten_resize_heap
__resumeException
abort
emscripten_memcpy_big
emscripten_resize_heap
environ_get
environ_sizes_get
fd_close
Expand Down
2 changes: 1 addition & 1 deletion tests/other/metadce/hello_world_O3_MAIN_MODULE_2.imports
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
GOT.mem.__heap_base
env.__emscripten_resize_heap
env.__indirect_function_table
env.__memory_base
env.__stack_pointer
env.__table_base
env.emscripten_memcpy_big
env.emscripten_resize_heap
env.memory
env.setTempRet0
wasi_snapshot_preview1.fd_write
2 changes: 1 addition & 1 deletion tests/other/metadce/hello_world_O3_MAIN_MODULE_2.sent
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
__emscripten_resize_heap
__indirect_function_table
__memory_base
__stack_pointer
__table_base
emscripten_memcpy_big
emscripten_resize_heap
fd_write
memory
setTempRet0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$__emscripten_resize_heap
$__main_void
$__wasm_call_ctors
$_start
$dlmalloc
$emscripten_resize_heap
$sbrk
2 changes: 1 addition & 1 deletion tests/other/test_split_main_module.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
wrote profile of 232 bytes (allocated 232 bytes)
wrote profile of 240 bytes (allocated 240 bytes)
Hello from main!
Hello from lib!
2 changes: 1 addition & 1 deletion tests/other/test_split_module.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
wrote profile of 224 bytes (allocated 224 bytes)
wrote profile of 232 bytes (allocated 232 bytes)
Hello! answer: 42
1 change: 1 addition & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -9981,6 +9981,7 @@ def test_split_module(self):
self.assertNotIn('profile', result)
self.assertIn('Hello! answer: 42', result)

@disabled('https://github.com/WebAssembly/binaryen/issues/3701')
def test_split_main_module(self):
initialTableSize = 16

Expand Down
8 changes: 7 additions & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,13 @@ def get_files(self):

libc_files += files_in_path(
path_components=['system', 'lib', 'libc'],
filenames=['extras.c', 'wasi-helpers.c', 'emscripten_pthread.c'])
filenames=[
'extras.c',
'wasi-helpers.c',
'emscripten_pthread.c',
'emscripten_get_heap_size.c',
'emscripten_resize_heap.c',
])

libc_files += files_in_path(
path_components=['system', 'lib', 'pthread'],
Expand Down